I need Help Creating A "for loop" for some really basic java code. Plz help
I am in university and have just started programming. we were told to create an image comprising of only squares circles and triangles and also to make it mov.
i created a boat comprising of 4 squares and a triangle for the sail.
the image is complete however when i compile it and run it each square moves at a different time so the boat doesn't look like its moving instead it looks like different shapes are moving at different times.
i was told i could make all of the shapes move at the same time using a for loop moving each shape one pixel at a time however i have no idea how to implement this. here is the code below which i have written so far.
somebody plz help :(
Code :
public class Picture
{
private Square ocean;
private Square boat;
private Triangle sail;
private Circle sun;
private Square sail2;
private Square sail3;
private Square boat2;
/**
* Constructor for objects of class Picture
*/
public Picture()
{
// nothing to do... instance variables are automatically set to null
}
/**
* Draw this picture.
*/
public void draw()
{
ocean = new Square();
ocean.moveVertical(150);
ocean.changeSize(800);
ocean.makeVisible();
ocean.changeColor("blue");
ocean.moveHorizontal(-60);
boat = new Square();
boat.changeColor("black");
boat.moveHorizontal(20);
boat.moveVertical(100);
boat.makeVisible();
boat.changeSize(70);
boat2 = new Square();
boat2.makeVisible();
boat2.changeColor("black");
boat2.moveHorizontal(90);
boat2.moveVertical(100);
boat2.changeSize(70);
sail = new Triangle();
sail.changeSize(100, 70);
sail.moveHorizontal(75);
sail.moveVertical(15);
sail.makeVisible();
sail.changeColor("red");
sail2 = new Square();
sail2.changeColor("black");
sail2.changeSize(20);
sail2.moveHorizontal(65);
sail2.moveVertical(80);
sail2.makeVisible();
sail3 = new Square();
sail3.changeColor("black");
sail3.changeSize(20);
sail3.moveVertical(80);
sail3.moveHorizontal(40);
sun = new Circle();
sun.changeColor("yellow");
sun.moveHorizontal(350);
sun.moveVertical(-100);
sun.changeSize(90);
sun.makeVisible();
sun.slowMoveVertical(110);
}
/**
* Change this picture to black/white display
*/
public void setBlackAndWhite()
{
if(ocean != null) // only if it's painted already...
{
ocean.changeColor("black");
boat.changeColor("white");
sail.changeColor("black");
sun.changeColor("black");
}
}
/**
* Change this picture to use color display
*/
public void setColor()
{
if(ocean != null) // only if it's painted already...
{
ocean.changeColor("red");
boat.changeColor("black");
sail.changeColor("green");
sun.changeColor("yellow");
}
}
}
Re: I need Help Creating A "for loop" for some really basic java code. Plz help
you are going to have to use a for loop to move then a little bit at a time (each)
I see 5 things here, 4 squares and a triangle
boats 1 and 2, sails 1 2 and 3
We can move it to the right like this, lets say ..move right by 90 pixels
I am not sure how well this will work since it may happen very fast, try it and fiddle with it (try changing how much it moves every cycle? from 3 to 1 maybe ..maybe from 3 to 6?)
for(int i=0; i < 30 ; ++i)
{
boat1.slowMoveHorizontal(3);
boat2.slowMoveHorizontal(3);
sail1.slowMoveHorizontal(3);
sail2.slowMoveHorizontal(3);
sail3.slowMoveHorizontal(3);
}
basically I will run the code in the { } 30 times,
the first section declares a variable i and sets it to 0
next it says keep running this code while i is still less than 30
next it says to add 1 to i every time..
Every time it runs it will move each boatpart a little bit...might look kinda funny though
(notice it runs 30 times, and moves each time 3 pixels...total 90)
in my opinion this is a stupid assignment to be teaching...for loops shouldn't be bothered with till you learn most of the even more basics...
Re: I need Help Creating A "for loop" for some really basic java code. Plz help
I got it to work!!!!!!!
im so grateful for your help.
thank you