/* * =================================================================== * PolygonGUI.java : Simple GUI with canvas connected to a mouse listener * and buttons connected to button listeners. * * Written By : Mark Austin May 18, 2001 * =================================================================== */ import java.util.*; import java.io.*; import java.text.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class PolygonGUI 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 [4]; buttons[0] = new JButton ("Clear"); buttons[0].addActionListener( new ButtonAction( buttons[0], gs )); buttons[1] = new JButton ("Load"); buttons[1].addActionListener( new ButtonAction( buttons[1], gs )); buttons[2] = new JButton ("Draw"); buttons[2].addActionListener( new ButtonAction( buttons[2], gs )); buttons[3] = new JButton ("Properties"); buttons[3].addActionListener( new ButtonAction( buttons[3], gs )); // 4. Create panel. Add buttons to panel. Panel p1 = new Panel(); for(int ii = 1; ii <= 4; ii++ ) p1.add( buttons[ii-1] ); // 5. Position canvas and panel within the frame. container.add("Center", gs ); container.add( "South", p1 ); } } /* * ================================================================= * 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() ); // Clear Screen .... if( s.compareTo("Clear") == 0 ) { gs.clearScreen(); } // Load polygon from file if( s.compareTo("Load") == 0 ) { gs.loadPolygon(); } // Draw polygon on canvas .... if( s.compareTo("Draw") == 0 ) { gs.drawPolygon(); } // Draw polygon properties on canvas .... if( s.compareTo("Properties") == 0 ) { gs.drawProperties(); } } } /* * ============================================================== * This class listens for mouse events associated with the canvas * ============================================================== */ class GraphicsListener implements MouseListener { private DemoGraphicsScreen gs; public GraphicsListener( DemoGraphicsScreen gs ) { this.gs = gs; } // When the mouse is released, we clear the screen // and draw the polygon and its properties .... public void mouseReleased ( MouseEvent e ) { gs.clearScreen(); gs.drawPolygon(); gs.drawProperties(); } 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 Canvas { int iXcoord, iYcoord; float ScaleFactor = 60; private Dimension size; private Graphics gs; PolygonAnalysis poly = new PolygonAnalysis(); // Use the paint() method to display the (x,y) coordinates of the // mouse when it is clicked... public void paint(Graphics g) { size = getSize(); clearScreen(); } // Get graphics and fill in background .... public void clearScreen () { gs = getGraphics(); Graphics2D g2D = (Graphics2D) gs; g2D.setColor( Color.white ); g2D.fillRect( 0, 0, size.width-1, size.height-1 ); } // Method to load and print polygon ..... public void loadPolygon () { // Load polygon coordinates from input file .... try { poly.polygonInput(); } catch (FileNotFoundException e){} catch (IOException e){} // Print polygon coordinates to scieen ... poly.polygonPrint(); } // Method to draw polygon ..... public void drawPolygon () { float fX1, fX2, fY1, fY2; gs = getGraphics(); Graphics2D g2D = (Graphics2D) gs; // Translate graphics coordinate system ..... g2D.translate ( 20, 20 ); // Draw coordinate axes .... g2D.drawLine ( -5, 0, (int) ScaleFactor, 0 ); g2D.drawLine ( 0, -5, 0, (int) ScaleFactor ); // Draw polygon .... for (int i = 0; i <= poly.NoPoints - 2; i = i + 1) { fX1 = poly.getX(i); fX2 = poly.getX(i+1); fY1 = poly.getY(i); fY2 = poly.getY(i+1); g2D.drawLine ( (int) (ScaleFactor*fX1), (int) (ScaleFactor*fY1), (int) (ScaleFactor*fX2), (int) (ScaleFactor*fY2) ); } fX1 = poly.getX( poly.NoPoints - 1); fX2 = poly.getX( 0 ); fY1 = poly.getY( poly.NoPoints - 1); fY2 = poly.getY( 0 ); g2D.drawLine ( (int) (ScaleFactor*fX1), (int) (ScaleFactor*fY1), (int) (ScaleFactor*fX2), (int) (ScaleFactor*fY2) ); } // Method to compute and print the polygon properties .... public void drawProperties () { float fPerimeter = poly.polygonPerimeter(); float fArea = poly.polygonArea(); gs = getGraphics(); Graphics2D g2D = (Graphics2D) gs; g2D.translate ( 20, 20 ); // Let's use a big "black" font .... Font f = new Font("Courier", Font.BOLD, 16 ); g2D.setFont( f ); g2D.setColor( Color.black ); // Draw area and perimeter on graphics screen .... g2D.drawString( "Polygon Area = " + fArea, ScaleFactor, 350 ); g2D.drawString( "Polygon Perimeter = " + fPerimeter, ScaleFactor, 380 ); // Draw max/min polygon properties .... // .... add details for assignment ..... } // Method to draw centroid of polygon ..... public void drawCentroid () { // .... add details for assignment ..... } }