Chris Pollett>Old Classes>PIC 20, Winter 2000>Hw5>Hw5 Solutions
Problem 12.11 ============= // Program Name: Printer.java // // Purpose: This application pops a window that would be appropriate // to configure the printing options for an Epson Printer. // It is supposed to look like the window drawn in D&D but // it does not do anything besides draw the window. // // // Known Bugs: None // // import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Printer extends JFrame { Container c; JPanel leftPanel = new JPanel(); JPanel outer = new JPanel(); // Function Name: Printer // // Purpose: sets the name on title bar, sets layout add the two // add the two outermost panels // // Known Bugs: // public Printer() { super("Printer"); c = getContentPane(); c.setLayout(new FlowLayout(FlowLayout.CENTER)); outer.setLayout(new BorderLayout()); addLeftPanel(); addButtonPanel(); c.add(outer); setSize(480,170); show(); } // Function Name: addLeftPanel // // Purpose: add everythings except four buttons on right // // // Known Bugs: // public void addLeftPanel() { leftPanel.setLayout(new BorderLayout()); JLabel epson = new JLabel("Printer: Epson EPL-7000"); leftPanel.add(epson,BorderLayout.NORTH); addBoxes(); addPrintQuality(); outer.add(leftPanel, BorderLayout.CENTER); } // Function Name: addBoxes // // Purpose: add all the checkboxes and radio buttons // in center of window // // Known Bugs: // public void addBoxes() { JCheckBox image = new JCheckBox("Image"); JCheckBox text = new JCheckBox("Text"); JCheckBox code = new JCheckBox("Code"); JRadioButton selection = new JRadioButton("Selection",false); JRadioButton all = new JRadioButton("All",true); JRadioButton applet = new JRadioButton("Applet",false); JPanel boxes = new JPanel(); boxes.setLayout(new GridLayout(3,2,20,5)); boxes.add(image); boxes.add(selection); boxes.add(text); boxes.add(all); boxes.add(code); boxes.add(applet); JPanel centeredBoxes = new JPanel(); centeredBoxes.setLayout(new FlowLayout(FlowLayout.CENTER)); centeredBoxes.add(boxes); leftPanel.add(centeredBoxes,BorderLayout.CENTER); } // Function Name: addPrintQuality // // Purpose: add JLabel, JComboBox JCheckBox of bottom left // hand side of window // // Known Bugs: // public void addPrintQuality() { String s[]={"High"}; JLabel printQuality = new JLabel("Print Quality: "); JComboBox high = new JComboBox(s); JCheckBox printToFile = new JCheckBox("Print to File ",false); JPanel pqPanel = new JPanel(); pqPanel.setLayout(new FlowLayout()); pqPanel.add(printQuality); pqPanel.add(high); pqPanel.add(printToFile); leftPanel.add(pqPanel,BorderLayout.SOUTH); } // Function Name: addButtonPanel // // Purpose: this adds the four buttons for the right hand // side of the window // // Known Bugs: // public void addButtonPanel() { JButton ok = new JButton("Ok"); JButton cancel = new JButton("Cancel"); JButton setup = new JButton("Setup..."); JButton help = new JButton("Help"); JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(4,1,5,5)); buttons.add(ok); buttons.add(cancel); buttons.add(setup); buttons.add(help); outer.add(buttons, BorderLayout.EAST); } // Function Name: main // // Purpose: construct a Printer; add window listener // // // Known Bugs: // public static void main (String args[]) { Printer app = new Printer(); app.addWindowListener( new WindowAdapter(){ public void windowClosing ( WindowEvent e) { System.exit(0); } } ); } } Problem 13.7 ============ // Program Name: Circle.java // // Purpose: The application create a window with a button, textarea and // circle. The textarea contains the circle's area, radius, // diameter, and perimeter. The circle is drawn in its own panel. // Whenever the button is clicked the old circle is erased and // a new circle of random size <150 // is drawn and the textarea is updated with its information. // // Known Bugs: None // // import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class Circle extends JFrame { CirclePanel circlePanel = new CirclePanel(); JTextArea circleProperties= new JTextArea(20,20); Container c; // Function Name: Circle // // Purpose: Constructor for a Circle application. // It adds the button, the textarea, and the CirclePanel // on which the circle is drawn. It also adds the // button listener // // Known Bugs: // public Circle() { super("Circle"); c = getContentPane(); c.setLayout(new BorderLayout()); c.add(circlePanel, BorderLayout.WEST); circleProperties.setEditable(false); circleProperties.setText(circlePanel.toString()); c.add(circleProperties, BorderLayout.CENTER); JButton circleButton = new JButton("New Circle"); circleButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { circlePanel.randomize(); circleProperties.setText( circlePanel.toString()); c.repaint(); } } ); c.add(circleButton, BorderLayout.SOUTH); setSize(400,220); show(); } // Function Name: main // // Purpose: create a Circle JFrame and an associated WindowListener // // // Known Bugs: // public static void main (String args[]) { Circle app = new Circle(); app.addWindowListener( new WindowAdapter(){ public void windowClosing ( WindowEvent e) { System.exit(0); } } ); } } // Class Name: CirclePanel // // Purpose: a JPanel which has a Circle drawn on it of diameter // given by its diameter variable // // Known Bugs: // // class CirclePanel extends JPanel { double diameter; // holds current circle diameter // Function Name: CirclePanel // // Purpose: set the background color of the CirclePanel // give a random initial value <150 to the diameter // // Known Bugs: // public CirclePanel() { super(); setBackground(Color.black); randomize(); } // Function Name: randomize // // Purpose: Sets the diameter or circle to be a random // value between 0 and 150 // // Known Bugs: // public void randomize() { diameter = Math.random()*150; } // Function Name: paintComponent // // Purpose: draws the circle on the CirclePanel // according to the size held in diameter // // Known Bugs: // public void paintComponent(Graphics g) { int top,bot; super.paintComponent(g); g.setColor(Color.red); top = (175-(int)diameter)/2; bot = (int)diameter; g.fillOval(top,top,bot,bot); } // Function Name: getPreferredSize // // Purpose: returns the preffered drawing size of a // CirclePanel object // // Known Bugs: // public Dimension getPreferredSize() { return new Dimension( 175, 175); } // Function Name: toString // // Purpose: returns a string containing information about // the area, radius, diameter, and circumference // of the circle on the CirclePanel // // Known Bugs: // public String toString() { DecimalFormat twoDigits = new DecimalFormat( "0.00"); double area = Math.PI*diameter*diameter/4, radius = diameter/2, circum = Math.PI*diameter; String s = " Circle Properties\n"+ " =================\n\n\n"+ " area = "+twoDigits.format(area)+"\n\n"+ " radius = "+twoDigits.format(radius)+"\n\n"+ " diameter = "+twoDigits.format(diameter)+"\n\n"+ " circumference = "+twoDigits.format(circum)+"\n"; return s; } } Problem 13.13 ============= // Program Name: MyColorChooser.java // // Purpose: A MyColorChooser is a JPanel with three Sliders and three // TextFields on it. The Sliders correspond to three color // red, green, blue. Adjusting the sliders changes the value in // the associated textfield, also it changes the color // returned by the MyColorChoosers getColor method. Finally, // changing a slider causes the container held in the vairable // c and passed to the MyColorChooser in its constructor to be // repainted. // // Known Bugs: // // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MyColorChooser extends JPanel { Container c; JSlider redSlider, greenSlider, blueSlider; JTextField redField, greenField, blueField; // Function Name: MyColorChooser // // Purpose: Constructs a MyColorChooser JPanel. // Sets up the three color sliders and their // associated textfields. // Set the Container c to be the Container passed // to the constructor. // // Known Bugs: // public MyColorChooser(Container contents) { super(); c=contents; setBackground(Color.gray); setLayout(new GridLayout(3,2,15,5)); redField = new JTextField(3); redSlider = new JSlider( SwingConstants.HORIZONTAL,0,255,125); setUpSliderField(redSlider,redField,Color.red); greenField = new JTextField(3); greenSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,125); setUpSliderField(greenSlider,greenField,Color.green); blueField = new JTextField(3); blueSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,125); setUpSliderField(blueSlider,blueField,Color.blue); } // Function Name: setUpSliderField // // Purpose: initialize a slider, initialize it background color // and its associated textfield // // Known Bugs: // void setUpSliderField(JSlider s, JTextField f, Color color) { f.setEditable(false); f.setText("125"); s.setBackground(color); s.setMajorTickSpacing(20); s.setPaintTicks(true); s.addChangeListener( new SlideListener(s, f ) ); add(s); add(f); } // Function Name: getPreferredSized // // Purpose: returns the preferred dimension for drawing something // of type MyColorChooser // // Known Bugs: // public Dimension getPreferredSize() { return new Dimension( 200, 150); } // Function Name: getColor // // Purpose: returns the color corresponding to the current slider // values // // Known Bugs: // public Color getColor() { return new Color(redSlider.getValue(), greenSlider.getValue(), blueSlider.getValue()); } // Inner Class Name: SlideListener // // Purpose: Used to handle stateChanged events for our three Sliders // // // Known Bugs: // class SlideListener implements ChangeListener { JSlider s; JTextField f; // Function Name: SlideListener // // Purpose: Constructor gets a copy of the slider // object and textfield it needs to handle // // Known Bugs: // public SlideListener(JSlider slider, JTextField field) { s=slider; f=field; } // Function Name: stateChanged // // Purpose: Repaint the Container the MyColorChooser lives on // and set the text to be the current slider value // // Known Bugs: // public void stateChanged(ChangeEvent e) { f.setText(""+s.getValue()); c.repaint(); } } } // Program Name: MyColorTest.java // // Purpose: This file creates a JFrame that allow the user to // manipulate three siders to set the Color of a rectangle // TextFields are also used to display current RGB value // // Known Bugs: // // import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyColorTest extends JFrame { Container c; MyColorChooser m; ColoredRectangle cr=new ColoredRectangle(); // Function Name: MyColorTest // // Purpose: Constructor for MyColorTest, sets the layout // and adds a MyColorChooser object and a ColorRectangle // object to the JFrame // // Known Bugs: // public MyColorTest() { super("MyColorChooserTest"); c=getContentPane(); c.setLayout(new BorderLayout()); m = new MyColorChooser(c); c.add(m, BorderLayout.CENTER); c.add(cr, BorderLayout.EAST); setSize(400,150); show(); } // Function Name: main // // Purpose: creates instance of MyColorTest adds a window // closing listener // // // Known Bugs: // public static void main (String args[]) { MyColorTest app = new MyColorChooserTest(); app.addWindowListener( new WindowAdapter(){ public void windowClosing ( WindowEvent e) { System.exit(0); } } ); } // Inner Class Name: ColoredRectangle // // Purpose: JPanel on which a rectangle is drawn. Is used by // MyColorTest to give the example color for the current // slider settings. // // Known Bugs: // class ColoredRectangle extends JPanel { // Function Name: ColoredRectangle // // Purpose: Set the background colour of this component to white // // // Known Bugs: // public ColoredRectangle() { super(); setBackground(Color.white); } // Function Name: paintComponent // // Purpose: fills the rectangle according to the color // given by the sliders in m. // // // Known Bugs: // public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(m.getColor()); g.fillRect(15,15,100,100); } // Function Name: getPreferredSize // // Purpose: returns the preferred size for drawing a ColorRectangle // object // // Known Bugs: // public Dimension getPreferredSize() { return new Dimension(130,130); } } //end ColoredRectangle } //end MyColorTest