Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I need Help Creating A "for loop" for some really basic java code. Plz help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy 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

    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");
    		}
    	}
     
    }
    Last edited by helloworld922; October 29th, 2009 at 09:21 PM.


  2. #2
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default 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...
    Last edited by rsala004; October 29th, 2009 at 04:22 PM.

  3. The Following 2 Users Say Thank You to rsala004 For This Useful Post:

    JavaPF (October 30th, 2009), Reiss (October 29th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Reiss; October 29th, 2009 at 07:06 PM.

Similar Threads

  1. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  2. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  3. HELP "code needed"
    By Dave in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: August 26th, 2009, 11:06 AM
  4. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM