| Assignment 2 --- Due Monday, March 5, 2007 |
|
Theory
|
Program
The following Java program ought to compile (at least under a 4.2 compiler) without any trouble, and run: javac Simple.javaYour task is to rewrite the createSimplePolygon() function so that the "simple" tab draws a simple polygon through those points. // Sample program // Draws a bunch of points // Then draws a random polygon through them. // // Your job is to make that polygon simple, by // permuting the elements of the array C[], in // the createSimplePolygon() function. // // Original by: Robert Hochberg // February 19, 2007 // // Modified by: // import java.io.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; public class Simple{ static int POINTWID = 4; // size of points // the x and y arrays hold the coordinates // the B array is the order of the points in the polygon // You want to fill the C array with the simple polygon static double x[] = new double[200]; static double y[] = new double[200]; static int B[] = new int[200]; // the permutation matrix static int C[] = new int[200]; // the one that becomes simple static SimpleFrame myFrame; static int numPoints = 3; public static void main(String args[]) { makePolygons(); // Create the frame to draw on myFrame = new SimpleFrame(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setSize(600, 600); myFrame.setVisible(true); } public static void makePolygons(){ // Build an array of random points in the unit square for(int i = 0; i < numPoints; i++){ x[i] = Math.random(); y[i] = Math.random();// Sample program B[i] = i; // default permutation } // Create the simple polygon createSimplePolygon(); } /* * This is the only function you need to mess with */ public static void createSimplePolygon(){ // Initialize the C[] array with the identity permutation for(int i = 0; i < numPoints; i++) C[i] = i; // Bubble sort the points from left to right for(int i = 0; i < numPoints; i++) for(int j = 0; j < numPoints - 1; j++) if(x[C[j]] > x[C[j+1]]){ int temp = C[j]; C[j] = C[j+1]; C[j+1] = temp; } } public static class SimpleFrame extends JFrame{ public static JSlider numPointsSlider; public SimpleFrame() { super("Create you own Simple Polygon"); Container content = getContentPane(); content.setLayout(new java.awt.BorderLayout()); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setPreferredSize (new java.awt.Dimension(300, 400)); tabbedPane.addTab("Scrambled", new ScrambledPanel()); tabbedPane.addTab("Simple", new SimplePanel()); content.add(tabbedPane, java.awt.BorderLayout.CENTER); // Slider for the number of points numPointsSlider = new JSlider (javax.swing.SwingConstants.HORIZONTAL, 3, 100, 11); numPointsSlider.addChangeListener (new javax.swing.event.ChangeListener () { public void stateChanged (javax.swing.event.ChangeEvent evt) { numPointsSliderStateChanged (evt); } } ); content.add(numPointsSlider, java.awt.BorderLayout.SOUTH); } private void numPointsSliderStateChanged (javax.swing.event.ChangeEvent evt) { numPoints = numPointsSlider.getValue(); makePolygons(); repaint(); } } public static class ScrambledPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // First set the scaling to fit the window Dimension size = getSize(); int Xwid = (int) (0.95 * size.width); int Ywid = (int) (0.95 * size.height); // First draw the segments g2.setColor(Color.red); for(int i = 0; i < numPoints; i++) g2.drawLine((int) (Xwid * x[B[i]]), (int) (Ywid * y[B[i]]), (int) (Xwid * x[B[(i+1) % numPoints]]), (int)(Ywid * y[B[(i+1) % numPoints]])); // Now draw the points for(int i = 0; i < numPoints; i++){ g2.fillRect((int) (Xwid * x[i]) - POINTWID, (int) (Ywid * y[i]) - POINTWID, 2*POINTWID + 1, 2*POINTWID + 1); } } } public static class SimplePanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // First set the scaling to fit the window Dimension size = getSize(); int Xwid = (int) (0.95 * size.width); int Ywid = (int) (0.95 * size.height); // First draw the segments g2.setColor(Color.red); for(int i = 0; i < numPoints; i++) g2.drawLine((int) (Xwid * x[C[i]]), (int) (Ywid * y[C[i]]), (int) (Xwid * x[C[(i+1) % numPoints]]), (int)(Ywid * y[C[(i+1) % numPoints]])); // Now draw the points for(int i = 0; i < numPoints; i++){ g2.fillRect((int) (Xwid * x[i]) - POINTWID, (int) (Ywid * y[i]) - POINTWID, 2*POINTWID + 1, 2*POINTWID + 1); } } } } |