I have a GUI that is designed to let the user draw lines, curves, rectangles, circles, text, and ELLIPSES. I have modified each class so they can be moved, but i cant figure out how to modify the ELLIPSE sub class.
Below i have the Element.java class and its LINE and ELLIPSE sub class (i didnt include all of the sub classes because its too long).
I just need to know what to do to modify the ELLIPSE sub class so i can move the ellipses.
______________
Element.java
______________
import java.awt.Color;
import java.awt.Shape;
import java.awt.Point;
import java.awt.Font;
import java.awt.Graphics2D;
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;
public abstract class Element {
public Element(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
// Set or reset highlight color
public void setHighlighted(boolean highlighted) {
this.highlighted = highlighted;
}
// Get the current position of the element
public Point getPosition() {
return position;
}
// Draw an element in a given graphics context
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.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);
return at.createTransformedShape(bounds).getBounds();
}
// Move an element
public void move(int deltax, int deltay) {
position.x += deltax;
position.y += deltay;
}
public abstract java.awt.Rectangle getBounds();
public abstract void modify(Point start, Point last);
public abstract void draw(Graphics2D g2D);
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
// 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));
}
public void draw(Graphics2D g2D) {
draw(g2D, line); // Call base draw method
}
// Obtain the rectangle bounding the line
public java.awt.Rectangle getBounds() {
return getBounds(line.getBounds());
}
// Change the end point for the line
public void modify(Point start, Point last) {
line.x2 = last.x - position.x;
line.y2 = last.y - position.y;
}
private Line2D.Double line;
}
public static class Ellipse extends Element {
public Ellipse(Point center, Point corner, Color color) {
super(color);
// Width is twice the difference between the x coordinates of center and corner
// Height is twice the difference between the y coordinates
ellipse = new Ellipse2D.Double(center.x<corner.x ? 2*center.x-corner.x : corner.x,
center.y<corner.y ? 2*center.y-corner.y : corner.y,
2.*Math.abs(center.x-corner.x), 2.*Math.abs(center.y-corner.y) );
}
public void draw(Graphics2D g2D) {
draw(g2D, ellipse);
}
public java.awt.Rectangle getBounds() {
return getBounds(ellipse.getBounds());
}
public void modify(Point center, Point corner) {
ellipse.x = center.x<corner.x ? 2*center.x-corner.x : corner.x;
ellipse.y = center.y<corner.y ? 2*center.y-corner.y : corner.y;
ellipse.width = 2.*Math.abs(center.x-corner.x);
ellipse.height = 2.*Math.abs(center.y-corner.y);
}
private Ellipse2D.Double ellipse;
}
}
----------------------------------------------------------------
IronChef -
http://www.freewebs.com/cool_recipes