📄 carshape.java
字号:
package com.bemjh.Shape;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.*;
import java.awt.geom.*;
/**
* <p>Title: MultiMoveableShape</p>
*
* <p>Description: MultiMoveable Shape</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: bemjh</p>
*
* @author bemjh
* @version 1.0
*/
public class CarShape implements IMoveable {
// private variable
//the object's coordinate
private int x, y;
// the object 's size
private int size;
public CarShape() {
}
/**
* the construct function
* @param x the object's x coordinate
* @param y the object's y coordinate
* @param size the size of the object
*/
public CarShape(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
/**
* this function is to draw a graph
*
* @param g graphics object
* @todo Implement this com.bemjh.Shape.IMoveable method
*/
public void draw(Graphics g) {
// g.fillOval(x,y,x+size,y+size);
Graphics2D g2 =(Graphics2D)g;
Rectangle2D.Double body = new Rectangle2D.Double(x,y+size/6,size-1,size/6);
Ellipse2D.Double frontTire = new Ellipse2D.Double(x+size/6,y+size/3,size/6,size/6);
Ellipse2D.Double rearTire = new Ellipse2D.Double(x+size*2/3,y+size/3,size/6,size/6);
Point2D.Double r1= new Point2D.Double(x+size/6,y+size/6);
Point2D.Double r2= new Point2D.Double(x+size/3,y);
Point2D.Double r3= new Point2D.Double(x+size*2/3,y);
Point2D.Double r4= new Point2D.Double(x+size*5/6,y+size/6);
Line2D.Double frontWindshield = new Line2D.Double(r1,r2);
Line2D.Double roofTop = new Line2D.Double(r2,r3);
Line2D.Double rearWindShield = new Line2D.Double(r3,r4);
g2.fill(frontTire);
g2.fill(rearTire);
g2.setColor(Color.red);
g2.fill(body);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindShield);
}
/**
* the function traslate is to increase the x and y of the object
*
* @param dx x direction to crease
* @param dy y direction to crease
* @todo Implement this com.bemjh.Shape.IMoveable method
*/
public void translate(double dx, double dy) {
if(x>350)
x=0;
if(y>200)
y=0;
x+=dx;
y+=dy;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -