How to do various Animation tasks in HSA2

How to make a rectangle object and draw it

Rectangle r1 = new Rectangle(150,300,200,80);

Recall, the parameters for rectangle are x,y,width,height, and that Y-axis is downwards so that the height goes down from (x,y) which is the top left corner.
Put the declaration of the rectangle in the global variables area.

In drawGraphics() add in this line:

gc.drawRect(r1.x, r1.y, r1.width, r1.height);

How to see if your mouse intersects a rectangle

In setup() add this:

gc.enableMouseMotion();
Now we can get the mouse position using x- and y- coordinates: mx = gc.getMouseX(); and my = gc.getMouseY();

You'd want to declare

int mx,my;
in the global variables area.
You could call the variables mouseX and mouseY instead of mx,my.

THIS DOES NOT WORK Get the position as a point object: Point mousePT = gc.getMousePosition();

We can use this in the "checking for collisions" section of our program:

if (r1.contains(mx, my)) {
    //do whatever you want to do when the mouse hits rectangle r1.
}
OR
if (r1.contains(gc.getMouseX(), gc.getMouseY())) {
    //do whatever you want to do when the mouse hits rectangle r1.
}

What sort of things could you do if you hit a rectangle?

Detecting keystrokes


if (gc.getKeyChar() == 'p') {...}

See http://quarkphysics.ca/ICS3U1/unit4/Keyboard.html
Please read the notes in this link, especially the program at the bottom, and learn how to use gc.getKeyCode() as well as the way to handle multiple keypresses at once via gc.isKeyDown(...);

Detecting mouse clicks


See http://quarkphysics.ca/ICS3U1/unit4/Mouse.html

Making a Rectangle Move

You already know how to make a rectangle and draw it, so to make it move, we'll just have to change the (x,y) values of the rectangle.
I'm assuming that you have a game loop. So just put this into the gameloop somewhere (normally, you would have a method to do this, e.g.
void moveRect(){
   r1.x += xspeed;
   r1.y += yspeed;
}

The next thing to do is to handle what happens when it goes off the screen:
* does it bounce back?
* does it come back on the other side of the screen?
* does the player lose a life/point? (e.g. in Pong or Breakout)

If you can make a rectangle move, you can move anything. Almost everything that we use is basically a rectangle.

Make a ball / rectanle / ... bounce off the edge of the screen

The left side of the screen is when x==0; the right is when x==ScreenWidth (whatever your constant is for the width of your screen).
if (ball.x < 0) { //This means that the left side of the ball is off of the left side of the screen if (vx < 0) { //Also check to make sure that the ball is moving to the left. //Use whatever your x-speed is called (ball.vx, vx, speedx, xspeed, ...) vx = -vx; //Make the ball move in the opposite direction ball.x = 0; //For really precise bouncing, move the left side of the ball exactly to the left side of the screen } } or

if (ball.x < 0 && ball.vx < 0) {
    vx *= -1;
    ball.x = 0;
}

You can do the same with the other sides -- the top is identical to the left --, but you have to be able to figure out where the right side of the ball is if you want it to bounce off of the right side of the screen.

Did the mouse click on a Rectangle (or Ball)?

This is useful for when you make your own buttons.

Here's the button code:
Rectangle btnGo = new Rectangle(150,300,200,80);
and in some sort of loop:
//wait until the user clicks 'go'
while(true){
   if (gc.getMouseClick() > 0) {
      int mx = gc.getMouseX();
      int my = gc.getMouseY();
      if (btnGo.contains(mx,my)) { //user has clicked on the button
         btnGo.y = -200;  //move the button off of the screen or find some other way to make it disappear (See "making an intro screen" below)
         break;
      }
   }
   gc.sleep(50); //sleep 50 ms before checking again if the mouse has been clicked
}

Making an Intro Screen

http://quarkphysics.ca/ICS3U1/unit4/introScreen.html

Make the graphics window start in full screen.

There are a number of ways to do this. The following way may not be a simple as

gc.setExtendedState(JFrame.MAXIMIZED_BOTH);
SCRW = gc.getDrawWidth();
SCRH = gc.getDrawHeight();
but at least it works reliably.

The next 3 lines should be put in the global variables section. Because they are labelled "static" they will be run BEFORE the GraphicsConsole gets created, so the GraphicsConsole can use these values for the size GraphicsConsole gc = new GraphicsConsole(SCRW, SCRH);

static Toolkit tk = Toolkit.getDefaultToolkit();  
static int SCRW = ((int) tk.getScreenSize().getWidth());  
static int SCRH = ((int) tk.getScreenSize().getHeight());