package koz; // change to your andrew id! // PLACE THIS FILE IN A DIRECTORY WITH YOUR andrewId AS ITS NAME!!! // Wrapper class for Hw3. // See comments in Hw2.java for more details. 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 Hw3... public class Hw3 extends Hw3Base { public static void main(String[] args) { Hw3Base.main(args); } } // Change to set the first class that is displayed on launch class StartWith { public static String startWith = "none"; } // eg: "2" class Hw3Q2 { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { System.out.println("Place code for Q2 here!!!"); int i = scanner.nextInt(); System.out.println("read: " + i); } } class Hw3Q3 { public static void main(String[] args) { System.out.println("Place code for Q3 here!!!"); } } class Hw3Q4 { public static void main(String[] args) { System.out.println("Place code for Q4 here!!!"); } } class Hw3Q5 { public static void main(String[] args) { System.out.println("Place code for Q5 here!!!"); } } class Hw3Q6 { public static void main(String[] args) { System.out.println("Place code for Q6 here!!!"); } } class Hw3Q7 { public static void main(String[] args) { System.out.println("Place code for Q7 here!!!"); } } class Hw3Q8_1 extends JComponent { public void paint(Graphics page) { Hw3.paintEmptyFlag(this,page,"Panama"); // REPLACE WITH YOUR CODE! } } class Hw3Q8_2 extends JComponent { public void paint(Graphics page) { Hw3.paintEmptyFlag(this,page,"Grenada"); // REPLACE WITH YOUR CODE! } } class Hw3Q8_3 extends JComponent { public void paint(Graphics page) { Hw3.paintEmptyFlag(this,page,"European Union"); // REPLACE WITH YOUR CODE! } } class Hw3Q8_4 extends JComponent { public void paint(Graphics page) { Hw3.paintEmptyFlag(this,page,"BONUS: Rotated Star"); // REPLACE WITH YOUR CODE! } } class Hw3Q8_5 extends JComponent { public void paint(Graphics page) { Hw3.paintEmptyFlag(this,page,"BONUS: St Pierre and Miquelon"); // 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 = "Q"; // (question.startsWith("5")) ? "Q" : "PP"; String className = packageName + ".Hw3" + 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 Hw3Base() { try { init(); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Exception in Hw3Base 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! Hw3 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(Hw3Base.class.getName()); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2,6)); String[] buttonLabels = { "2", "3", "4", "5", "6", "7", "8.1", "8.2", "8.3", "8.4", "8.5" }; for (int i=0; i