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.

itchyscratchya at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 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.

iboluia at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

i am supposed to create 2 circle which will blink at different timing... i only set the red to be blinking as i think this shld be enuf

the blinking effect is not correct

it still doesnt work

Does the word "vague" mean anything to you? ;o)

What is the effect you're trying to achieve with this code you've posted? What's the actual effect you're seeing?

itchyscratchya at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 4
Learn how to use the "Code" formatting tags when posting code so the code is readable.
camickra at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

oh... i din notice there is a code tag. sorry abt that :p

what i am trying to achieve is that the painting of 2 circles at different timing, with the location very near to each other, will not have the undesired effect similar to the codes i have posted. because its like the red circle is being cropped.

is it possible?

iboluia at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 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();}

}

Michael_Dunna at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 7
cool! this shld be something i want. i will try it out in the proj to see if it works :)btw how is it that the use of counter works?
iboluia at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...
# 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

Michael_Dunna at 2007-7-9 6:08:06 > top of Java-index,Desktop,Core GUI APIs...