// TCubicCurve.java import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Vector; public class TCubicCurve extends JFrame { DrawingCanvas canvas; JLabel label, coords; public TCubicCurve() { // 1. Assign a name and get the content pane super("TCubicCurve"); Container container = getContentPane(); // 2. Create a display panel JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1,2)); // 3. Add the display labels label = new JLabel("Mouse Location (x, y): ", JLabel.RIGHT); panel.add(label); panel.add(label); coords = new JLabel(""); panel.add(coords); // 4. Add the panel to the container container.add(panel, BorderLayout.SOUTH); // 5. Add the drawing canvas canvas = new DrawingCanvas(); container.add(canvas); // 6. Add the window closing listener and display the // frame with proper size addWindowListener(new WindowEventHandler()); pack(); show(); } // 7. Definition of window listener class class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // 8. The main method... public static void main(String arg[]) { new TCubicCurve(); } // 9. Definition of Drawing Canvas class DrawingCanvas extends Canvas { // Coordinates to draw a straight line, quadratic curve, // and a cubic curve float x1,y1, xc1cur,yc1cur, xc1new, yc1new, xc2cur,yc2cur, xc2new, yc2new, x4cur,y4cur, x4new, y4new; // Some useful flags int pressNo = 0; int dragFlag1 = -1; int dragFlag2 = -1; boolean clearFlag = false; // For creating a dashed stroke float dashes[] = {5f, 5f}; BasicStroke stroke; // 10. Constructor public DrawingCanvas() { setBackground(Color.white); addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseMotionListener()); setSize(400, 400); stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10f, dashes, 0f); } // 11. The overriding update() method public void update(Graphics g) { paint(g); } // 12. The overriding paint method public void paint(Graphics g) { // Set up the graphics context Graphics2D g2D = (Graphics2D) g; // 13. Interactively draw the straight line if (pressNo == 1) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); g2D.setStroke(stroke); // Erase the currently existing line g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur)); // Draw the new line g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values x4cur = x4new; y4cur = y4new; } // 14. Interactively draw the quadratic curve else if (pressNo == 2) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); g2D.setStroke(stroke); if (dragFlag1 != -1) { // Erase the previously existing quadratic curve g2D.draw( new QuadCurve2D.Float(x1,y1, xc1cur,yc1cur, x4new,y4new)); } dragFlag1++; // Reset the drag-flag // Draw the new quadratic curve g2D.draw( new QuadCurve2D.Float(x1,y1, xc1new,yc1new, x4new,y4new)); // Update the coordinate values xc1cur = xc1new; yc1cur = yc1new; } // 15. Interactively draw the cubic curve else if (pressNo == 3) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); if (dragFlag2 != -1) { // Erase the currently existing curve g2D.draw( new CubicCurve2D.Float(x1,y1, xc1new,yc1new, xc2cur,yc2cur, x4new,y4new)); } dragFlag2++; // Reset the drag flag // Draw the new curve g2D.draw( new CubicCurve2D.Float(x1,y1, xc1new,yc1new, xc2new,yc2new, x4new,y4new)); // Update the current coordinate values xc2cur = xc2new; yc2cur = yc2new; } // 16. Clear the guiding straight and quadratic lines if (clearFlag) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); g2D.setStroke(stroke); g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); g2D.draw( new QuadCurve2D.Float(x1,y1, xc1new,yc1new, x4new,y4new)); // Reset the flag clearFlag = false; } } // 17. Definition of the mouse listener class MyMouseListener extends MouseAdapter { // 18. When a mouse button is pressed... public void mousePressed(MouseEvent e) { if (pressNo == 0) { // Note: This is the first press pressNo++; // Update the starting and current coordinates x1 = x4cur = e.getX(); y1 = y4cur = e.getY(); } else if (pressNo == 1) { // Note: This is the second press pressNo++; // Update the current control coordinates-1 xc1cur = e.getX(); yc1cur = e.getY(); } else if (pressNo == 2) { // Note: This is the third press pressNo++; // Update the current control coordinates-2 xc2cur = e.getX(); yc2cur = e.getY(); } } // 19. When the mouse is released... public void mouseReleased(MouseEvent e) { if (pressNo == 1) { x4new = e.getX(); y4new = e.getY(); canvas.repaint(); } else if (pressNo == 2) { xc1new = e.getX(); yc1new = e.getY(); canvas.repaint(); } else if (pressNo == 3) { xc2new = e.getX(); yc2new = e.getY(); canvas.repaint(); // Reset the flags after the drawing a curve pressNo = 0; dragFlag1 = -1; dragFlag2 = -1; clearFlag = true; } } } // 20. Definition of mouse motion listener class MyMouseMotionListener extends MouseMotionAdapter { // 21. When the mouse is dragged... public void mouseDragged(MouseEvent e) { if (pressNo == 1) { // Update for the new values of coordinates of the // ending point of a straight line. x4new = e.getX(); y4new = e.getY(); // Display the location coordinates of the mouse String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); // Call the repaint() on canvas canvas.repaint(); } else if (pressNo == 2) { // Update for the control point-1 of the quadratic // curve. This curve uses the starting and ending points // as those of the straight line. xc1new = e.getX(); yc1new = e.getY(); // Display the location coordinates of the mouse String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); // Call the repaint() on canvas canvas.repaint(); } else if (pressNo == 3) { // Update for the control point-2 of the cubic curve; // The starting and ending points, and the control // point-2 are the same as that of the quadratic curve. xc2new = e.getX(); yc2new = e.getY(); // Display the location coordinates of the mouse String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); // Call the repaint() on canvas canvas.repaint(); } } // 22. When the mouse is moved... public void mouseMoved(MouseEvent e) { // Display the coordinates of the mouse pointer String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); } } } }