📄 element.java
字号:
import java.awt.Color;
import java.awt.Shape;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.io.*;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Attr;
public abstract class Element implements Serializable {
public Element(Color color) {
this.color = color;
}
// Constructor to initialize element fields from a DOM XML Element object
protected Element(org.w3c.dom.Element xmlElement) {
// Get the <color> element
org.w3c.dom.NodeList list = xmlElement.getElementsByTagName("color");
setElementColor((org.w3c.dom.Element)list.item(0)); // Set the color
list = xmlElement.getElementsByTagName("position"); // Get <position>
setElementPosition((org.w3c.dom.Element)list.item(0)); // Set the position
angle = Double.parseDouble(xmlElement.getAttribute("angle")); // Set the angle
}
public Color getColor() {
return color;
}
// Set or reset highlight color
public void setHighlighted(boolean highlighted) {
this.highlighted = highlighted;
}
public Point getPosition() {
return position;
}
protected void draw(Graphics2D g2D, Shape element) {
g2D.setPaint(highlighted ? Color.MAGENTA : color); // Set the element color
AffineTransform old = g2D.getTransform(); // Save the current transform
g2D.translate(position.x, position.y); // Translate to position
g2D.rotate(angle); // Rotate about position
g2D.draw(element); // Draw the element
g2D.setTransform(old); // Restore original transform
}
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 void move(int deltax, int deltay) {
position.x += deltax;
position.y += deltay;
}
public void rotate(double angle) {
this.angle += angle;
}
protected org.w3c.dom.Element createColorElement(Document doc) {
org.w3c.dom.Element colorElement = doc.createElement("color");
Attr attr = doc.createAttribute("R");
attr.setValue(String.valueOf(color.getRed()));
colorElement.setAttributeNode(attr);
attr = doc.createAttribute("G");
attr.setValue(String.valueOf(color.getGreen()));
colorElement.setAttributeNode(attr);
attr = doc.createAttribute("B");
attr.setValue(String.valueOf(color.getBlue()));
colorElement.setAttributeNode(attr);
return colorElement;
}
protected org.w3c.dom.Element createPointTypeElement(Document doc,
String name,
String xValue,
String yValue) {
org.w3c.dom.Element element = doc.createElement(name);
Attr attr = doc.createAttribute("x"); // Create attribute x
attr.setValue(xValue); // and set its value
element.setAttributeNode(attr); // Insert the x attribute
attr = doc.createAttribute("y"); // Create attribute y
attr.setValue(yValue); // and set its value
element.setAttributeNode(attr); // Insert the y attribute
return element;
}
protected org.w3c.dom.Element createPositionElement(Document doc) {
return createPointTypeElement(doc, "position",
String.valueOf(position.getX()),
String.valueOf(position.getY()));
}
protected void setElementColor(org.w3c.dom.Element colorElement) {
color = new Color(Integer.parseInt(colorElement.getAttribute("R")),
Integer.parseInt(colorElement.getAttribute("G")),
Integer.parseInt(colorElement.getAttribute("B")));
}
protected void setElementPosition(org.w3c.dom.Element posElement) {
position = new Point();
position.setLocation(Double.parseDouble(posElement.getAttribute("x")),
Double.parseDouble(posElement.getAttribute("y")));
}
public abstract java.awt.Rectangle getBounds();
public abstract void modify(Point start, Point last);
public abstract void draw(Graphics2D g2D);
public abstract void addElementNode(Document document);
protected Color color; // Color of a shape
protected boolean highlighted = false; // Highlight flag
final static Point origin = new Point(); // Point 0,0
protected Point position; // Element position
protected double angle = 0.0; // Rotation angle
// Nested class defining a line
public static class Line extends Element {
public Line(Point start, Point end, Color color) {
super(color);
position = start;
line = new Line2D.Double(origin, new Point(end.x - position.x,
end.y - position.y));
}
// Create a Element.Line object from a DOM Element
// Content is <color>, <position>, <endpoint> elements. Attribute is angle.
public Line(org.w3c.dom.Element xmlElement) {
super(xmlElement);
org.w3c.dom.NodeList list = xmlElement.getElementsByTagName("endpoint");
org.w3c.dom.Element endpoint = (org.w3c.dom.Element)list.item(0);
line = new Line2D.Double(origin.x, origin.y,
Double.parseDouble(endpoint.getAttribute("x"))-position.getX(),
Double.parseDouble(endpoint.getAttribute("y"))-position.getY());
}
public java.awt.Rectangle getBounds() {
return getBounds(line.getBounds());
}
public void modify(Point start, Point last) {
line.x2 = last.x - position.x;
line.y2 = last.y - position.y;
}
public void draw(Graphics2D g2D) {
draw(g2D, line); // Call base draw method
}
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 void addElementNode(Document doc) {
org.w3c.dom.Element lineElement = doc.createElement("line");
// Create the angle attribute and attach it to the <line> node
Attr attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
lineElement.setAttributeNode(attr);
// Append the <color>, <position>, and <endpoint> nodes as children
lineElement.appendChild(createColorElement(doc));
lineElement.appendChild(createPositionElement(doc));
lineElement.appendChild(createEndpointElement(doc));
// Append the <line> node to the document root node
doc.getDocumentElement().appendChild(lineElement);
}
private org.w3c.dom.Element createEndpointElement(Document doc) {
return createPointTypeElement(doc, "endpoint",
String.valueOf(line.x2+position.x),
String.valueOf(line.y2+position.y));
}
private Line2D.Double line;
}
// Nested class defining a rectangle
public static class Rectangle extends Element {
public Rectangle(Point start, Point end, Color color) {
super(color);
position = new Point(Math.min(start.x, end.x),
Math.min(start.y, end.y));
rectangle = new Rectangle2D.Double(origin.x,
origin.y,
Math.abs(start.x - end.x), // Width
Math.abs(start.y - end.y)); // & height
}
// Create a Element.Rectangle object from a DOM Element
// Rectangle has angle attribute. Content is <color>,<position>,<bottomright>
public Rectangle(org.w3c.dom.Element xmlElement) {
super(xmlElement);
org.w3c.dom.NodeList list = xmlElement.getElementsByTagName("bottomright");
org.w3c.dom.Element bottomright = (org.w3c.dom.Element)list.item(0);
rectangle = new Rectangle2D.Double(origin.x, origin.y,
Double.parseDouble(bottomright.getAttribute("x"))-position.getX(),
Double.parseDouble(bottomright.getAttribute("y"))-position.getY());
}
public java.awt.Rectangle getBounds() {
return getBounds(rectangle.getBounds());
}
public void modify(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);
}
public void draw(Graphics2D g2D) {
draw(g2D, rectangle); // Call base draw method
}
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);
}
public void addElementNode(Document doc) {
org.w3c.dom.Element rectElement = doc.createElement("rectangle");
// Create the angle attribute and attach it to the <rectangle> node
Attr attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
rectElement.setAttributeNode(attr);
// Append the <color>, <position>, and <bottomright> nodes as children
rectElement.appendChild(createColorElement(doc));
rectElement.appendChild(createPositionElement(doc));
rectElement.appendChild(createBottomrightElement(doc));
doc.getDocumentElement().appendChild(rectElement);
}
private org.w3c.dom.Element createBottomrightElement(Document doc) {
return createPointTypeElement(doc, "bottomright",
String.valueOf(rectangle.width+position.x),
String.valueOf(rectangle.height+position.y));
}
private Rectangle2D.Double rectangle;
}
// Nested class defining a circle
public static class Circle extends Element {
public Circle(Point center, Point circum, Color color) {
super(color);
// Radius is distance from center to circumference
double radius = center.distance(circum);
position = new Point(center.x - (int)radius,
center.y - (int)radius);
circle = new Ellipse2D.Double(origin.x, origin.y, // Position - top-left
2.*radius, 2.*radius ); // Width & height
}
// Create a Element.Circle object from a DOM Element
// Circle has radius, angle attributes. Content is <color>, <position>
public Circle(org.w3c.dom.Element xmlElement) {
super(xmlElement);
double radius = Double.parseDouble(xmlElement.getAttribute("radius"));
circle = new Ellipse2D.Double(origin.x, origin.y, // Position - top-left
2.*radius, 2.*radius ); // Width & height
}
public java.awt.Rectangle getBounds() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -