Java I
Jonah Warrenjonah@parsons.edu
http://a.parsons.edu/~java2004
Lab 5
Complete the following exercises:
- Create a RolloverBlock class that has xPos, yPos (integers) and a isOver (boolean) as variables.
- Create a constructor that takes in an xPos, a yPos and sets them from parameters that are passed in.
- In the constructor, initialize isOver to false.
- Create a draw function called draw() in your class that draws a black or white block based on isOver.
- Create a function called update() that checks to see if the cursor is over the block and sets isOver appropriately.
- Create an instance of your class in the setup() function, and call your update and draw functions from the loop() function.
int canvasSize=200;
Car[] car = new Car[10];
void setup() {
size(canvasSize, canvasSize);
for(int i=0;i<10;i++) {
car[i] = new Car(random(canvasSize), random(canvasSize), random(2.0));
}
}
void loop() {
background(255);
for(int i=0;i<10;i++) {
car[i].update();
car[i].draw();
}
}
class Car {
float xPos;
float yPos;
float speed;
Car(float xp, float yp, float sp) {
xPos = xp;
yPos = yp;
speed = sp;
}
void draw() {
rect(xPos, yPos, 25, 10);
}
void update() {
xPos = xPos + speed;
if (xPos > canvasSize) {
xPos = 0;
}
else if (xPos < 0) {
xPos = canvasSize;
}
}
}
Homework 5
(Please email me if you have any questions or problems)- Look over this processing example related to objects...
- Objects
- Look out this week for an email with other reading.
- Creating your own class / objects:
- Create a class that draws a picture of your choosing, or draws the first initial in your name.
- Use some class variables that will affect the drawing's positioning and dimensions.
- Create a draw function for your class, which you call from the loop function. Create an update function if you need one.
- Have the drawing affected by either the mouseX, or mouseY or a keyboard press. Info on keyboad inputhere.
- Create an array of these objects, and use the index of a for loop to determine the positioning of your objects.
- Post this composition.
Here is an example of an object that responds to mouse position. - Dynamically creating objects:
- Use the class you created in question 1. Try and figure out how to start with a blank canvas, and the create a new drawing of the object you made in question 1 in the position where the user clicks.
- Post this composition.
- HINTS:
- You will need to create an array of your objects - make it 100 so potentially, 100 objects can be drawn on the canvas.
- You will need to have a variable called numObjects which is initialized to 0
- When a mouse click happens, create a new object, with x and y appropriately initialized
- Increment numObjects.
- Use numObjects in your for loops somehow.
- Finish up last week's assignment if you haven't.