Files
processing4/java/examples/Topics/Motion/Bounce/applet/Bounce.java
benfry eb64b2d4fc
2011-01-26 19:22:19 +00:00

68 lines
1.4 KiB
Java

import processing.core.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class Bounce extends PApplet {
/**
* Bounce.
*
* When the shape hits the edge of the window, it reverses its direction.
*/
int size = 60; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed = 2.8f; // Speed of the shape
float yspeed = 2.2f; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
public void setup()
{
size(640, 200);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
}
public void draw()
{
background(102);
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-size || ypos < 0) {
ydirection *= -1;
}
// Draw the shape
ellipse(xpos+size/2, ypos+size/2, size, size);
}
static public void main(String args[]) {
PApplet.main(new String[] { "Bounce" });
}
}