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?

  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.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need Help for School Assignment.

    Hello Dman and welcome to the Java Programming Forums

    I would like to be able to compile this code but I don't have hsa.Console

    Can you please attach this or tell me where I can download it?

    Once I can compile the application I will have a play with it and see if I can offer a solution..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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

    Default Re: Need Help for School Assignment.

    Part 2 Make your UFO fly
    Use a for-loop to make theUFO in your NightSky program fly across the sky along a straight path
    between two points that the user enters.
    Requirements
    1. Pseudocode and a flow chart that show the logic of the program. (4 TIPS)
    2. Java code that
    a. shows that you know how to use a for-loop (4 KU)
    b. draws the correct output, even if the dimensions of your drawing are modified (4 A)
    c. would be easy for someone else to read, understand and modify (4 C)
    3. Name the new class A5yourLastNameAnimation.
    Hints
    • First decide how many frames you want your program to draw.
    • Before you get to the loop, your program will first need to compute how much horizontal and
    vertical distance the UFO must cover in each frame.
    • To slow down the animation, insert a “time waster” loop inside your main for-loop. For
    example,
    for(int time = 0; time <= 100000; time++)
    {
    // nothing goes here
    }
    Without such a time-wasting loop, Java runs the animation so fast that your eye cannot see it.


    PS. I cannot send you hsa.Console because none of the uploading files support the filetype that it is.

    I could transfer the files into hsa.stdin if you wish, I'm not sure if you have that file however.

    I am sending you the link to our classroom site, you can download our version of java with hsa.Console through the support resources column on the right hand side under the file called read171_install. I'm not sure if this link will work however, so if not I guess you can't do much about it.

    If you can do hsa.Stdin I can convert the file to that type as well if you like, thanks.
    Last edited by Dman; April 7th, 2009 at 01:06 PM.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need Help for School Assignment.

    Hello Dman,

    Please send me the link to your classroom site. I still have not got it...

    You could email me the class file if you wish? Ill reply to your PM with my email address.

    I need to have a version of this code that I can compile before I can offer any assistance.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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