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

• April 6th, 2009, 08:26 AM
Dman
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 :P).

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

Code :

```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   } } }```
• April 6th, 2009, 08:35 AM
JavaPF
Re: Need Help for School Assignment.
Hello Dman and welcome to the Java Programming Forums :D

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

Once I can compile the application I will have a play with it and see if I can offer a solution..
• April 7th, 2009, 12:40 PM
Dman
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.
• April 8th, 2009, 09:19 AM
JavaPF
Re: Need Help for School Assignment.
Hello Dman,