aspose file tools*
The moose likes Beginning Java and the fly likes animation problem in applet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "animation problem in applet." Watch "animation problem in applet." New topic
Author

animation problem in applet.

Kelly Frasure
Greenhorn

Joined: Mar 03, 2001
Posts: 15
I am having trouble trying to figure out how to get this to move across the screen. It is a truck. This is an assignment for school and I can't use any methods from the polygon class. I think I need to change the values in the array and then the location. I'm just not sure how to go about it. Please help. Here is my code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Truck1 extends Applet
{
private final int APPLET_WIDTH = 400;
private final int APPLET_HEIGHT = 400;
private final int DELAY = 20;
private Timer timer;
public int[] xStart = {15, 15, 15, 15, 15, 68, 70, 72, 75, 77,
80, 80, 80, 67, 64, 64, 64, 39, 19, 15};
public int[] yStart = {8, 70, 73, 75, 77, 77, 77, 77, 77, 77, 77,
55, 33, 33, 33, 20, 8, 8, 8, 8};
public int[] xMove = {20, 20, 20, 20, 20, 73, 75, 77, 80, 82,
85, 85, 85, 72, 69, 69, 69, 44, 24, 20};
public int[] yMove = {13, 75, 78, 80, 82, 82, 82, 82, 82, 82, 82,
60, 38, 38, 38, 25, 13, 13, 13, 13};
//===============================================================
public void init()
{
timer = new Timer (DELAY, new ReboundActionListener());
timer.start();
setBackground (Color.white);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
//===============================================================
public void paint (Graphics page)
{
page.setColor (Color.red);
page.fillPolygon (xStart, yStart, xStart.length);
page.fillPolygon (xMove, yMove, xMove.length);
page.fillOval(25, 77, 25, 25); //left tire
page.fillOval(55, 77, 25, 25); //right tire
page.setColor(Color.white);
page.fillOval(30, 85, 10, 10); //center of left tire
page.fillOval(60, 85, 10, 10); //center of right tire
}
//=============================================================
private class ReboundActionListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
repaint();
}
}
}
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hi, Kelly!
This truck rolls, not very smoothly, across the screen... See if you can use some of this code.
//import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Truck1 extends Frame
{
private final int APPLET_WIDTH = 400;
private final int APPLET_HEIGHT = 400;
private int x = 0;
private final int DELAY = 20;
private Timer timer;
public int[] xStart = {15, 15, 15, 15, 15, 68, 70, 72, 75, 77,
80, 80, 80, 67, 64, 64, 64, 39, 19, 15};
public int[] yStart = {8, 70, 73, 75, 77, 77, 77, 77, 77, 77, 77,
55, 33, 33, 33, 20, 8, 8, 8, 8};
public int[] xMove = {20, 20, 20, 20, 20, 73, 75, 77, 80, 82,
85, 85, 85, 72, 69, 69, 69, 44, 24, 20};
public int[] yMove = {13, 75, 78, 80, 82, 82, 82, 82, 82, 82, 82,
60, 38, 38, 38, 25, 13, 13, 13, 13};
//===============================================================
public Truck1()
{
timer = new Timer (DELAY, new ReboundActionListener());
timer.start();
setBackground (Color.white);
setSize (500, 500);
setVisible(true);
}
//===============================================================
public void paint (Graphics page)
{
page.setColor (Color.red);
page.fillPolygon (xStart, yStart, xStart.length);
page.fillPolygon (xMove, yMove, xMove.length);
page.fillOval(x+25, 77, 25, 25); //left tire25
page.fillOval(x+55, 77, 25, 25); //right tire55
page.setColor(Color.white);
page.fillOval(x+30, 85, 10, 10); //center of left tire30
page.fillOval(x+60, 85, 10, 10); //center of right tire60
}
//=============================================================
private class ReboundActionListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
for (int i = 0; i < xStart.length; i++)
{
xStart[i] += 1;
xMove[i] += 1;
}
x += 1;
repaint();
}
}
public static void main(String[] a) {
new Truck1();
}
}

As you can see I've made your applet an application. Don't bother about that. Focus on the additions in the ReboundActionListener. And also note that I've added a variable x to your wheels, so they can move along with the rest of the truck.
Hope this helps!
/Kaspar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: animation problem in applet.