| CIT 591 MotionPicture Example Fall 2002, David Matuszek |
Here is the complete code for the MotionPicture application. It's
also available as a BlueJ project in this zip file.
MotionPicture.java |
import java.awt.*; // So we can draw
/**
* This application draws a few figures in a window
* and moves them around.
*
* @author David Matuszek
* @version 1.0
*/
public class MotionPicture extends Frame {
Figure[] figures = new Figure[7];
Graphics graphics;
/**
* The main method just creates a MotionPicture object and
* tells it to do all the work, thus avoiding certain problems
* with static variables.
*/
public static void main(String args[]) {
MotionPicture p = new MotionPicture();
p.doTheWork();
}
/**
* Creates figures and moves them around in a window.
*/
void doTheWork() {
// Create the user interface
Panel panel = new Panel();
add(panel);
setSize(500, 400);
setVisible(true);
graphics = panel.getGraphics();
// Create the figures
figures[0] = new Blob();
figures[1] = new Blob();
figures[2] = new BlueGuy();
figures[3] = new BlueGuy();
figures[4] = new Ring();
figures[5] = new Ring();
figures[6] = new Clover();
for (int timer = 0; timer < 300; timer++) {
// Move the figures
for (int i = 0; i < figures.length; i++) {
figures[i].move(panel.getWidth(), panel.getHeight());
}
// Draw the figures
for (int i = 0; i < figures.length; i++) {
figures[i].draw(graphics);
}
// Pause
try { Thread.sleep(50); } catch (InterruptedException e) {}
// Erase the figures
for (int i = 0; i < figures.length; i++) {
figures[i].erase(graphics);
}
graphics.fillRect(0, 0, panel.getWidth(), panel.getHeight());
}
// Close the window
dispose();
}
}
|
Figure.java |
import java.awt.*; // So we can draw
/**
* This application draws a few figures in a window
* and moves them around.
*
* @author David Matuszek
* @version 1.0
*/
public class MotionPicture extends Frame {
Figure[] figures = new Figure[7];
Graphics graphics;
/**
* The main method just creates a MotionPicture object and
* tells it to do all the work, thus avoiding certain problems
* with static variables.
*/
public static void main(String args[]) {
MotionPicture p = new MotionPicture();
p.doTheWork();
}
/**
* Creates figures and moves them around in a window.
*/
void doTheWork() {
// Create the user interface
Panel panel = new Panel();
add(panel);
setSize(500, 400);
setVisible(true);
graphics = panel.getGraphics();
// Create the figures
figures[0] = new Blob();
figures[1] = new Blob();
figures[2] = new BlueGuy();
figures[3] = new BlueGuy();
figures[4] = new Ring();
figures[5] = new Ring();
figures[6] = new Clover();
for (int timer = 0; timer < 300; timer++) {
// Move the figures
for (int i = 0; i < figures.length; i++) {
figures[i].move(panel.getWidth(), panel.getHeight());
}
// Draw the figures
for (int i = 0; i < figures.length; i++) {
figures[i].draw(graphics);
}
// Pause
try { Thread.sleep(50); } catch (InterruptedException e) {}
// Erase the figures
for (int i = 0; i < figures.length; i++) {
figures[i].erase(graphics);
}
graphics.fillRect(0, 0, panel.getWidth(), panel.getHeight());
}
// Close the window
dispose();
}
}
|
Blob.java |
import java.awt.*;
/**
* A pulsating red blob.
*
* @author David Matuszek
* @version 1.0
*/
public class Blob extends Figure {
boolean expanding = true;
Blob() {
width = 30;
height = 20;
}
void draw(Graphics g) {
g.setColor(Color.red);
if (expanding) {
height = height + 1;
if (height >= 60) {
expanding = false;
}
} else { // contracting
height = height - 4;
if (height <= 10) {
expanding = true;
}
}
g.fillOval(x, y, width, height);
}
}
|
BlueGuy.java |
import java.awt.*;
/**
* An unhappy face..
*
* @author David Matuszek
* @version 1.0
*/
public class BlueGuy extends Figure {
BlueGuy() {
width = 35;
height = 28;
}
void draw(Graphics g) {
g.setColor(new Color(0x6699FF));
g.fillOval(x, y, width, height);
g.setColor(Color.black);
g.fillOval(x + 10, y + 10, 5, 5);
g.fillOval(x + 20, y + 10, 5, 5);
g.drawArc(x, y + 20, width, height + 10, 45, 90);
}
}
|
Clover.java |
import java.awt.*;
/**
* A green clover.
*
* @author David Matuszek
* @version 1.0
*/
public class Clover extends Figure {
Clover() {
width = 60;
height = 60;
}
void draw(Graphics g) {
g.setColor(new Color(0x00CC00));
g.fillOval(x, y + 20, 30, 20);
g.fillOval(x + 30, y + 20, 30, 20);
g.fillOval(x + 20, y, 20, 30);
g.fillOval(x + 20, y + 30, 20, 30);
}
}
|
Ring.java |
import java.awt.*;
/**
* A ring-shaped object.
*
* @author David Matuszek
* @version 1.0
*/
public class Ring extends Figure {
Ring() {
width = 50;
height = 50;
}
void draw(Graphics g) {
g.setColor(Color.orange);
for (int i = 0; i < 5; i++) {
g.drawOval(x + i, y + i, width - i - i, height - i - i);
}
}
}
|
Example output |
|