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 4 of 4

Thread: How to use for loop for movement of points in a picture display?

Threaded View

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

    Default How to use for loop for movement of points in a picture display?

    This is my code so far.

    It is a picture displaying an alien and a house. My assignment was to randomly generates 20 stars onto the screen, and it works well. The second part of the assignment is to make the alien spaceship move from one integer to the next depending on what the user enters, however I am having trouble understanding the process of how to do this. I know I need to create a forloop for the movement of the ship, but I don't know how to get it to move to where the user enters. If you can help me please do, (if you feel like doing it for me and then explaining it, you could do that as well to save my butt ).

    I HAVE STARTED THE SECOND PART OF THE ASSIGNMENT AT THE TOP HOWEVER I HAVE COMMENTED IT SO THAT THE STARS CAN RUN PROPERLY.

    import java.util.*;
    import hsa.Console;
    import java.awt.Color;
    public class Alien
    {
        static Console c;           // The output console
     
        public static void main (String[] args)
        {
            c = new Console ("My Alien Creature");
     
            int heightOfWindow, widthOfWindow, time, star;
            double x, y;
            x = 0;
            y = 0;
     
    //c.println ("Please enter two numbers.");
    //c.println ("\nThe Alien controlling the spaceship will then fly the ship between both of the \nintegers that you entered.");
     
            //x = c.readInt ();
            //y = c.readInt ();
           // c.clear();
     
    {
     
                //Drawing Commands for my Space Ship
     
                c.setColor (Color.black); //Night Sky
                c.fillRect (0, 0, 800, 800);
     
                c.setColor (Color.green);
                c.fillRect (0, 425, 850, 425);
     
                c.setColor (Color.blue); //Floor; Ground
                c.fillRect (85, 225, 200, 200); //House Rectangle
     
                c.setColor (Color.blue);
                c.fillRect (85, 225, 200, 200);
     
                c.setColor (Color.cyan);
                c.fillRect (195, 325, 50, 100); //Window Square
     
                c.setColor (Color.red);
                c.fillRect (195, 325, 50, 100);
     
                c.setColor (Color.cyan);
                c.fillRect (115, 325, 50, 50); //Door Rectangle
     
                c.setColor (Color.green);
                c.fillOval (350, 275, 200, 65); //Body
     
                c.setColor (Color.red);
                c.fillOval (375, 305, 15, 10); //Upper Portion
     
                c.setColor (Color.red);
                c.fillOval (505, 305, 15, 10); //Head
     
                c.setColor (Color.green);
                c.fillOval (395, 250, 105, 40); // Spaceship
     
                c.setColor (Color.blue);
                c.fillOval (505, 75, 100, 85); //Dot on Spaceship
     
                c.setColor (Color.red);
                c.fillOval (425, 215, 11, 6); //EYE #1
     
                c.setColor (Color.red);
                c.fillOval (450, 215, 11, 6); //EYE #2
     
                c.setColor (Color.green);
                c.fillOval (417, 199, 58, 50); // Other Dot on Spaceship
     
                c.setColor (Color.red);
                c.drawLine (450, 205, 475, 185);
                c.setColor (Color.red);
     
                c.drawLine (435, 205, 410, 185);
                c.setColor (Color.blue);
     
                c.drawLine (85, 225, 185, 115);
     
                c.setColor (Color.blue);
                c.drawLine (285, 225, 185, 115);
            }
            {
                delay ();
            }
        }
     
     
        public static void delay ()
        {
            int heightOfWindow = 100;
            int widthOfWindow = c.getWidth ();
            int time = 0;
            int star = 0;
     
            for (star = 0 ; star < 20 ; star++)
            {
     
     
                double x = java.lang.Math.random ();
                double y = java.lang.Math.random ();
                int xInt = (int) (x * widthOfWindow);
                int yInt = (int) (y * heightOfWindow);
     
                c.setColor (Color.yellow);
                c.fillStar (xInt, yInt, 50, 50);
                //generate random number
     
            }
        }
    }
    Last edited by Deep_4; November 7th, 2012 at 01:15 PM.


Similar Threads

  1. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  2. New assignments for JAVA beginners
    By Peetah05 in forum File I/O & Other I/O Streams
    Replies: 24
    Last Post: April 24th, 2009, 11:33 AM
  3. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  4. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM