Help with simple animation in JApplet.

J

John D.

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);
}
}
 
A

Andrew Thompson

John said:
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
 
J

John D.

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.
 
J

John D.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top