0

Problem: I am trying to update the canvas with new painting objects based on user action. The canvas dosent get updated.

What i have done: The user interacts with the DnD action,The transferrable object reaches the canvas, Calls an update graphics method created by me. And the method simply uses the aldready created graphics 2d object and draws images using it.I have checkd the DnD action,the object is properly recived at canvas class and i was able to print them out using System.out.println.

A sample code,that has a similar function to that of mine,

Paint class:

    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;


public class PaintPanel extends JPanel{

    private Graphics2D drawImage;

    public PaintPanel()
    {

    }

    @Override
    public void paint(Graphics g) {
         drawImage = (Graphics2D) g;
         drawImage.setColor(Color.WHITE);
         drawImage.fillRect(0, 0, getWidth(), getHeight());
    }

    public void updateGraphics(int length,int width)
    {
        drawImage.setColor(Color.black);
        drawImage.drawRect(100, 150, length, width);
        repaint();
    }

}

mainframe class:

       import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;


public class MainPaint extends JFrame{


    public MainPaint()
    {
        setTitle("test paint");
        setSize(400,400);
        setLayout(new BorderLayout());

        final PaintPanel paintPan = new PaintPanel();
        JButton testButon = new JButton("Display shape");
        add(paintPan,BorderLayout.CENTER);
        add(testButon,BorderLayout.PAGE_END);

        testButon.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                paintPan.updateGraphics(50,50);
                repaint();
            }
        });
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new MainPaint();
    }

}
Balaram26
  • 1,349
  • 3
  • 15
  • 32
  • I shall post an working code as soon as possible.I thought if i post what i have done,that might help to spot the mistake or the wrong approach that i have done here. – Balaram26 Dec 26 '12 at 11:17
  • I just posted a working sample code of the issue.Thanks for the replies – Balaram26 Dec 26 '12 at 11:37
  • what i posted is not the real code,but just an sample short executable code ,that has the same issue. – Balaram26 Dec 26 '12 at 11:44

2 Answers2

9
Graphics2D drawImage;  //source of the problem!

Don't attempt to cache a Graphics (or Graphics2D) instance! Instead:

  1. Add the new objects to a list
  2. Call repaint().
  3. In paintComponent(Graphics) draw the list of objects.

An alternative to that is to use a BufferedImage as the drawing object. See this answer for an example.


Update - SSCCE based on latest code.

MainPaint after button press

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainPaint extends JFrame {

    public MainPaint() {
        setTitle("test paint");
        setSize(400, 400);
        setLayout(new BorderLayout());

        final PaintPanel paintPan = new PaintPanel();
        JButton testButon = new JButton("Display shape");
        add(paintPan, BorderLayout.CENTER);
        add(testButon, BorderLayout.PAGE_END);

        testButon.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                paintPan.updateGraphics(50, 50);
                repaint();
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
        new MainPaint();
    }
}

class PaintPanel extends JPanel {

    private int x, y;
    private Color color = null;

    public PaintPanel() {
        setBackground(Color.ORANGE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D drawImage = (Graphics2D) g;
        if (color != null) {
            drawImage.setColor(color);
            drawImage.drawRect(100, 150, x, y);
        }
    }

    public void updateGraphics(int length, int width) {
        color = Color.RED;
        x = length;
        y = width;
        repaint();
    }
}

Note

There are still a number of things about that code that need changing. I decided to stop at the earliest variant that worked to display the rectangle on button click.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for the reply.My doubt can we create say a new rectangle completely by pressing display image button? – Balaram26 Dec 26 '12 at 12:11
0

I think you need to call the validate() method.

UVM
  • 9,776
  • 6
  • 41
  • 66