Sep 11, 2005 20:01
import java.awt.Color;
import yops.*;
/*
* This illustrates how various lines and shapes can be created and added to a graphics display.
*/
public class Lab0 {
Lab0(GraphicsPanel display) {
// Start by adding a circle to the display. This is done by creating an ellipse whose width
// and height are both 25. The first two parameters (5 and 10) are the x and y coordinates of the
// upper left corner of the "bounding box" of the Ellipse. A bounding box is the smallest rectangle
// that contains the shape.
Ellipse sun = new Ellipse(5,10,25,25);
sun.setFillColor(Color.YELLOW);
display.add(sun);
// Add two lines. The parameters are x1, y1, x2, y2 (the endpoints of the lines)
display.add(new Line(90,100,120,80));
display.add(new Line(150,100,120,80));
// Add a rectangle. The parameters are x, y, width, height (where x and y are
// the upper left corner of the rectangle.
display.add(new Rectangle(90,100,60,80));
addWindow(display, 100, 150, 10);
//add two new windows
addWindow2(display, 100, 120, 10);
addWindow3 (display, 115, 90, 5);
// add a door
display.add(new Rectangle(120,140,20,40));
Ellipse knob = new Ellipse(120,155,5,5);
knob.setFillColor(Color.BLACK);
display.add(knob);
}
void addWindow(GraphicsPanel panel, int x, int y, int size) {
panel.add(new Line(x+size/2, y, x+size/2, y+size));
panel.add(new Line(x, y+size/2, x+size, y+size/2));
Rectangle box = new Rectangle(x,y,size,size);
box.setFillColor(Color.YELLOW);
panel.add(box);
}
void addWindow2(GraphicsPanel panel, int x, int y, int size) {
panel.add(new Line(x+size/2, y, x+size/2, y+size));
panel.add(new Line(x, y+size/2, x+size, y+size/2));
Rectangle box = new Rectangle(x,y,size,size);
box.setFillColor(Color.BLUE);
panel.add(box);
}
void addWindow3(GraphicsPanel panel, int x, int y, int size) {
panel.add(new Line(x+size/2, y, x+size/2, y+size));
panel.add(new Line(x, y+size/2, x+size, y+size/2));
Rectangle box = new Rectangle(x,y,size,size);
box.setFillColor(Color.RED);
panel.add(box);
}
// The "main" method is where your program starts. For now, don't worry about what this code does.
public static void main(String[] args) {
YOPS yops = new YOPS(200, 200); // create a YOPS window with 1 panel that is 200x300
new Lab0(yops.getGraphicsPanel(0));
}
}