Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 2nd, 2006, 06:20 PM
Authorized User
Join Date: Jul 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ironchef
Default Moving an ellipse

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.



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,
    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) {
      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) {

      // 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
<b>IronChef</b> - http://www.freewebs.com/cool_recipes

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i draw an ellipse jerryham VB.NET 10 September 23rd, 2008 03:10 PM
Using a Ellipse Button in datagridview hossrad C# 2005 0 May 6th, 2008 07:18 AM
Moving viewport schoolBoy Javascript How-To 7 March 7th, 2007 05:03 PM
Moving through a Do Loop mmcdonal VBScript 1 April 8th, 2005 02:10 PM
moving a column p_gauri7 Crystal Reports 6 December 20th, 2004 03:27 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.