escape_room - Cos 240 Object Oriented Languages Professor:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Cos 240 – Object Oriented Languages Professor: Svetla Boycheva Assignment 2: Escape Room 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
I hereby declare that the submitted answers are all my own work; that I did not copy them from someone else; and that no one but me answered the questions. Signature March 23, 2010 I have successfully completed the assignment. Code 2
Background image of page 2
SecretObject.java import java.awt.*; import java.util.*; import java.applet.*; import java.awt.event.*; import javax.swing.JLabel; public class SecretObject { private int x_pos; //the x coordinate of the secret object private int y_pos; //the y coordinate of the secret object private int x_speed; //the speed across the x axis private int y_speed; //the speed across the y axis private static final int right_out = 530; //the maximum x coordinate the secret object can have private static final int left_out = 10; //the minimum x coordinate the secret object can have private static final int down_out =350; //the maximum y coordinate the secret object can have private static final int up_out = 10; //the minimum y coordinate the secret object can have private int width; //width of the secret object private int height; //height of the secret object private int shape; //1 for rectangle, 2 for oval private Color originalColor;//original color of the secret object private Color color; //current color of the secret object private int order; //the order to be clicked on public SecretObject (int id) //create objects with random coordinates, shapes and speed { Color[] colors = {Color.PINK, Color.RED, Color.ORANGE, Color.YELLOW, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.BLUE, Color.GRAY}; Random gen = new Random(); this.x_pos = gen.nextInt(551); this.y_pos = gen.nextInt(376); width = gen.nextInt(56) + 20; height = gen.nextInt(26) + 20; shape = gen.nextInt(2); originalColor = color = colors[gen.nextInt(colors.length)]; order = id; x_speed = gen.nextInt(4) + 1; y_speed = gen.nextInt(4) + 1; } public void move ()//moves the object around the screen { x_pos += x_speed; y_pos += y_speed; 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
isOut(); } public void isOut () //checks whether the object is inside the specified boundaries { if (x_speed < 0) { if (x_pos < left_out) //if at the left boundary and moving to the left invert the direction of movement { x_speed = - x_speed; } } else if (x_speed > 0) //if at the right boundary and moving to the right invert the direction of movement { if (x_pos > right_out) { x_speed = - x_speed; } }
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

escape_room - Cos 240 Object Oriented Languages Professor:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online