This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Applets and the fly likes Getting this to move. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Getting this to move." Watch "Getting this to move." New topic
Author

Getting this to move.

Kelly Frasure
Greenhorn

Joined: Mar 03, 2001
Posts: 15
I need this to move across the screen. Can anyone help?
This is an assignment for school, and I can't use any methods from the polygon class. I think I need to change the array values, but not quite sure how to go about it.
Here is the 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();
}
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kelly,
To make something move we need a point to move and all the other drawing points must be relative to it. See the following code. I started with your code and changed a few things.

Enjoy,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting this to move.