paint method for component
hi, i am having this problem with graphics painting. i am supposed to create 2 circle which will blink at different timing. however since the circles are close together, the blinking effect is not correct. i have pasted my codes below. any help will be appreciated!
import java.awt.*;
import javax.swing.Timer;
import java.awt.event.*;
import javax.swing.*;
public class problem {
public problem() {
}
public static void main(String[] args) throws Exception {
problem main = new problem();
run panel = new run();
panel.setPreferredSize(new Dimension(300, 300));
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(500, 500));
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static class run extends JPanel implements ActionListener {
private boolean flash = false;
private boolean t2 = false;
public run() {
Timer timer1 = new Timer(2000, this);
Timer timer2 = new Timer(500, this);
timer1.start();
timer2.start();
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillOval(50, 50, 100, 100);
if (flash) {
flash = false;
} else {
g.setColor(Color.red);
g.fillOval(50, 50, 50, 50);
flash = true;
}
}
public void actionPerformed(ActionEvent e) {
this.repaint();
}
}
}
[1465 byte] By [
iboluia] at [2007-11-26 18:34:00]

# 1
Override paintComponent(), not paint().
Also note that the way you have coded it,
- only the red circle will blink.
- both timers do exactly the same, ie cause the red circle to change state
If you want the two circles to flash at different rates, you need a boolean for each circle and you need a separate listener for each timer, each setting the relevant boolean.
# 2
do you mean change to this method?
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
g.fillOval(50, 50, 100, 100);
if (flash) {
flash = false;
} else {
g.setColor(Color.red);
g.fillOval(50, 50, 50, 50);
flash = true;
}
}
i tried but it still doesnt work..
yup i know the blue circle is not blinking. i only set the red to be blinking as i think this shld be enuf to see the effect.
# 6
is this what you're trying to do?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Problem
{
public Problem()
{
Run panel = new Run();
panel.setPreferredSize(new Dimension(300, 300));
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
class Run extends JPanel implements ActionListener
{
int counter = 0;
public Run()
{
Timer timer1 = new Timer(500, this);
timer1.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(counter % 8 < 4)
{
g.setColor(Color.blue);
g.fillOval(50, 50, 100, 100);
}
if(counter % 2 == 0)
{
g.setColor(Color.red);
g.fillOval(50, 50, 50, 50);
}
}
public void actionPerformed(ActionEvent e)
{
counter++;
this.repaint();
}
}
public static void main(String[] args){new Problem();}
}
# 8
> how is it that the use of counter works?
I'm using one timer, not two.
the timer code is to increase counter by 1, then repaint
repaint calls paintComponent(), which firstly clears the previous paint
super.paintComponent(g);
then, depending on the value of the counter, the red/blue circles are drawn
if(counter % 8 < 4)
so, for 2 seconds (4 x 500) the blue circle is drawn, then another 2 seconds it is not drawn
i.e.'flashing' every 2 seconds
similar for the red circle, but for every 500 ms