package polygon;
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class Polygon extends JFrame implements Runnable {
static final int XBORDER = 0;
static final int YBORDER = 0;
static final int YTITLE = 30;
static final int WINDOW_BORDER = 8;
static final int WINDOW_WIDTH = 2*(WINDOW_BORDER + XBORDER) + 800;
static final int WINDOW_HEIGHT = YTITLE + WINDOW_BORDER + 2 * YBORDER + 500;
boolean animateFirstTime = true;
int xsize = -1;
int ysize = -1;
Image image;
Graphics2D g;
int polygonMove;
int polygonRotate;
static Polygon frame;
public static void main(String[] args) {
frame = new Polygon();
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Polygon() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.BUTTON1 == e.getButton()) {
//left button
// location of the cursor.
int xpos = e.getX();
int ypos = e.getY();
}
if (e.BUTTON3 == e.getButton()) {
//right button
reset();
}
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
repaint();
}
});
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.VK_UP == e.getKeyCode()) {
} else if (e.VK_DOWN == e.getKeyCode()) {
} else if (e.VK_LEFT == e.getKeyCode()) {
} else if (e.VK_RIGHT == e.getKeyCode()) {
}
repaint();
}
});
init();
start();
}
Thread relaxer;
////////////////////////////////////////////////////////////////////////////
public void init() {
requestFocus();
}
////////////////////////////////////////////////////////////////////////////
public void destroy() {
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public void paint(Graphics gOld) {
if (image == null || xsize != getSize().width || ysize != getSize().height) {
xsize = getSize().width;
ysize = getSize().height;
image = createImage(xsize, ysize);
g = (Graphics2D) image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
//fill background
g.setColor(Color.cyan);
g.fillRect(0, 0, xsize, ysize);
int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
//fill border
g.setColor(Color.white);
g.fillPolygon(x, y, 4);
// draw border
g.setColor(Color.red);
g.drawPolyline(x, y, 5);
if (animateFirstTime) {
gOld.drawImage(image, 0, 0, null);
return;
}
Color Fred = new Color (89,20,80);
g.setColor(Fred);
g.fillRect(130, 80, 40, 70);
g.setColor(Color.GRAY);
g.drawOval(230, 80, 40, 70);
g.setColor(new Color(30,198,227));
g.setFont(new Font("Arial",Font.PLAIN,50));
g.drawString("Hi there!",160,300);
drawBox(600,200,0,100,15,Color.blue);
drawBox(600,300,0,4,8,Color.gray);
drawBox(600,350,0,1,2.5,Color.black);
drawBox(625,300,0,1,1,Color.cyan);
drawBox(575,300,0,1,1,Color.cyan);
drawBox(625,250,0,1,1,Color.cyan);
drawBox(575,250,0,1,1,Color.cyan);
drawBox(400,300,0,4,13,Color.gray);
drawBox(400,350,0,1,2.5,Color.black);
drawBox(425,300,0,1,1,Color.cyan);
drawBox(375,300,0,1,1,Color.cyan);
drawBox(425,250,0,1,1,Color.cyan);
drawBox(375,250,0,1,1,Color.cyan);
drawBox(425,200,0,1,1,Color.cyan);
drawBox(375,200,0,1,1,Color.cyan);
drawcar(100,100,20,100,65,Color.GREEN);
drawBox(600,450,0,100,10,Color.lightGray);
drawBox(375,400,0,100,3,Color.black);
drawBox(130+polygonMove++,385,0,4,1,Color.red);
drawBox(130+polygonMove++,365,0,2,1,Color.red);
drawCircle(100+polygonMove++,400,0,0.8,0.8,Color.DARK_GRAY);
drawCircle(160+polygonMove++,400,0,0.8,0.8,Color.DARK_GRAY);
drawBox(1130-polygonMove++,368,0,0.8,0.8,Color.cyan);
drawBox(1130-polygonMove++,385,0,4,1,Color.blue);
drawBox(1130-polygonMove++,365,0,2,1,Color.blue);
drawCircle(1100-polygonMove++,400,0,0.8,0.8,Color.DARK_GRAY);
drawCircle(1160-polygonMove++,400,0,0.8,0.8,Color.DARK_GRAY);
drawHouse(100,100,0+polygonRotate++,5,5,Color.yellow);
drawBox(130+polygonMove++,368,0,0.8,0.8,Color.cyan);
drawBox(300,321,0,0.5,3,Color.BLACK);
drawOval(300,275,0,2,4,Color.GREEN);
drawBox(600-+polygonMove++,365,0,10.8,3,Color.yellow);
drawBox(560-+polygonMove++,360,0,1,1,Color.cyan);
drawBox(670-+polygonMove++,360,0,1,1,Color.cyan);
drawBox(610-+polygonMove++,360,0,1,1,Color.cyan);
drawBox(513-+polygonMove++,355,0,2,2,Color.cyan);
drawCircle(700-+polygonMove++,400,0,1,1,Color.DARK_GRAY);
drawCircle(500-+polygonMove++,400,0,1,1,Color.DARK_GRAY);
drawBox(1130-polygonMove++,368,0,0.8,0.8,Color.cyan);
g.setColor(new Color(130,98,117));
g.setFont(new Font("Arial",Font.PLAIN,50));
g.drawString("Work day!",270,150);
Color sunColor = new Color(230,200,0);
g.setColor(sunColor);
g.setColor(Color.yellow);
g.setFont(new Font("Arial",Font.PLAIN,30));
g.drawString("zooom",50,340);
gOld.drawImage(image, 0, 0, null);
}
////////////////////////////////////////////////////////////////////////////
public void drawcar(int xpos,int ypos,double rot,double xscale,double yscale,Color color)
{
g.translate(xpos,ypos);
g.rotate(rot * Math.PI/180.0);
g.scale( xscale , yscale );
g.setColor(color);
drawBox(130,385,0,4,1,Color.red);
drawBox(130,365,0,2,1,Color.red);
drawCircle(100,400,0,0.8,0.8,Color.DARK_GRAY);
drawCircle(160,400,0,0.8,0.8,Color.DARK_GRAY);
drawBox(130,368,0,0.8,0.8,Color.cyan);
g.scale( 1.0/xscale,1.0/yscale );
g.rotate(-rot * Math.PI/180.0);
g.translate(-xpos,-ypos);
}
////////////////////////////////////////////////////////////////////////////
public void drawHouse(int xpos,int ypos,double rot,double xscale,double yscale,Color color)
{
g.translate(xpos,ypos);
g.rotate(rot * Math.PI/180.0);
g.scale( xscale , yscale );
g.setColor(color);
int xvals[] = {5,10,5,10,5,0,-5,-10,-5,-10,-5,0};
int yvals[] = {-5,-5,0,5,5,10,5,5,0,-5,-5,-10};
g.fillPolygon(xvals,yvals,xvals.length);
g.scale( 1.0/xscale,1.0/yscale );
g.rotate(-rot * Math.PI/180.0);
g.translate(-xpos,-ypos);
}
////////////////////////////////////////////////////////////////////////////
public void drawBox(int xpos,int ypos,double rot,double xscale,double yscale,Color color)
{
g.translate(xpos,ypos);
g.rotate(rot * Math.PI/180.0);
g.scale( xscale , yscale );
g.setColor(color);
g.fillRect(-10,-10,20,20);
g.scale( 1.0/xscale,1.0/yscale );
g.rotate(-rot * Math.PI/180.0);
g.translate(-xpos,-ypos);
}
////////////////////////////////////////////////////////////////////////////
// drawCircle(100,100,0,1,1,Color.blue);
public void drawCircle(int xpos,int ypos,double rot,double xscale,double yscale,Color color)
{
g.translate(xpos,ypos);
g.rotate(rot * Math.PI/180.0);
g.scale( xscale , yscale );
g.setColor(color);
g.fillOval(-10,-10,20,20);
g.scale( 1.0/xscale,1.0/yscale );
g.rotate(-rot * Math.PI/180.0);
g.translate(-xpos,-ypos);
}
////////////////////////////////////////////////////////////////////////////
// drawCircle(150,100,0,2,1,Color.blue);
public void drawOval(int xpos,int ypos,double rot,double xscale,double yscale,Color color)
{
g.translate(xpos,ypos);
g.rotate(rot * Math.PI/180.0);
g.scale( xscale , yscale );
g.setColor(color);
g.fillOval(-10,-10,20,20);
g.scale( 1.0/xscale,1.0/yscale );
g.rotate(-rot * Math.PI/180.0);
g.translate(-xpos,-ypos);
}
////////////////////////////////////////////////////////////////////////////
// needed for implement runnable
public void run() {
while (true) {
animate();
repaint();
double seconds = .04; //time that 1 frame takes.
int miliseconds = (int) (1000.0 * seconds);
try {
Thread.sleep(miliseconds);
} catch (InterruptedException e) {
}
}
}
/////////////////////////////////////////////////////////////////////////
public void reset() {
}
/////////////////////////////////////////////////////////////////////////
public void animate() {
if (animateFirstTime) {
animateFirstTime = false;
if (xsize != getSize().width || ysize != getSize().height) {
xsize = getSize().width;
ysize = getSize().height;
}
reset();
}
polygonMove++;
polygonRotate++;
}
////////////////////////////////////////////////////////////////////////////
public void start() {
if (relaxer == null) {
relaxer = new Thread(this);
relaxer.start();
}
}
////////////////////////////////////////////////////////////////////////////
public void stop() {
if (relaxer.isAlive()) {
relaxer.stop();
}
relaxer = null;
}
/////////////////////////////////////////////////////////////////////////
public int getX(int x) {
return (x + XBORDER + WINDOW_BORDER);
}
public int getY(int y) {
return (y + YBORDER + YTITLE );
}
public int getYNormal(int y) {
return (-y + YBORDER + YTITLE + getHeight2());
}
public int getWidth2() {
return (xsize - 2 * (XBORDER + WINDOW_BORDER));
}
public int getHeight2() {
return (ysize - 2 * YBORDER - WINDOW_BORDER - YTITLE);
}
}