/* * =================================================================== * DemoGUI.java : Simple GUI with canvas connected to a mouse listener * and buttons connected to button listeners. * * Written By : Mark Austin May, 2005 * =================================================================== */ import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class DemoGUI extends JApplet { Container container = null; DemoGraphicsScreen gs; public void init() { // 1. Get a handle on the applet's content pane. container = this.getContentPane(); // 2. Create a graphics screen and mouse listener, // and add content pane to center of applet. DemoGraphicsScreen gs = new DemoGraphicsScreen(); gs.addMouseListener( new GraphicsListener( gs )); // 3. Create buttons and button listeners. JButton buttons[] = new JButton [2]; buttons[0] = new JButton ("Clear"); buttons[0].addActionListener( new ButtonAction( buttons[0], gs )); buttons[1] = new JButton ("Triangle"); buttons[1].addActionListener( new ButtonAction( buttons[1], gs )); // 4. Create panel. Add buttons to panel. Panel p1 = new Panel(); for(int ii = 1; ii <= 2; ii++ ) p1.add( buttons[ii-1] ); // 5. Position canvas and panel within the frame. container.add( "South", p1 ); container.add("Center", gs ); } } /* * ================================================================= * This class listens for action events associated with the buttons. * ================================================================= */ class ButtonAction implements ActionListener { private JButton b; private DemoGraphicsScreen gs; public ButtonAction ( JButton b, DemoGraphicsScreen gs ) { this.b = b; this.gs = gs; } public void actionPerformed ( ActionEvent e ) { String s = new String(e.getActionCommand()); System.out.println(s); if( s.compareTo("Clear") == 0 ) { gs.clear(); } if( s.compareTo("Triangle") == 0 ) { gs.gsDraw(); } } } /* * ============================================================== * This class listens for mouse events associated with the canvas * ============================================================== */ class GraphicsListener implements MouseListener { private DemoGraphicsScreen gs; public GraphicsListener( DemoGraphicsScreen gs ) { this.gs = gs; } public void mouseReleased ( MouseEvent e ) { // Get and save new coordinates... gs.iXcoord = e.getX(); gs.iYcoord = e.getY(); // Print cursor coordinates and distance to triangle... gs.drawText(); } public void mouseClicked ( MouseEvent e ) {}; public void mouseEntered ( MouseEvent e ) {}; public void mouseExited ( MouseEvent e ) {}; public void mousePressed ( MouseEvent e ) {}; } /* * ======================= * Create graphics screen. * ======================= */ class DemoGraphicsScreen extends JPanel { int iXcoord, iYcoord; String cursorMessage = null; String distanceMessage = null; Triangle t; private Dimension size; private Graphics gs; // Constructor method public DemoGraphicsScreen() { t = new Triangle(); t.node1 = new Node(); t.node2 = new Node(); t.node3 = new Node(); t.node1.dX = 100.0; t.node1.dY = 100.0; t.node2.setName ("n1"); t.node2.dX = 250.0; t.node2.dY = 100.0; t.node3.setName ("n2"); t.node3.dX = 290.0; t.node3.dY = 300.0; t.node1.setName ("n3"); t.edge1 = new Edge( "e1", t.node1, t.node2 ); t.edge2 = new Edge( "e2", t.node2, t.node3 ); t.edge3 = new Edge( "e3", t.node3, t.node1 ); } // Use the paint() method to display the (x,y) coordinates of the // mouse when it is clicked... public void paint(Graphics g) { size = getSize(); drawText(); } // Draw cursor coordinates on the canvas .... public void drawText() { gs = getGraphics(); Graphics2D g2D = (Graphics2D) gs; Node pt1 = new Node( iXcoord, iYcoord ); // Display of cursor coordinates .... if ( cursorMessage != null ) { g2D.setColor( Color.white ); g2D.drawString( cursorMessage, 15, getHeight()-50); } cursorMessage = "Cursor: " + iXcoord + ", " + iYcoord; g2D.setColor(Color.blue); g2D.drawString( cursorMessage, 15, getHeight()-50); // Display distance to triangle if ( distanceMessage != null ) { g2D.setColor( Color.white ); g2D.drawString( distanceMessage, 15, getHeight()-30); } distanceMessage = "Distance to triangle: " + t.distanceToPoint( pt1 ); g2D.setColor(Color.blue); g2D.drawString( distanceMessage, 15, getHeight()-30); } // Get graphics and fill in background .... public void clear () { gs = getGraphics(); Graphics2D g2D = (Graphics2D) gs; g2D.setColor( Color.white ); g2D.fillRect( 0, 0, size.width-1, size.height-1 ); } // Method to draw triangle .... public void gsDraw () { gs = getGraphics(); Graphics2D g2D = (Graphics2D) gs; // Draw triangle nodes and edges .... g2D.setColor( Color.blue ); g2D.drawLine ( (int) t.node1.dX, (int) t.node1.dY, (int) t.node2.dX, (int) t.node2.dY ); g2D.drawLine ( (int) t.node2.dX, (int) t.node2.dY, (int) t.node3.dX, (int) t.node3.dY ); g2D.drawLine ( (int) t.node1.dX, (int) t.node1.dY, (int) t.node3.dX, (int) t.node3.dY ); g2D.fillRect( (int) (t.node1.dX-3), (int) (t.node1.dY-3), 6, 6); g2D.fillRect( (int) (t.node2.dX-3), (int) (t.node2.dY-3), 6, 6); g2D.fillRect( (int) (t.node3.dX-3), (int) (t.node3.dY-3), 6, 6); } }