/* ColorAppDemo.java @author version 1.0 03/06/96 Hirschfield & Decker @author version 1.2 01/18/99 by Robert Philbin @author version 2.1 04/13/10 R.Philbin - convert to Swing application */ /* This comment outlines your assignment for this lablet. After each number below, starting with #2, recompile and run 1 What are the class file names? To which classes do they correspond? Answer by adding a comment after each class specifying the name of the resulting file. I;ve done this for class ColorAppDemo 2 Experiment with changing the 18 in f = new Font("Serif", Font.BOLD, 18); to some other number. Also, find and document the location of the on-line documentation for this method. To change the font, use one of Serif, Sans-serif, Monospaced, Dialog, or DialogInput. 3 Try commenting out one of the repaint(); calls. What happens and why? 4 What is the purpose of each of the 6 variables? Answer this by writing a short comment after each as I've done for the variable f. 5 Change the numeral 3 to a 2 in the line colorOfText = ++colorOfText % 3; What is the effect? Why? Put your answer immediately after the line (~line91) as a comment. 6 How did we know to name the methods actionPerformed and paint and how did we know what their "signatures" should be (i.e., the "arguments" or "parameters" in the parentheses) ? Answer this by adding comments just before each of the three methods in question which specify where in the documentation you found the method and its signature. 7 Add two, different, colors to the text color scheme. 8 Change the output text to something of your own choosing. For a challenge, try making it print out the name of the Color, either fore- or back-ground. */ import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class ColorAppDemo extends JPanel // Q1: generates ColorAppDemo.class { Font f; // output Font int colorOfBackground = 0; // Q4: ??? int colorOfText = 0; // Q4: ??? JButton backButton; // Q4: ??? JButton textButton; // Q4: ??? Dimension myAppDim; // Q4: ??? public ColorAppDemo() { f = new Font("Helvetica", Font.BOLD, 18); // Q2: ??? backButton = new JButton ("Background Color"); add(backButton); backButton.addActionListener( new BackGndButtonListener() ); textButton = new JButton("Text Color"); add(textButton); textButton.addActionListener( new TextButtonListener() ); } // BackGndButtonListener is an 'inner' class that can 'see' instance variables class BackGndButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { colorOfBackground = ++colorOfBackground % 4; repaint(); // Q3: ??? } } // TextButtonListener is an 'inner' class that can 'see' instance variables class TextButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { colorOfText = ++colorOfText % 3; // A5: ????????????? repaint(); // Q3: ??? } } // This method is called whenever the window is redrawn // Who calls it? public void paint(Graphics g) { super.paint(g); switch (colorOfBackground) { case 0: setBackground(Color.cyan); break; case 1: setBackground(Color.orange); break; case 2: setBackground(Color.red); break; case 3: setBackground(Color.black); break; default: setBackground(Color.cyan); break; } switch (colorOfText) { case 0: setForeground(Color.blue); break; case 1: setForeground(Color.magenta); break; case 2: setForeground(Color.green); break; default: setForeground(Color.blue); break; } g.setFont(f); myAppDim = getSize(); g.drawString("Goodbye World! Hello Java!!", (myAppDim.width/2) - 120, (myAppDim.height/2) + 20); } /** for testing purposes only, generally in a Test... class */ public static void main(String args[]) { ColorAppDemo cad = new ColorAppDemo(); javax.swing.JFrame app = new javax.swing.JFrame("Test ColorAppDemo"); app.add( cad ); app.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE ); app.setSize( 400, 300 ); app.setVisible( true ); } }