// TEllipse.java import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class TEllipse extends JFrame { DrawingCanvas canvas; JLabel location; public TEllipse() { // 1. Assign a title and get a reference to the content // pane of the swing frame super("TEllipse"); Container container = getContentPane(); // 2. Add the drawing canvas canvas = new DrawingCanvas(); container.add(canvas); // 3. Create a display panel with titled border JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1,2)); panel.add(new JLabel("Location (x0,y0): ", JLabel.RIGHT)); location = new JLabel(""); panel.add(location); TitledBorder border = new TitledBorder( "Select the Ellipse to Move It in the Canvas"); panel.setBorder(border); // 4. Add the panel to the container container.add(panel, BorderLayout.SOUTH); // 5. Add a window listener to close the frame and display it // with a suitable packing size for the contents. addWindowListener(new WindowEventHandler()); pack(); show(); } // 6. Definition of the window listener class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // 7. The main method... public static void main(String arg[]) { new TEllipse(); } // 8. Definition of the drawing canvas class class DrawingCanvas extends Canvas { double x,y, w,h; // parameters of ellipse int x1,y1, x2,y2; Ellipse2D ellipse; Ellipse2D selectedShape; Rectangle2D boundingRec; Cursor curCursor; // 9. Constructor public DrawingCanvas() { // Values for the ellipse parameters x = 20; y = 20; w = 100; h = 75; setBackground(Color.white); addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseMotionListener()); setSize(400, 300); // canvas width and height } // 10. The overriding paint method public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; // Draw the ellipse ellipse = new Ellipse2D.Double(x,y, w,h); g2D.draw(ellipse); // When the ellipse is selected using mouse, hightlight it. if (boundingRec != null) { drawHighlightSquares(g2D, boundingRec); } // Change the mouse cursor if necessary if (curCursor != null) setCursor(curCursor); } // 11. Method to draw highlight squares public void drawHighlightSquares(Graphics2D g2D, Rectangle2D r) { double x = r.getX(); double y = r.getY(); double w = r.getWidth(); double h = r.getHeight(); g2D.setColor(Color.black); g2D.fill(new Rectangle.Double(x-3.0, y-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w*0.5-3.0, y-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w-3.0, y-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x-3.0, y+h*0.5-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w-3.0, y+h*0.5-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x-3.0, y+h-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w*0.5-3.0, y+h-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w-3.0, y+h-3.0, 6.0, 6.0)); } // 12. Definition of the mouse listener class class MyMouseListener extends MouseAdapter { // When the mouse button is pressed public void mousePressed(MouseEvent e) { // Check if the mouse pointer is inside the ellipse if (ellipse.contains(e.getX(), e.getY())) { //Initialize the selection status and bounding rectangle selectedShape = ellipse; if (boundingRec != null) boundingRec = ellipse.getBounds2D(); // To display the location and size of ellipse displayParameters(selectedShape); } else { // If the mouse pointer is not over the ellipse boundingRec = null; location.setText(""); } // Update the canvas canvas.repaint(); // Store the mouse coordinates x1 = e.getX(); y1 = e.getY(); } // When the mouse button is released public void mouseReleased(MouseEvent e) { // Test if the mouse pointer is inside the ellipse if (ellipse.contains(e.getX(), e.getY())) { boundingRec = ellipse.getBounds2D(); selectedShape = ellipse; // To display the location and size of ellipse displayParameters(selectedShape); } // Update the canvas canvas.repaint(); } // When the mouse button is clicked public void mouseClicked(MouseEvent e) { // Test if the mouse pointer is inside the ellipse if (ellipse.contains(e.getX(), e.getY())) { selectedShape = ellipse; // Need to draw the bounding box when the mouse // button is clicked boundingRec = ellipse.getBounds2D(); // To display the location and size of ellipse displayParameters(selectedShape); } else { if (boundingRec != null) boundingRec = null; location.setText(""); } // Update the canvas canvas.repaint(); } } // 13. The mouse motion listener class MyMouseMotionListener extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { // Test if the mouse pointer is inside the ellipse if (ellipse.contains(e.getX(),e.getY())) { boundingRec = null; selectedShape = ellipse; // Retrieve the mouse coordinates x2 = e.getX(); y2 = e.getY(); // Update the values for the location of the // ellipse by adding/subtracting the difference // in the mouse movement. x = x + x2 - x1; y = y + y2 - y1; // Store the latest mouse location as (x1, y1). x1 = x2; y1 = y2; } if (selectedShape != null) displayParameters(selectedShape); // Update the canvas canvas.repaint(); } // When the mouse is moved, this method controls the // type of mouse cursor. public void mouseMoved(MouseEvent e) { if (ellipse != null) { // Averts a null pointer in // event dispatch thread if (ellipse.contains(e.getX(),e.getY())) { // Set the cursor to the hand type. curCursor = Cursor.getPredefinedCursor( Cursor.HAND_CURSOR); } else { // Set the cursor to the default one. curCursor = Cursor.getDefaultCursor(); } } canvas.repaint(); } } // 14. To display the instantaneous location coordinates while // dragging the ellipse. public void displayParameters(Shape shape) { double x = selectedShape.getX(); double y = selectedShape.getY(); double w = selectedShape.getWidth(); double h = selectedShape.getHeight(); String locString = "(" + Double.toString(x) + "," + Double.toString(y) + ")"; String sizeString = "(" + Double.toString(w) + "," + Double.toString(h) + ")"; location.setText(locString); } } }