CSE190L Sample Program handout #10 Driver class ColorsMain.java ---------------------------- // Driver class that launches the application public class ColorsMain { public static void main(String[] args) { ColorsFrame frame = new ColorsFrame(); frame.start(); } } Panel class ColorsPanel.java ---------------------------- // This class draws a colored panel with some text in it. import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class ColorsPanel extends JPanel { private boolean textIsBlack; private String text; private int x; private int y; public ColorsPanel() { textIsBlack = true; setPreferredSize(new Dimension(200, 200)); } public void setTextParameters(String text, int x, int y) { this.text = text; this.x = x; this.y = y; repaint(); } // switches the color uses for the text public void switchTextColor() { textIsBlack = !textIsBlack; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; if (textIsBlack) g2.setPaint(Color.BLACK); else g2.setPaint(Color.WHITE); g2.drawString(text, x, y); } } Frame class ColorsFrame.java ---------------------------- // This class sets up the user interface controls for a program that controls // a panel with text in it. The user is given buttons to change the panel's // background color and when the user clicks on the panel, it switches the // text color. import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; public class ColorsFrame { private JFrame frame; private ColorsPanel panel; private JTextField textEntry; private JTextField xEntry; private JTextField yEntry; private static final String INITIAL_TEXT = "Hello World!"; private static final int INITIAL_XY = 50; public ColorsFrame() { frame = new JFrame(); frame.setTitle("Colors fun"); // frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // construct panel and set up a mouse listener to switch its text // color when the user clicks on the panel panel = new ColorsPanel(); panel.setTextParameters(INITIAL_TEXT, INITIAL_XY, INITIAL_XY); panel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { panel.switchTextColor(); } }); frame.add(panel, BorderLayout.CENTER); // add the panel of buttons at the top of the frame frame.add(buttonPanel(), BorderLayout.NORTH); frame.add(textFieldPanel(), BorderLayout.SOUTH); frame.pack(); } // call this method to start the program executing public void start() { frame.setVisible(true); } // constructs and returns a panel with five buttons that change the // background color of the panel. private JPanel buttonPanel() { JPanel result = new JPanel(); result.setLayout(new GridLayout(0, 3)); result.add(makeButton("Yellow", Color.YELLOW), BorderLayout.NORTH); result.add(makeButton("Blue", Color.BLUE), BorderLayout.SOUTH); result.add(makeButton("Red", Color.RED), BorderLayout.EAST); result.add(makeButton("Magenta", Color.MAGENTA), BorderLayout.WEST); result.add(makeButton("Green", Color.GREEN), BorderLayout.CENTER); return result; } // constructs and returns a JButton with the given text that when pressed // will set the background color of the panel to the given color private JButton makeButton(String text, final Color c) { JButton button = new JButton(text); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel.setBackground(c); } }); return button; } // Constructs a panel that has three text fields for controlling the // colored panel: one for the text to draw, and two for x/y coordinates // of where to draw the string. private JPanel textFieldPanel() { JPanel result = new JPanel(); ActionListener textFieldListener = new ActionListener() { public void actionPerformed(ActionEvent e) { updatePanel(); } }; FocusListener textFocusListener = new FocusAdapter() { public void focusLost(FocusEvent e) { updatePanel(); } }; textEntry = makeField(INITIAL_TEXT, 30, textFieldListener, textFocusListener, "text to display", result); xEntry = makeField("" + INITIAL_XY, 3, textFieldListener, textFocusListener, "x", result); yEntry = makeField("" + INITIAL_XY, 3, textFieldListener, textFocusListener, "y", result); return result; } // constructs and returns a text field with given text and columns; adds // the given focus listener and action listener; adds to the given panel // along with a label using the given label text. private JTextField makeField(String text, int cols, ActionListener action, FocusListener focus, String label, JPanel p) { JTextField field = new JTextField(text, cols); field.addActionListener(action); field.addFocusListener(focus); p.add(new JLabel(label)); p.add(field); return field; } // updates the panel's string and x/y coordinates private void updatePanel() { int x, y; try { x = Integer.parseInt(xEntry.getText().trim()); } catch (NumberFormatException e) { x = INITIAL_XY; xEntry.setText("" + x); Toolkit.getDefaultToolkit().beep(); } try { y = Integer.parseInt(yEntry.getText().trim()); } catch (NumberFormatException e) { y = INITIAL_XY; yEntry.setText(y + ""); Toolkit.getDefaultToolkit().beep(); } panel.setTextParameters(textEntry.getText(), x, y); } }
Stuart Reges
Last modified: Wed Apr 18 19:26:44 PDT 2007