I'm in an introduction to programming class, and I just made a program that draws a beautiful picture of a truck out of a bunch of circles, triangles and squares. You'll have to take my word for it that those shape classes are all made up, but now that I've got it drawn I want to make the vehicle drive away off the screen. I tried to make a method that would move all the objects, but it moves them one at a time, so the headlight scrolls off the screen and when it's done a wheel goes, then the windshield, etc.
I think what I need to do is some kind of loop, so each object moves only one pixel before the next object goes and then it all starts over again and over again until every object has moved as far as I want it it. But I don't know how to do that. Can someone help? here's my code:
/**
* This class was heavily modified for Assignment number 1 in CSIS 1400 -
* section 1 by Jake Moses. It originaly displayed a simple house with
* a setting sun. Now it displays a complex truck on a stary night during
* a lunar eclipse. The original author's comments are below:
*
* This class represents a simple picture. You can draw the picture using
* the draw method. But wait, there's more: being an electronic picture, it
* can be changed. You can set it to black-and-white display and back to
* colors (only after it's been drawn, of course).
*
* This class was written as an early example for teaching Java with BlueJ.
*
* @author Michael Kolling and David J. Barnes
* @version 1.1 (24 May 2001)
*/
public class Picture
{
private Square sky;
private Square grass;
private Square bumper_1;
private Square bumper_2;
private Square hood_1;
private Square hood_2;
private Square bed_1;
private Square bed_2;
private Square bed_3;
private Square window;
private Square cab;
private Triangle windshield;
private Triangle hoodslant;
private Triangle doorline_1;
private Triangle doorline_2;
private Triangle handle;
private Circle wheel_1;
private Circle wheel_2;
private Circle headlamp;
private Circle moon;
private Circle eclipse;
private Circle star_1;
private Circle star_2;
private Circle star_3;
private Circle star_4;
private Circle star_5;
private Circle star_6;
private Circle star_7;
/**
* Constructor for objects of class Picture
*/
public Picture()
{
// nothing to do... instance variables are automatically set to null
}
/**
* Draw this picture.
* Code changed by Jake Moses on Sept. 20, 2005 for assignment.
* I made all new fields and changed the constructor and methods
* to create and manipulate a much more complicated picture than
* the house with the sunset.
*/
public void draw()
{
sky = new Square();
sky.changeColor("blue");
sky.moveVertical(-100);
sky.moveHorizontal(-100);
sky.changeSize(1000);
sky.makeVisible();
grass = new Square();
grass.changeColor("green");
grass.moveVertical(190);
grass.moveHorizontal(-100);
grass.changeSize(1000);
grass.makeVisible();
moon = new Circle();
moon.changeColor("yellow");
moon.moveHorizontal(24);
moon.moveVertical(-48);
moon.changeSize(40);
moon.makeVisible();
eclipse = new Circle();
eclipse.changeColor("blue");
eclipse.moveHorizontal(68);
eclipse.moveVertical(-48);
eclipse.changeSize(40);
eclipse.makeVisible();
star_1 = new Circle();
star_1.changeColor("yellow");
star_1.moveHorizontal(75);
star_1.moveVertical(-10);
star_1.changeSize(3);
star_1.makeVisible();
star_2 = new Circle();
star_2.changeColor("yellow");
star_2.moveHorizontal(245);
star_2.moveVertical(0);
star_2.changeSize(3);
star_2.makeVisible();
star_3 = new Circle();
star_3.changeColor("yellow");
star_3.moveHorizontal(255);
star_3.moveVertical(-15);
star_3.changeSize(3);
star_3.makeVisible();
star_4 = new Circle();
star_4.changeColor("yellow");
star_4.moveHorizontal(145);
star_4.moveVertical(-45);
star_4.changeSize(3);
star_4.makeVisible();
star_5 = new Circle();
star_5.changeColor("yellow");
star_5.moveHorizontal(103);
star_5.moveVertical(18);
star_5.changeSize(3);
star_5.makeVisible();
star_6 = new Circle();
star_6.changeColor("yellow");
star_6.moveHorizontal(-15);
star_6.moveVertical(-3);
star_6.changeSize(3);
star_6.makeVisible();
star_7 = new Circle();
star_7.changeColor("yellow");
star_7.moveHorizontal(193);
star_7.moveVertical(-5);
star_7.changeSize(3);
star_7.makeVisible();
windshield = new Triangle();
windshield.changeColor("white");
windshield.changeSize(45, 40);
windshield.moveHorizontal(80);
windshield.moveVertical(135);
windshield.makeVisible();
cab = new Square();
cab.changeColor("red");
cab.moveVertical(90);
cab.moveHorizontal(70);
cab.changeSize(40);
cab.makeVisible();
window = new Square();
window.changeColor("white");
window.moveHorizontal(74);
window.moveVertical(97);
window.changeSize(33);
window.makeVisible();
headlamp = new Circle();
headlamp.changeColor("white");
headlamp.moveHorizontal(35);
headlamp.moveVertical(130);
headlamp.changeSize(15);
headlamp.makeVisible();
hood_1 = new Square();
hood_1.changeColor("red");
hood_1.moveVertical(130);
hood_1.changeSize(50);
hood_1.makeVisible();
hoodslant = new Triangle();
hoodslant.changeColor("blue");
hoodslant.changeSize(-11, 120);
hoodslant.moveHorizontal(

;
hoodslant.moveVertical(174);
hoodslant.makeVisible();
hood_2 = new Square();
hood_2.changeColor("red");
hood_2.moveVertical(130);
hood_2.moveHorizontal(50);
hood_2.changeSize(50);
hood_2.makeVisible();
bed_1 = new Square();
bed_1.changeColor("red");
bed_1.moveVertical(130);
bed_1.moveHorizontal(100);
bed_1.changeSize(50);
bed_1.makeVisible();
bed_2 = new Square();
bed_2.changeColor("red");
bed_2.moveVertical(130);
bed_2.moveHorizontal(115);
bed_2.changeSize(50);
bed_2.makeVisible();
bed_3 = new Square();
bed_3.changeColor("red");
bed_3.moveVertical(130);
bed_3.moveHorizontal(130);
bed_3.changeSize(50);
bed_3.makeVisible();
doorline_1 = new Triangle();
doorline_1.changeColor("black");
doorline_1.changeSize(70, 2);
doorline_1.moveHorizontal(82);
doorline_1.moveVertical(146);
doorline_1.makeVisible();
doorline_2 = new Triangle();
doorline_2.changeColor("black");
doorline_2.changeSize(70, 2);
doorline_2.moveHorizontal(120);
doorline_2.moveVertical(146);
doorline_2.makeVisible();
handle = new Triangle();
handle.changeColor("black");
handle.changeSize(3, 15);
handle.moveHorizontal(110);
handle.moveVertical(170);
handle.makeVisible();
bumper_1 = new Square();
bumper_1.changeColor("black");
bumper_1.moveVertical(164);
bumper_1.moveHorizontal(-8);
bumper_1.changeSize(16);
bumper_1.makeVisible();
bumper_2 = new Square();
bumper_2.changeColor("black");
bumper_2.moveVertical(164);
bumper_2.moveHorizontal(172);
bumper_2.changeSize(16);
bumper_2.makeVisible();
wheel_1 = new Circle();
wheel_1.changeColor("black");
wheel_1.moveHorizontal(65);
wheel_1.moveVertical(150);
wheel_1.changeSize(35);
wheel_1.makeVisible();
wheel_2 = new Circle();
wheel_2.changeColor("black");
wheel_2.moveHorizontal(160);
wheel_2.moveVertical(146);
wheel_2.changeSize(40);
wheel_2.makeVisible();
}
/**
* Make car drive off the screen. Unfortunately it goes
* one piece at a time. Code added Sept. 20, 2005 by Jake Moses.
* Too bad it's broken

*/
public void drive()
{
if(hood_1 != null) // only if it's painted already...
{
hood_1.slowMoveHorizontal(-500);
hood_2.slowMoveHorizontal(-500);
hoodslant.slowMoveHorizontal(-500);
bed_1.slowMoveHorizontal(-500);
bed_2.slowMoveHorizontal(-500);
bed_3.slowMoveHorizontal(-500);
windshield.slowMoveHorizontal(-500);
cab.slowMoveHorizontal(-500);
window.slowMoveHorizontal(-500);
headlamp.slowMoveHorizontal(-500);
bumper_1.slowMoveHorizontal(-500);
bumper_2.slowMoveHorizontal(-500);
doorline_1.slowMoveHorizontal(-500);
doorline_2.slowMoveHorizontal(-500);
handle.slowMoveHorizontal(-500);
wheel_1.slowMoveHorizontal(-500);
wheel_2.slowMoveHorizontal(-500);
}
}
/**
* Initiate a lunar eclipse.
* Code added Sept 20, 2005 to add a cool method.
*/
public void eclipse()
{
if(hood_1 != null) // only if it's painted already...
{
eclipse.slowMoveHorizontal(-100);
}
}
/**
* Change this picture to black/white display
* Changed Sept. 20, 2005 by Jake Moses to reflect changes in fields
*/
public void setBlackAndWhite()
{
if(hood_1 != null) // only if it's painted already...
{
sky.changeColor("white");
grass.changeColor("black");
moon.changeColor("black");
eclipse.changeColor("white");
star_1.changeColor("black");
star_2.changeColor("black");
star_3.changeColor("black");
star_4.changeColor("black");
star_5.changeColor("black");
star_6.changeColor("black");
star_7.changeColor("black");
hood_1.changeColor("black");
hood_2.changeColor("black");
bed_1.changeColor("black");
bed_2.changeColor("black");
bed_3.changeColor("black");
windshield.changeColor("white");
cab.changeColor("black");
bumper_1.changeColor("black");
bumper_2.changeColor("black");
window.changeColor("white");
wheel_1.changeColor("white");
wheel_2.changeColor("white");
doorline_1.changeColor("white");
doorline_2.changeColor("white");
handle.changeColor("white");
hoodslant.changeColor("white");
}
}
/**
* Change this picture to use color display
* Changed Sept. 20, 2005 by Jake Moses to reflect changes in fields
*/
public void setColor()
{
if(hood_1 != null) // only if it's painted already...
{
sky.changeColor("blue");
grass.changeColor("green");
moon.changeColor("yellow");
star_1.changeColor("yellow");
star_2.changeColor("yellow");
star_3.changeColor("yellow");
star_4.changeColor("yellow");
star_5.changeColor("yellow");
star_6.changeColor("yellow");
star_7.changeColor("yellow");
eclipse.changeColor("blue");
hood_1.changeColor("red");
hood_2.changeColor("red");
bed_1.changeColor("red");
bed_2.changeColor("red");
bed_3.changeColor("red");
windshield.changeColor("black");
cab.changeColor("red");
bumper_1.changeColor("black");
bumper_2.changeColor("black");
window.changeColor("black");
wheel_1.changeColor("black");
wheel_2.changeColor("black");
doorline_1.changeColor("black");
doorline_2.changeColor("black");
handle.changeColor("black");
hoodslant.changeColor("blue");
}
}
}