Someone told me that one (or more) redraw(s) is/are triggered even after I do timer.stop(). I just cannot see how and would really appreciate it if someone could bring it out in an obvious way.
Anyways, here is the code:
Any help would be greatly appreciated!Code:import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; public class Bounds extends JPanel { int x = 50, y = 50, width = 20, height = 20; Timer timer; public Bounds() { JFrame frame = new JFrame(); frame.setVisible(true); frame.setSize(800,600); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this); ActionListener listener = new graphicsListener(); timer = new Timer(200, listener); timer.start(); } protected void paintComponent(Graphics g) { super.paintComponent(g); // so that the background gets refreshed g.drawRect(x, y, width, height); System.out.println("Panel width: " + getWidth() + " Panel height: " + getHeight()); System.out.println("x+width = " + (x+width)); System.out.println("y+height = " + (y+height)); if(x+width >= getWidth() || y+height >= getHeight()) { System.out.println("Out of bounds!"); JOptionPane.showMessageDialog(null, "Out of bounds!"); timer.stop(); // Why isn't this having an effect? } x+=10; y+=10; width+=20; height+=20; } public class graphicsListener implements ActionListener { public void actionPerformed(ActionEvent event) { repaint(); } } public static void main(String[] args) { new Bounds(); } }
Thanks in advance!


Reply With Quote
Bookmarks