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