HW 4 Due: Midnight SATURDAY the 14th (NOTE: In lecture, I announced due Fri. This made sense when I thought I could get this out well before end of day today. Since I didn't, please take an extra day.) How to turn in: Send two pieces of email to your TA having THE EXACT subject line and contents as specified below. Email #1: Subject: Item1.java Contents should be: A new program that takes advantage of the fact that the new and improved ViewColors class is reusable. The program should be invoked with java Item1 Email #2: Subject: ViewSliderPanel.java Contents should be: A new version of ViewSliderPanel.java that doesn't suffer from the structural problems we discussed in class. This new version should REPLACE the ViewSliderPanel.java provided below. This a quick assignment just intended to exercise your ability to make and leverage good code. Grading Criterion: 2 points: Clearly demonstrates an ability to make and leverage good code. 1 points: Partially demonstrates an ability to make and leverage good code. 0 points: No demonstration. ------------------------------------- // // Original ViewColors.java // For information only. Revised version below. // // Principle problem: // NOT REUSABLE: contains in a single file all // the logic for building the frame, populating it with things, and // dealing with Main. // //******************************************************************** // ViewColors.java Authors: Lewis/Loftus // // Demonstrates the use slider components. //******************************************************************** import java.awt.*; import javax.swing.*; public class ViewColors { public static void main (String[] args) { JFrame frame = new JFrame ("View Colors"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel colorPanel = new JPanel(); colorPanel.setPreferredSize (new Dimension (100, 100)); colorPanel.setBackground (new Color (0, 0, 0)); Container cp = frame.getContentPane(); cp.setLayout (new FlowLayout()); cp.add (new ViewSliderPanel(colorPanel)); cp.add (colorPanel); frame.pack(); frame.show(); } } // END OF FILE //--------------------------------------------------------------------- // Save up to next END OF FILE into a file called ViewColors.java // // Revised ViewColors.demo // import java.awt.*; import javax.swing.*; public class ViewColors extends JFrame { public ViewColors() { getContentPane().add(new ViewColorsGUI(this)); } public static void main (String[] args) { JFrame frame = new ViewColors(); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.pack(); frame.show(); } } class ViewColorsGUI extends JPanel { public ViewColorsGUI(JFrame f) { setPreferredSize (new Dimension (100, 100)); setBackground (new Color (0, 0, 0)); Container cp = f.getContentPane(); cp.setLayout (new FlowLayout()); cp.add (new ViewSliderPanel(this)); cp.add (this); } } // END OF FILE ////////////////////////////////////////// // Original ViewSliderPanel.java // Save this to ViewSliderPanel.java // Edit it to solve the problems describe above. // // Problems with ViewSliderPanel: // // 1. single constructor doesn't fit on a screen // 2. lots of repeating code. // 3. failure to express differences in terms of best available tools. // 4. shows lots of details to client. // 5. Combines layout and creation/construction. // //******************************************************************** // ViewSliderPanel.java Authors: Lewis/Loftus // // Represents the slider control panel for the ViewColors program. //******************************************************************** import java.awt.*; import javax.swing.*; import javax.swing.event.*; public class ViewSliderPanel extends JPanel { private JPanel colorPanel; private JSlider rSlider, gSlider, bSlider; private JLabel rLabel, gLabel, bLabel; //----------------------------------------------------------------- // Sets up the sliders and their labels, aligning them along // their left edge using a box layout. //----------------------------------------------------------------- public ViewSliderPanel (JPanel panel) { colorPanel = panel; rSlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); rSlider.setMajorTickSpacing (50); rSlider.setMinorTickSpacing (10); rSlider.setPaintTicks (true); rSlider.setPaintLabels (true); rSlider.setAlignmentX (Component.LEFT_ALIGNMENT); gSlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); gSlider.setMajorTickSpacing (50); gSlider.setMinorTickSpacing (10); gSlider.setPaintTicks (true); gSlider.setPaintLabels (true); gSlider.setAlignmentX (Component.LEFT_ALIGNMENT); bSlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); bSlider.setMajorTickSpacing (50); bSlider.setMinorTickSpacing (10); bSlider.setPaintTicks (true); bSlider.setPaintLabels (true); bSlider.setAlignmentX (Component.LEFT_ALIGNMENT); SliderListener listener = new SliderListener(); rSlider.addChangeListener (listener); gSlider.addChangeListener (listener); bSlider.addChangeListener (listener); rLabel = new JLabel ("Red: 0"); rLabel.setAlignmentX (Component.LEFT_ALIGNMENT); gLabel = new JLabel ("Green: 0"); gLabel.setAlignmentX (Component.LEFT_ALIGNMENT); bLabel = new JLabel ("Blue: 0"); bLabel.setAlignmentX (Component.LEFT_ALIGNMENT); setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); add (rLabel); add (rSlider); add (Box.createRigidArea (new Dimension (0, 20))); add (gLabel); add (gSlider); add (Box.createRigidArea (new Dimension (0, 20))); add (bLabel); add (bSlider); } //***************************************************************** // Represents the listener for all three sliders. //***************************************************************** private class SliderListener implements ChangeListener { //-------------------------------------------------------------- // Gets the value of each slider, then updates the labels and // the color panel. //-------------------------------------------------------------- public void stateChanged (ChangeEvent event) { int red, green, blue; System.out.println(event); red = rSlider.getValue(); green = gSlider.getValue(); blue = bSlider.getValue(); rLabel.setText ("Red: " + red); gLabel.setText ("Green: " + green); bLabel.setText ("Blue: " + blue); colorPanel.setBackground (new Color (red, green, blue)); } } } // END OF FILE