Hi all,
I've having problems with my repaint() in the applet below. Can
someone please suggest how to fix it?
I'm trying to create a simple animation, by painting three circles,
and varying their locations. I created a simple for loop to make the
animation run a 100 times. I want to repaint() after each iteration.
I've included a Thread.sleep() method to have time to notice the swap.
Problem is that the repaint() does not seem to get called at each
iteration.
Can someone please explain how to get around this? Thanks.
Cheers,
John D.
/****************************************/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TestAnim extends JApplet implements MouseListener
{
int x1 = 100, y1 = 50;
int x2 = 50, y2 = 150;
int x3 = 150, y3 = 150;
final int size = 50;
public void mousePressed(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{
for(int i = 0; i < 100; i++)
{
swapPos();
System.out.println(i);
repaint(); // PROBLEM FOUND HERE. THIS REPAINT() REFUSES
TO WORK.
try
{
Thread.sleep(500);
}
catch(InterruptedException ie)
{
}
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x1, y1, size, size);
g.setColor(Color.blue);
g.fillOval(x2, y2, size, size);
g.setColor(Color.green);
g.fillOval(x3, y3, size, size);
}
public void swapPos()
{
int xTemp = x1; int yTemp = y1;
x1 = x2; y1 = y2;
x2 = x3; y2 = y3;
x3 = xTemp; y3 = yTemp;
}
public void start()
{
addMouseListener(this);
}
}
Roedy Green - 31 Oct 2005 03:30 GMT
> Thread.sleep(500);
> }
Just like humans, a Java thread cannot paint in its sleep.
You are tying up the Swing Thread so it never gets around to painting
anything.
See http://mindprod.com/jgloss/swingthreads.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Andrew Thompson - 31 Oct 2005 04:30 GMT
> I've having problems with my repaint() in the applet
JApplet, just so we are all on the same page..
>..below. Can
> someone please suggest how to fix it?
Why are you using a JApplet? If overriding 'paint()/paintComponent()'
to draw the entire area yourself, it makes sense to use the simplest
container that can do it - in the case, a java.applet.Applet.
> I'm trying to create a simple animation, by painting three circles,
> and varying their locations. I created a simple for loop to make the
> animation run a 100 times. I want to repaint() after each iteration.
> I've included a Thread.sleep() method to have time to notice the swap.
> Problem is that the repaint() does not seem to get called at each
> iteration.
That will not work (as you have seen).
<http://www.physci.org/guifaq.jsp#2.4>
Check here for a working AWT (override paint()) example of animation..
<http://www.physci.org/launcher.jsp?class=/codes/AnimateBalls/AnimateFrame>
..and Swing (override paintComponent()) animation..
<http://www.physci.org/launcher.jsp?class=/codes/AnimateBalls/JAnimateFrame>
HTH
John D. - 01 Nov 2005 11:08 GMT
Hi all,
Thanks for your help, but I'm still a bit in the dark, mainly because
I'm a newbie.
Could someone please point me towards an example of an applet in which
animation is triggered by an event, e.g. a mouse click.
Thanks,
John D.
Roedy Green - 01 Nov 2005 11:22 GMT
>Could someone please point me towards an example of an applet in which
>animation is triggered by an event, e.g. a mouse click.
Presumably you know how to write a listener.
If not see http://mindprod.com/jgloss/event11.html
in your listener just do
compoentToDraw.repaint();
to trigger another paint cycle.
See Timer if you want repaint to be called automatically every x
milliseconds. See http://mindprod.com/jgloss/timer.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
John D. - 02 Nov 2005 04:40 GMT
Hi all,
Thanks again. But I believe I'm properly lost.
I believe I did put my repaint() in my listener, i.e. MouseClicked.
Can you show me which modifications are needed in my code, or else
point me to an example on the web where an animation is specifically
triggered by an event.
Thanks,
John.