1

I wanted to make a 2D game. I started making the drawing class, but I came across a problem: the ActionListener wouldn't work. It wouldn't draw or output my message to say it was working. Here is the code:

public class Drawing extends JPanel implements ActionListener {

    private int count = 0;

    public void actionPerformed(ActionEvent e) {
        count++;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        System.out.println("Hi");
        g.setColor(Color.black);
        g.clearRect(0, 0, Boot.WIDTH, Boot.HEIGHT);
        g.fillRect(0, 0, Boot.WIDTH, Boot.HEIGHT);

        g.setColor(Color.white);
        g.drawString("Path count: " + count, 50, 50);
    }
}

I would assume this would work, as I used this way of drawing in other projects. What would be causing this?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91

1 Answers1

4

You're not supposed to keep a reference to a Graphics object and call paint() directly. You're supposed to call repaint(), and wait for Swing to call the paintComponent() method, that you should override to perform your custom paintings on the Graphics object that Swing passes as argument to the method.

See http://java.sun.com/products/jfc/tsc/articles/painting/index.html for more information.

public class Drawing extends JPanel implements ActionListener {

    private int count = 0;

    public void actionPerformed(ActionEvent e) {
        count++;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        System.out.println("Hi");
        g.setColor(Color.black);
        g.clearRect(0, 0, Boot.WIDTH, Boot.HEIGHT);
        g.fillRect(0, 0, Boot.WIDTH, Boot.HEIGHT);

        g.setColor(Color.white);
        g.drawString("Path count: " + count, 50, 50);
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • @Duncan Palmer and Boot.frame.getGraphics(), do nothing, will go away on first Swing event that internally to repaint JComponent, do nothing without saving to BufferedImage +1 – mKorbel Jun 08 '12 at 22:34
  • Does it matter if I use a AWT Frame or Swing JFrame? – Duncan Palmer Jun 08 '12 at 23:00
  • @Duncan: Yes it probably matters. This is a Swing application, and so you shouldn't be using a Frame object at all, only a JFrame. Otherwise you risk losing many of the advantages of Swing. – Hovercraft Full Of Eels Jun 08 '12 at 23:04
  • So you're saying that you do not see the hi message each time the JButton that uses this ActionListener is pressed? – Hovercraft Full Of Eels Jun 08 '12 at 23:33
  • It isn't for a JButton, this is for drawing to the JFrames JPanel . – Duncan Palmer Jun 09 '12 at 00:15
  • @Duncan: then why the ActionListener? If you don't have a mechanism for calling `repaint()` then why would you expect the code to change count or repaint the GUI? -- unless you're placing the ActionListener in a Swing Timer or something similar. – Hovercraft Full Of Eels Jun 09 '12 at 01:59
  • I thought that is how you did it. How would I draw to the JPanel contiously? – Duncan Palmer Jun 09 '12 at 02:04
  • Thought that was how you did what? It appears that there is much you need to clarify, especially exactly what you're trying to do. – Hovercraft Full Of Eels Jun 09 '12 at 02:06
  • @DuncanPalmer : Here have a look [How to Draw String on Click, of a Button](http://stackoverflow.com/questions/10809514/using-the-coordinate-plane-in-the-jframe/10811315#10811315) – nIcE cOw Jun 09 '12 at 03:31