// TQuadCurve.java /* * * */ import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Vector; public class TQuadCurve extends JApplet { DrawingCanvas canvas; JLabel curveParamValues, curveFlatValue; public void init() { // 1. Get the content pane Container container = getContentPane(); // 2. Create a display panel with titled border JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); TitledBorder border = new TitledBorder( "Select a Curve To Display Its Points and Flatness Index"); panel.setBorder(border); // 3. Add the display labels to display curve points and // flatness index. JLabel curveParam = new JLabel("Curve Points(p1, pc, p2): "); panel.add(curveParam); curveParamValues = new JLabel(""); curveParamValues.setOpaque(true); curveParamValues.setBackground(Color.white); curveParamValues.setForeground(Color.black); panel.add(curveParamValues); JLabel curveFlat = new JLabel("Curve Flatness (index): "); panel.add(curveFlat); curveFlatValue = new JLabel(""); curveFlatValue.setOpaque(true); curveFlatValue.setBackground(Color.white); curveFlatValue.setForeground(Color.black); panel.add(curveFlatValue); // 4. Add the panel to the applet container.add(panel, BorderLayout.SOUTH); // 5. Add the drawing canvas to the applet canvas = new DrawingCanvas(); container.add(canvas); } // 6. Definition of 'DrawingCanvas' class class DrawingCanvas extends Canvas { Vector quadCurves; QuadCurve2D selectedCurve = null; Rectangle2D boundingRec = null; // 7. Constructor public DrawingCanvas() { setBackground(Color.white); setSize(400,200); // width and height of canvas addMouseListener(new MyMouseListener()); // 8. Store the quadratic curves in a vector. Parameters // of the constructors are in the order P1(x1, y1), // P2(x2, y2) and P3(x3, y3). quadCurves = new Vector(); quadCurves.addElement( new QuadCurve2D.Float(20,20, 80,160, 120,20)); quadCurves.addElement( new QuadCurve2D.Float(120,100, 160,40, 200,180)); quadCurves.addElement( new QuadCurve2D.Float(240,20, 220,60, 260,120)); quadCurves.addElement( new QuadCurve2D.Float(250,160, 260,140, 280,180)); quadCurves.addElement( new QuadCurve2D.Float(300,180, 340,40, 380,120)); quadCurves.addElement( new QuadCurve2D.Float(20,180, 80,170, 120,190)); } // 9. The Overriding paint method public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; // 10. Display quadratic curves from the vector for (int i=0; i