/* * 1.1 version. */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.util.*; //import Point2D; //import SubdivisionCurve2D; /* * This displays a framed area. When the user clicks within * the area, this program displays a dot and a string indicating * the coordinates where the click occurred. */ public class Main extends Applet implements ActionListener, AdjustmentListener, ItemListener { FramedArea framedArea; Label statusLabel, levelLabel; Button clearButton, addButton; Scrollbar levelSlider; Checkbox cbDelete, cbApprox, cbInterp; CheckboxGroup cbgType = new CheckboxGroup(); boolean bDeletePoints = false; int iLevel = 0; int iType = SubdivisionCurve2D.APPROXIMATING; public Main() { super(); this.setBackground(Color.lightGray); } public void init() // Called by browser { GridBagLayout gbLayout = new GridBagLayout(); GridBagConstraints gbConstraints = new GridBagConstraints(); setLayout(gbLayout); ////////////////////////////////////////////////////////////////// gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbConstraints.weightx = 1.0; gbConstraints.weighty = 0.0; Label label0 = new Label(" "); gbLayout.setConstraints(label0, gbConstraints); add(label0); Label label1 = new Label(" "); gbLayout.setConstraints(label1, gbConstraints); add(label1); Label label2 = new Label(" "); gbLayout.setConstraints(label2, gbConstraints); add(label2); Label label3 = new Label(" "); gbLayout.setConstraints(label3, gbConstraints); add(label3); Label label4 = new Label(" "); gbLayout.setConstraints(label4, gbConstraints); add(label4); Label label5 = new Label(" "); gbLayout.setConstraints(label5, gbConstraints); add(label5); Label label6 = new Label(" "); gbLayout.setConstraints(label6, gbConstraints); add(label6); Label label7 = new Label(" "); gbLayout.setConstraints(label7, gbConstraints); add(label7); Label label8 = new Label(" "); gbLayout.setConstraints(label8, gbConstraints); add(label8); Label label9 = new Label(" "); gbLayout.setConstraints(label9, gbConstraints); add(label9); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row Label labelEnd = new Label(" "); gbLayout.setConstraints(labelEnd, gbConstraints); add(labelEnd); ////////////////////////////////////////////////////////////////// statusLabel = new Label("Click within the framed area."); gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbConstraints.gridwidth = 9; gbLayout.setConstraints(statusLabel, gbConstraints); add(statusLabel); gbConstraints.gridwidth = 1; clearButton = new Button( "clear" ); clearButton.addActionListener(this); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row gbLayout.setConstraints(clearButton, gbConstraints); add(clearButton); framedArea = new FramedArea(this); gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.weighty = 1.0; gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row gbLayout.setConstraints(framedArea, gbConstraints); add(framedArea); addButton = new Button( "start new curve" ); gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbConstraints.gridwidth = 2; gbConstraints.weighty = 0.0; addButton.addActionListener(this); gbLayout.setConstraints(addButton, gbConstraints); add(addButton); Label blank1 = new Label(" "); gbConstraints.gridwidth = 5; gbLayout.setConstraints(blank1, gbConstraints); add(blank1); cbDelete = new Checkbox("delete selected points"); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row cbDelete.addItemListener(this); gbLayout.setConstraints(cbDelete, gbConstraints); add(cbDelete); Label blank2 = new Label(" "); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbLayout.setConstraints(blank2, gbConstraints); add(blank2); levelLabel = new Label("Subdivision Level: 0"); gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbConstraints.gridwidth = 1; gbConstraints.weighty = 0.0; gbLayout.setConstraints(levelLabel, gbConstraints); add(levelLabel); levelSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 6); levelSlider.setBlockIncrement(1); levelSlider.addAdjustmentListener(this); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row gbLayout.setConstraints(levelSlider, gbConstraints); add(levelSlider); Label blank3 = new Label(" "); gbConstraints.gridwidth = 1; gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbLayout.setConstraints(blank3, gbConstraints); add(blank3); cbApprox = new Checkbox( "Approximating", cbgType, true); gbConstraints.gridwidth = 5; cbApprox.addItemListener(this); gbLayout.setConstraints(cbApprox, gbConstraints); add(cbApprox); Label blank4 = new Label(" "); gbConstraints.gridwidth = 2; gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbLayout.setConstraints(blank4, gbConstraints); add(blank4); cbInterp = new Checkbox( "Interpolating", cbgType, false); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; //end row cbInterp.addItemListener(this); gbLayout.setConstraints(cbInterp, gbConstraints); add(cbInterp); } public void adjustmentValueChanged(AdjustmentEvent ae) { if(ae.getSource() == levelSlider) { iLevel = levelSlider.getValue(); framedArea.setSubdivisionLevel(iLevel); updateLevelLabel(iLevel); } } public void actionPerformed(ActionEvent ae) { if(ae.getSource() == clearButton) { framedArea.clearSelectedArea(); } else if(ae.getSource() == addButton) { framedArea.addCurveToSelectedArea(); } } public void itemStateChanged(ItemEvent ie) { if(ie.getSource() == cbDelete) { if( ie.getStateChange() == ItemEvent.SELECTED ) { bDeletePoints = true; } else { bDeletePoints = false; } } else if(ie.getSource() == cbApprox) { if( ie.getStateChange() == ItemEvent.SELECTED ) { iType = SubdivisionCurve2D.APPROXIMATING; framedArea.setSubdivisionType(iType); } } else if(ie.getSource() == cbInterp) { if( ie.getStateChange() == ItemEvent.SELECTED ) { iType = SubdivisionCurve2D.INTERPOLATING; framedArea.setSubdivisionType(iType); } } } public void updateStatusLabel(int xx, int yy) { int x = xx + 3; int y = yy + 3; statusLabel.setText("Control point added at (" + x + ", " + y + ")"); } public void updateLevelLabel(int level) { levelLabel.setText(" Subdivision Level: " + level); } public static void main (String[] args) { Main m = new Main(); m.init(); final Frame f = new Frame("Subdivision Curve"); f.add(m); f.setSize(600,600); f.show(); } } ////////////////////////////////////////////////////////////////////////////////// // This class exists solely to put a frame around the coordinate area. class FramedArea extends Panel { SelectionArea sArea = null; public FramedArea(Main controller) { super(); setLayout(new GridLayout(1,0)); //Set layout so its contents are as big as possible. sArea = new SelectionArea(controller); add(sArea); } public void clearSelectedArea() { sArea.clearAll(); } public void addCurveToSelectedArea() { sArea.addCurve(); } public void setSubdivisionLevel(int iLevel) { sArea.setSubdivisionLevel(iLevel); } public void setSubdivisionType(int iType) { sArea.setSubdivisionType(iType); } public Insets getInsets() { return new Insets(4,4,5,5); } public void paint(Graphics g) { Dimension d = getSize(); Color bg = getBackground(); g.setColor(bg); g.draw3DRect(0, 0, d.width - 1, d.height - 1, true); g.draw3DRect(3, 3, d.width - 7, d.height - 7, false); } } ////////////////////////////////////////////////////////////////////////////////// class SelectionArea extends Canvas { Main controller; Vector m_vSubdivisionCurves = new Vector(); SubdivisionCurve2D m_scCurveSelected = null; SubdivisionCurve2D m_scCurveCurrent = null; public SelectionArea(Main controller) { super(); this.controller = controller; MyListener myListener = new MyListener(); addMouseListener(myListener); addMouseMotionListener(myListener); } class MyListener extends MouseAdapter implements MouseMotionListener { public void mousePressed(MouseEvent e) { /* System.out.println( "Button Pressed: " + e.getModifiers() ); System.out.println( " : " + MouseEvent.BUTTON1_MASK + " " + MouseEvent.BUTTON2_MASK + " " + MouseEvent.BUTTON3_MASK ); */ if( (e.getModifiers() == MouseEvent.BUTTON1_MASK) || // Java 1.2 Button 1 (e.getModifiers() == 0) ) // Java 1.1 { SubdivisionCurve2D scCurve; int x = e.getX(); int y = e.getY(); m_scCurveSelected = null; for (Enumeration enum = m_vSubdivisionCurves.elements(); enum.hasMoreElements(); ) { scCurve = (SubdivisionCurve2D)enum.nextElement(); if( scCurve.selectedControlPoint(x, y) ) // Select point { m_scCurveSelected = scCurve; } } if( m_scCurveSelected == null ) // Clicked in empty space { // Add new Point controller.updateStatusLabel(x, y); Point2D pt = new Point2D(x, y); if( m_scCurveCurrent == null ) { m_scCurveCurrent = new SubdivisionCurve2D(controller.iLevel, controller.iType); m_vSubdivisionCurves.addElement(m_scCurveCurrent); } m_scCurveSelected = m_scCurveCurrent; m_scCurveCurrent.addPoint(pt); //m_scCurveCurrent.display(); repaint(); } else // Clicked on point { if( controller.bDeletePoints ) // delete selected point { m_scCurveSelected.deleteSelectedControlPoint(); controller.statusLabel.setText("Control point deleted at (" +x+ ", " +y+ ")"); repaint(); } } } else if( e.getModifiers() == MouseEvent.BUTTON2_MASK ) // Button 2 { } else if( e.getModifiers() == MouseEvent.BUTTON3_MASK ) // Button 3 { } else { System.out.println( "Button Press Error"); } } public void mouseDragged(MouseEvent e) { if( m_scCurveSelected != null ) { int x = e.getX(); int y = e.getY(); m_scCurveSelected.moveSelectedControlPoint( x, y ); controller.statusLabel.setText("Control point moved to (" +x+ ", " +y+ ")"); repaint(); } } public void mouseMoved(MouseEvent e) { } public void mouseReleased(MouseEvent e) { if( m_scCurveSelected != null ) { m_scCurveSelected.deselectControlPoint(); } } } public void clearAll() { m_scCurveSelected = null; m_scCurveCurrent = null; m_vSubdivisionCurves.removeAllElements(); repaint(); } public void addCurve() { if( m_vSubdivisionCurves != null) { m_scCurveCurrent = new SubdivisionCurve2D(controller.iLevel, controller.iType); m_vSubdivisionCurves.addElement(m_scCurveCurrent); m_scCurveSelected = null; } } public void setSubdivisionLevel(int iLevel) { SubdivisionCurve2D scCurve; for (Enumeration enum = m_vSubdivisionCurves.elements(); enum.hasMoreElements(); ) { scCurve = (SubdivisionCurve2D)enum.nextElement(); scCurve.setLevel(iLevel); } repaint(); } public void setSubdivisionType(int iType) { SubdivisionCurve2D scCurve; for (Enumeration enum = m_vSubdivisionCurves.elements(); enum.hasMoreElements(); ) { scCurve = (SubdivisionCurve2D)enum.nextElement(); scCurve.setType(iType); } repaint(); } public void paint(Graphics g) { SubdivisionCurve2D scCurve; for (Enumeration enum = m_vSubdivisionCurves.elements(); enum.hasMoreElements(); ) { scCurve = (SubdivisionCurve2D)enum.nextElement(); scCurve.paint(g); } } }