📄 elements.java
字号:
package boya;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
abstract class Elements implements Serializable{
protected Color color;
public Elements(Color color){
this.color=color;
}
public Color getColor(){
return color;
}
public abstract java.awt.Rectangle getBounds();
public abstract void moving(Point start,Point end);
public abstract void draw(Graphics2D g2D);
public static class Line extends Elements{
private Line2D.Double line;
public Line(Point start,Point end,Color color){
super(color);
line=new Line2D.Double(start,end);
}
public java.awt.Rectangle getBounds(){
return line.getBounds();
}
public void moving(Point start,Point end){
line.x2=end.x;
line.y2=end.y;
}
public void draw(Graphics2D g2D){
g2D.setPaint(color);
g2D.draw(line);
}
private void writeObject(ObjectOutputStream out)
throws IOException{
out.writeDouble(line.x2);
out.writeDouble(line.y2);
}
private void readObject(java.io.ObjectInputStream in)
throws IOException,ClassNotFoundException{
double x2=in.readDouble();
double y2=in.readDouble();
line=new Line2D.Double(0,0,x2,y2);
}
}
public static class Rectangle extends Elements{
private Rectangle2D.Double rectangle;
public Rectangle(Point start,Point end,Color color){
super(color);
rectangle=new Rectangle2D.Double(
Math.min(start.x,end.x),Math.min(start.y,end.y),
Math.abs(start.x-end.x),Math.abs(start.y-end.y));
}
public java.awt.Rectangle getBounds(){
return rectangle.getBounds();
}
public void moving(Point start,Point end){
rectangle.x=Math.min(start.x,end.x);
rectangle.y=Math.min(start.y,end.y);
rectangle.width=Math.abs(start.x-end.x);
rectangle.height=Math.abs(start.y-end.y);
}
public void draw(Graphics2D g2D){
g2D.setPaint(color);
g2D.draw(rectangle);
}
private void writeObject(ObjectOutputStream out)
throws IOException{
out.writeDouble(rectangle.width);
out.writeDouble(rectangle.height);
}
private void readObject(java.io.ObjectInputStream in)
throws IOException,ClassNotFoundException{
double width=in.readDouble();
double height=in.readDouble();
rectangle=new Rectangle2D.Double(0,0,width,height);
}
}
public static class Circle extends Elements{
private Ellipse2D.Double circle;
public Circle(Point center,Point circum,Color color){
super(color);
double radius=center.distance(circum);
circle=new Ellipse2D.Double(center.x-radius,center.y-radius,2*radius,2*radius);
}
public java.awt.Rectangle getBounds(){
return circle.getBounds();
}
public void moving(Point center,Point circum){
double radius=center.distance(circum);
circle.x=center.x-(int)radius;
circle.y=center.y-(int)radius;
circle.width=circle.height=2*radius;
}
public void draw(Graphics2D g2D){
g2D.setPaint(color);
g2D.draw(circle);
}
private void writeObject(ObjectOutputStream out)
throws IOException{
out.writeDouble(circle.width);
}
private void readObject(java.io.ObjectInputStream in)
throws IOException,ClassNotFoundException{
double width=in.readDouble();
circle=new Ellipse2D.Double(0,0,width,width);
}
}
public static class Curve extends Elements{
private GeneralPath curve;
public Curve(Point start,Point next,Color color){
super(color);
curve=new GeneralPath();
curve.moveTo(start.x,start.y);
curve.lineTo(next.x,next.y);
}
public java.awt.Rectangle getBounds(){
return curve.getBounds();
}
public void moving(Point start,Point next){
curve.lineTo(next.x,next.y);
}
public void draw(Graphics2D g2D){
g2D.setPaint(color);
g2D.draw(curve);
}
private void writeObject(ObjectOutputStream out)
throws IOException{
PathIterator iterator=curve.getPathIterator(new AffineTransform());
Vector coords=new Vector();
int max=6;
float[] temp=new float[max];
int result=iterator.currentSegment(temp);
if(!(result==iterator.SEG_MOVETO)){
System.out.println("No Starting moveto");
return;
}
iterator.next();
while(!iterator.isDone()){
result=iterator.currentSegment(temp);
if(!(result==iterator.SEG_LINETO)){
System.out.println("Invalid segment type");
return;
}
coords.add(new Float(temp[0]));
coords.add(new Float(temp[1]));
iterator.next();
}
out.writeObject(coords);
}
private void readObject(java.io.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);
}
}
}
public static class Text extends Elements{
private Font font;
private String text;
private Point pos;
private java.awt.Rectangle bounds=null;
public Text(Font font,String text,Point pos,Color color,java.awt.Rectangle bounds){
super(color);
this.font=font;
this.text=text;
this.pos=pos;
this.bounds=bounds;
this.bounds.setLocation(pos.x,pos.y-(int)bounds.getHeight());
}
public java.awt.Rectangle getBounds(){
return bounds;
}
public void moving(Point start,Point end){
}
public void draw(Graphics2D g2D){
g2D.setPaint(color);
Font oldFont=g2D.getFont();
g2D.setFont(font);
g2D.drawString(text,pos.x,pos.y);
g2D.setFont(oldFont);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -