📄 element.java
字号:
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
public abstract class Element implements Serializable{
public Element(Color color){
this.color=color;
}
public Color getColor(){
return color;
}
public Point getPosition(){
return position;
}
public void setHightLight(boolean hightLight){
this.hightLight=hightLight;
}
public void draw(Graphics2D g2d,Shape element){
if(element==null) System.out.println("a");
g2d.setPaint(hightLight?Color.magenta:color);
AffineTransform old=g2d.getTransform();
g2d.translate(position.x,position.y);
g2d.rotate(angle);
g2d.draw(element);
g2d.setTransform(old);
}
public void move(int x,int y){
position.x+=x;
position.y+=y;
}
public void rotate(double angle){
this.angle+=angle;
}
protected java.awt.Rectangle getBounds(java.awt.Rectangle bounds){
AffineTransform at=AffineTransform.getTranslateInstance(position.x,position.y);
at.rotate(angle);
return at.createTransformedShape(bounds).getBounds();
}
public static class Line extends Element {
public Line(Point start,Point last,Color color){
super(color);
position=start;
line=new Line2D.Double(origin,new Point(last.x-position.x,last.y-position.y));
}
public void draw(Graphics2D g2d){
draw(g2d,line);
}
public java.awt.Rectangle getBounds( ){
return getBounds(line.getBounds());
}
public void nodify(Point start,Point last){
line.x2=last.x-position.x;
line.y2=last.y-position.y;
}
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeDouble(line.x2);
out.writeDouble(line.y2);
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
double x2=in.readDouble();
double y2=in.readDouble();
line =new Line2D.Double(0,0,x2,y2);
}
transient private Line2D.Double line;
}
public static class Rectangle extends Element{
public Rectangle(Point start,Point last,Color color){
super(color);
position =new Point(Math.min(start.x,last.x),Math.min(start.y,last.y));
rectangle=new Rectangle2D.Double(origin.x,origin.y,Math.abs(start.x-last.x),Math.abs(start.y-last.y));
}
public void draw(Graphics2D g2d){
draw(g2d,rectangle);
}
public java.awt.Rectangle getBounds(){
return getBounds(rectangle.getBounds());
}
public void nodify(Point start,Point last){
position.x=Math.min(start.x,last.x);
position.y=Math.min(start.y,last.y);
rectangle.width=Math.abs(start.x-last.x);
rectangle.height=Math.abs(start.y-last.y);
}
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeDouble(rectangle.width);
out.writeDouble(rectangle.height);
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
double width=in.readDouble();
double height=in.readDouble();
rectangle =new Rectangle2D.Double(0,0,width,height);
}
transient private Rectangle2D.Double rectangle;
}
public static class Ellipse extends Element{
public Ellipse(Point start,Point last,Color color){
super(color);
position =new Point(Math.min(start.x,last.x),Math.min(start.y,last.y));
ellipse=new Ellipse2D.Double(origin.x,origin.y,Math.abs(start.x-last.x),Math.abs(start.y-last.y));
}
public void draw(Graphics2D g2d){
draw(g2d,ellipse);
}
public java.awt.Rectangle getBounds(){
return getBounds(ellipse.getBounds());
}
public void nodify(Point start,Point last){
position.x=Math.min(start.x,last.x);
position.y=Math.min(start.y,last.y);
ellipse.width=Math.abs(start.x-last.x);
ellipse.height=Math.abs(start.y-last.y);
}
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeDouble(ellipse.width);
out.writeDouble(ellipse.height);
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
double width=in.readDouble();
double height=in.readDouble();
ellipse =new Ellipse2D.Double(0,0,width,height);
}
transient private Ellipse2D.Double ellipse;
}
public static class Circle extends Element{
public Circle(Point center,Point circum,Color color){
super(color);
double radius=center.distance(circum);
position =new Point(center.x-(int)radius,center.y-(int)radius);
circle=new Ellipse2D.Double(origin.x,origin.y ,2*radius,2*radius);
}
public void draw(Graphics2D g2d){
draw(g2d,circle);
}
public java.awt.Rectangle getBounds(){
return getBounds(circle.getBounds());
}
public void nodify(Point center,Point circum){
double radius=center.distance(circum);
position.x=center.x-(int)radius;
position.y=center.y-(int)radius;
circle.width=circle.height=2*radius;
}
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeDouble(circle.width);
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
double width=in.readDouble();
circle =new Ellipse2D.Double(0,0,width,width);
}
transient private Ellipse2D.Double circle;
}
public static class Curve extends Element{
public Curve(Point center,Point circum,Color color){
super(color);
curve=new GeneralPath();
position=center;
curve.moveTo(origin.x,origin.y);
curve.lineTo(circum.x-position.x,circum.y-position.y);
}
public void draw(Graphics2D g2d){
draw(g2d,curve);
}
public java.awt.Rectangle getBounds(){
return getBounds(curve.getBounds());
}
public void nodify(Point center,Point circum){
curve.lineTo(circum.x-position.x,circum.y-position.y);
}
private void writeObject(ObjectOutputStream out) throws IOException{
PathIterator iterator=curve.getPathIterator(new AffineTransform());
Vector coords=new Vector();
int maxCount=6;
float[] temp=new float[maxCount];
int result =iterator.currentSegment(temp);
if(!(result==iterator.SEG_MOVETO))
throw new IOException("No ");
iterator.next();
while(!iterator.isDone()){
result=iterator.currentSegment(temp);
if(!(result==iterator.SEG_LINETO))
throw new IOException("No2 ");
coords.add(new Float(temp[1]));
iterator.next();
}
out.writeObject(coords);
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
Vector coords=(Vector)in.readObject();
curve=new GeneralPath();
curve.moveTo(0,0);
float x,y;
for(int i=0;i<coords.size();i+=2){
x=((Float)coords.get(i)).floatValue();
y=((Float)coords.get(i+1)).floatValue();
curve.lineTo(x,y);
}
}
transient private GeneralPath curve;
}
public static class Text extends Element{
public Text(Font font,String text,Point position1,Color color,java.awt.Rectangle bounds1){
super(color);
this.font=font;
this.text=text;
position=position1;
position.y-=(int)bounds.getHeight();
bounds=new java.awt.Rectangle(origin.x,origin.y,bounds1.width,bounds.height);
bounds.setLocation(position.x,position.y-(int)bounds.getHeight());
}
public void draw(Graphics2D g2d){
g2d.setPaint(hightLight?Color.magenta:color);
Font oldFont=g2d.getFont();
g2d.setFont(font);
AffineTransform old=g2d.getTransform();
g2d.translate(position.x,position.y);
g2d.rotate(angle);
g2d.drawString(text,position.x,position.y);
g2d.setTransform(old);
g2d.setFont(oldFont);
}
public java.awt.Rectangle getBounds(){
return getBounds(bounds);
}
public void nodify(Point start,Point last){
}
private Font font;
private String text;
private java.awt.Rectangle bounds=null;
}
public abstract void draw(Graphics2D g2d);
public abstract java.awt.Rectangle getBounds();
public abstract void nodify(Point start,Point last);
protected Color color;
protected boolean hightLight=false;
final static Point origin=new Point();
protected Point position;
protected double angle=0.0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -