• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting this to move.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic