package koz; // change to your andrew id! // PLACE THIS FILE IN A DIRECTORY WITH YOUR andrewId AS ITS NAME!!! /* * Wrapper class for Hw2. You can write your classes in their own files, if you prefer, but you must copy-and-paste them into this file to submit them. Your emailed and printed submissions must only be this one (edited) file. To use this file, do the following: 1) Change the first line to read "package " 2) If you want, change the String "startWith" to run a problem when you launch. 3) Do not import the java.util.Scanner class -- use the one in this file. For now, this replacement Scanner class just includes the methods nextInt(), nextDouble(), nextFloat(), next(), and nextLine(). The file also includes a simple Polygon class that lets you draw and fill polygons without using arrays (seeing as we've not learned how to use them yet). There is some sample code below (Question 2.17), but it works as such: Polygon p1 = new Polygon(); p1.addPoint(50, 50); p1.addPoint(100,75); p1.addPoint(100,100); p1.addPoint(50,125); p1.addPoint(50,50); p1.draw(page); You add (x,y) points, and when you're done, you call the draw method as shown. Note that you can add all the points at once, as a convenience (though sometimes this might lead to more confusing code, especially if there are lots of points), and you can fill the polygon instead of drawing its outline, as such: Polygon p2 = new Polygon(); p2.addPoints(150, 50, 140, 75, 100, 100, 140, 125, 150, 150, 150, 50); p2.fill(page); Again, this code is used in a sample below. */ import javax.swing.*; import java.awt.*; import java.lang.reflect.*; import java.io.*; import java.util.ArrayList; // import java.util.Scanner; // do NOT import Scanner; use ours (see below!) // Leave this class right here, as-is, so DrJava can see the first class in the file // is the public class Hw2... public class Hw2 extends Hw2Base { public static void main(String[] args) { Hw2Base.main(args); } } // Change to set the first class that is displayed on launch class StartWith { public static String startWith = "none"; } // eg: "2.2" class Hw2PP2_2 { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { System.out.println("Place code for PP2.2 here!!!"); int i = scanner.nextInt(); System.out.println("read: " + i); } } class Hw2PP2_5 { public static void main(String[] args) { System.out.println("Place code for PP2.5 here!!!"); } } class Hw2PP2_6 { public static void main(String[] args) { System.out.println("Place code for PP2.6 here!!!"); } } class Hw2PP2_7 { public static void main(String[] args) { System.out.println("Place code for PP2.7 here!!!"); } } class Hw2PP2_9 { public static void main(String[] args) { System.out.println("Place code for PP2.9 here!!!"); } } class Hw2PP2_17 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyPP(this,page,"2.17"); // REPLACE WITH YOUR CODE! // Here is some sample code to draw polygons. // You should delete this code before you submit your homework! Polygon p1 = new Polygon(); p1.addPoint(50, 50); p1.addPoint(100,75); p1.addPoint(100,100); p1.addPoint(50,125); p1.addPoint(50,50); Polygon p2 = new Polygon(); p2.addPoints(150, 50, 140, 75, 100, 100, 140, 125, 150, 150, 150, 50); page.setColor(Color.black); p1.draw(page); page.setColor(Color.white); p2.fill(page); } } class Hw2PP2_18 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyPP(this,page,"2.18"); // REPLACE WITH YOUR CODE! } } class Hw2PP2_19 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyPP(this,page,"2.19"); // REPLACE WITH YOUR CODE! } } class Hw2PP2_20 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyPP(this,page,"2.20"); // REPLACE WITH YOUR CODE! } } class Hw2Q5_1 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyFlag(this,page,"Hungary"); // REPLACE WITH YOUR CODE! } } class Hw2Q5_2 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyFlag(this,page,"Iceland"); // REPLACE WITH YOUR CODE! } } class Hw2Q5_3 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyFlag(this,page,"Macedonia"); // REPLACE WITH YOUR CODE! } } class Hw2Q5_4 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyFlag(this,page,"the EU"); // REPLACE WITH YOUR CODE! } } class Hw2Q5_5 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyFlag(this,page,"Australia"); // REPLACE WITH YOUR CODE! } } class Hw2Q5_6 extends JComponent { public void paint(Graphics page) { Hw2.paintEmptyFlag(this,page,"Micronesia"); // REPLACE WITH YOUR CODE! } } ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// ///////////////// END OF YOUR CODE ///////////////////// ///////////////// (you may ignore all the code below!!! ///////////////////// ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // a simple Polygon renderer class Polygon { ArrayList xValues = new ArrayList(); ArrayList yValues = new ArrayList(); public void addPoint(int x, int y) { xValues.add(x); yValues.add(y); } public void addPoints(int... values) { int points = values.length/2; for (int i=0; i a) { int[] result = new int[a.size()]; for (int i=0; i 0) { byte b[] = new byte[available]; pin.read(b); String input = new String(b,0,b.length); console.append(input); if (realOut != null) realOut.print(input); } } try { reader.sleep(readerDelay);}catch(InterruptedException ie) {} } } catch (Exception e) { oops("Error in console runReader!",e); } } void launchConsoleMain(String question, final Method mainMethod) { console.setBackground(getBgColor()); console.setText("Running " + question + ":\n"); setMainComponent(consolePane); // use invokeLater so the consolePane displays before any user prompts SwingUtilities.invokeLater(new Runnable() { public void run() { runConsoleMain(mainMethod); } }); } void runConsoleMain(final Method mainMethod) { try { System.setOut(newOut); startReader(); readerDelay=100; mainMethod.invoke(null,new Object[]{null}); } catch (Exception e) { oops("Error: " + e.getCause(),e.getCause()); } finally { readerDelay=100; System.setOut(realOut); } } void oops(String msg, Throwable e) { if (msg.contains("Input canceled")) msg = "Input canceled"; else { System.err.println(msg); e.printStackTrace(); } setMainComponent(new JLabel(msg)); } public void launchFlagViewer(String question) { String qlabel = (question.startsWith("5")) ? "Q" : "PP"; String className = packageName + ".Hw2" + qlabel + question.replace(".","_"); try { Class c = Class.forName(className); JComponent jc = (JComponent) c.newInstance(); setMainComponent(jc); } catch (Exception e) { oops("Failed to load class: " + className,e); } } JPanel cp; // set to outer frame's content pane JComponent mainComponent = null; String packageName; void setMainComponent(JComponent c) { if (mainComponent != null) cp.remove(mainComponent); mainComponent = c; cp.add(c); cp.revalidate(); } public static final JFrame frame = new JFrame(); public Hw2Base() { try { init(); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Exception in Hw2Base constructor: " + e.toString(), "ERROR!", JOptionPane.ERROR_MESSAGE); } } public void init() { String className = getClass().getName(); if (!className.contains(".")) throw new RuntimeException("Packages not set up correctly! Hw2 not in a package!"); packageName = className.substring(0,className.lastIndexOf('.')); if ((packageName.equals("koz")) && (!"Gretchen".equals(System.getenv("USERNAME"))) && (!"David".equals(System.getenv("USERNAME")))) { JOptionPane.showMessageDialog(null, "Please change the package to your andrew id!", "Alert", JOptionPane.ERROR_MESSAGE); System.exit(1); } frame.setTitle(Hw2Base.class.getName()); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(3,6)); String[] buttonLabels = { "2.2", "2.5", "2.6", "2.7", "2.9", "2.17", "2.18", "2.19", "2.20", "5.1", "5.2", "5.3", "5.4", "5.5", "5.6" }; for (int i=0; i