0

why is my java.awt.Window not repainting after I called the repaint() method?

public class Counter extends Window implements ActionListener {

private static final long serialVersionUID = 1L;
private Timer timer;
private int time;

public Counter() {
    super(null);
    setAlwaysOnTop(true);
    setBounds(getGraphicsConfiguration().getBounds());
    setBackground(new Color(0, true));
    setVisible(true);
    timer = new Timer(1000, this);
    timer.start();
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.clearRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.RED);
    g.drawString(String.valueOf(time), getWidth()/2, getHeight()/2);
}

@Override
public void update(Graphics g) {
    super.update(g);
}

@Override
public void actionPerformed(ActionEvent e) {
    time++;
    repaint();
}

As you can see i created a timer with a delay of 1 second. After that i call repaint() to draw the counter's number on the screen. But it only draws a zero on my screen and stops drawing then (the zero stays on screen). First i thought that the paint method is only called once, but i tested a System.out.prinln() and prooved that the paint method is executed every second so it should actually repaint the window... So i don't know where i made a mistake.

And yes it is my intention to use the awt.Window and not a JFrame or Frame etc..

AvarionDE
  • 160
  • 11

1 Answers1

2

I finally got to run the code on Windows 7, and I could replicate the issue. For some reason, paint isn't being called; why, I don't know. Because I wouldn't do it this way, I've never had that issue.

Instead, I'd start by having the counter class extend from something JPanel or JComponent (just remember to make JPanel transparent) and then add it to a JWindow, something like this:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;


public class Counter extends JPanel implements ActionListener {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JWindow window = new JWindow();
                window.add(new Counter());
                window.pack();
                window.setLocationRelativeTo(null);
                window.setBackground(new Color(0, 0, 0, 0));
                window.setVisible(true);
            }
        });
    }

    private static final long serialVersionUID = 1L;
    private Timer timer;
    private int time;

    public Counter() {
        setOpaque(false);
        timer = new Timer(1000, this);
        timer.start();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
        System.out.println(time);
        g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.RED);
        g.drawString(String.valueOf(time), getWidth() / 2, getHeight() / 2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        time++;
        System.out.println("..." + time);
        repaint();
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366