/* Hello585 second example program for CS 585 in Swing. Demonstrates mouse events, painting, tooltips, cursors. Does not implement keyboard accelerator for buttons events. This program has been reworked from the original awt since 1998. Some of the approaches use the older styles .... How could you have the different color and fonts displayed at the same time? No refresh of canvas when changing font or color? Mike Barnes 9/17/02 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; // subclass Frame for the main window public class Hello585Swing extends JFrame { // instantiate the menubar and menuitems private JFrame myFrame; private JMenuBar menuB; private JMenuItem arialItem, timesItem, courierItem, aboutItem, usageItem; private FontItemListener fontItemListener = new FontItemListener(); private HelpItemListener helpItemListener = new HelpItemListener(); private ThreeButtonPanel buttonPanel = new ThreeButtonPanel(); private MyCanvas canvas = new MyCanvas(); public static void main(String args[]) { Hello585Swing mainW = new Hello585Swing("Hello CS585"); mainW.setBounds(20,20,400,200); mainW.setVisible(true); // need to be able to close the window. mainW.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); } // the constructor of the frame window public Hello585Swing(String frameTitle) { super(frameTitle); // call base constructor to set title setIconImage( (new ImageIcon("hi.gif").getImage())); Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); // create menus JMenuBar menuB = new JMenuBar(); setJMenuBar(menuB); menuB.add(createFontMenu()); JMenu HelpMenu = createHelpMenu(); menuB.add(HelpMenu); // add action listerners for menu item "callbacks" arialItem.addActionListener(fontItemListener); timesItem.addActionListener(fontItemListener); courierItem.addActionListener(fontItemListener); aboutItem.addActionListener(helpItemListener); usageItem.addActionListener(helpItemListener); contentPane.add(buttonPanel); // set up canvas canvas.addMouseListener(new MyCanvasMouseListener()); contentPane.add(canvas); /* how do i set these for the frame? contentPane.setMinimumSize(new Dimension(200, 100)); contentPane.setPreferredSize(new Dimension(400,200)); contentPane.setMaximumSize(new Dimension(600, 300)); */ } private JMenu createFontMenu() { JMenu fontMenu = new JMenu("Font", true); fontMenu.setToolTipText("change greeting font family and size"); // create font menu items, mnemonics and accelerators arialItem = new JMenuItem("Arial 10pt"); arialItem.setMnemonic('A'); arialItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_A, ActionEvent.ALT_MASK)); timesItem = new JMenuItem("Times 14pt"); timesItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_T, ActionEvent.ALT_MASK)); timesItem.setMnemonic('T'); courierItem = new JMenuItem("Courier 18pt"); courierItem.setMnemonic('C'); courierItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C, ActionEvent.ALT_MASK)); fontMenu.add(arialItem); fontMenu.add(timesItem); fontMenu.add(courierItem); return fontMenu; } private JMenu createHelpMenu() { JMenu helpMenu = new JMenu ("Help", true); helpMenu.setToolTipText("Everyone needs some help sometime...."); // create help menu items and shortcuts aboutItem = new JMenuItem("About"); aboutItem.setToolTipText("Author information"); aboutItem.setMnemonic('A'); usageItem = new JMenuItem("Usage"); usageItem.setToolTipText("How to use this program"); aboutItem.setMnemonic('U'); helpMenu.add(aboutItem); helpMenu.add(usageItem); return helpMenu; } // font menu listener class FontItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { JMenuItem item = (JMenuItem) event.getSource(); if (item == arialItem) canvas.setFont(new Font("Helvetica", Font.PLAIN, 10)); else if (item == timesItem) canvas.setFont(new Font("TimesRoman", Font.PLAIN, 14)); else canvas.setFont(new Font("Courier", Font.PLAIN, 18)); } } // help menu listener class HelpItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { JMenuItem item = (JMenuItem) event.getSource(); if (item == aboutItem) { Object[] options = {"OK"}; Icon renzo = new ImageIcon("renzo.gif"); JOptionPane.showOptionDialog(null, "Hello585 Swing example\n" + "CS 585 GUI\n" + "Mike Barnes", "About", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, renzo, options, options[0] ); } else // if (item == usageItem) System.out.println(" usageItem"); } } // MyCanvas class MyCanvas extends JPanel { public MyCanvas() { setBackground(Color.white); setToolTipText("click in this window to display greeting"); setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); } public void paintComponent(Graphics g) { super.paintComponent(g); // paint background g.drawString("Click to display \"Hello CS 585!\"", 20, 50); } // Draw without using paintComponent or paint public void showHelloString(int x, int y) { Graphics g = getComponentGraphics(getGraphics()); g.drawString("Hello CS 585!", x, y); } } class MyCanvasMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent event) { Point place = event.getPoint(); canvas.showHelloString(place.x, place.y); } } // Create and layout button panel // could also Box instead of JPanel class ThreeButtonPanel extends JPanel implements ActionListener, MouseListener { private JButton redB = new JButton("Red"); private JButton blueB = new JButton("Blue"); private JButton greenB = new JButton("Green"); public ThreeButtonPanel () { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // set size, alignment, tooltip, cursor, listeners for buttons. float align = Component.CENTER_ALIGNMENT; setButton(redB, "red", 70, 30, align); setButton(blueB, "red", 70, 30, align); setButton(greenB, "red", 70, 30, align); add(Box.createVerticalGlue()); add(redB); // rigidArea used to space vertical, and set horizontal width add(Box.createRigidArea(new Dimension (100,10))); add(blueB); add(Box.createRigidArea(new Dimension (100,10))); add(greenB); add(Box.createVerticalGlue()); setBackground(new Color(0, 255,255)); } private void setButton(JButton b, String color, int width, int height, float align) { b.setMaximumSize(new Dimension(width, height)); b.setAlignmentX(align); b.setToolTipText("change greeting color to " + color); b.setCursor(new Cursor(Cursor.HAND_CURSOR)); b.addActionListener(this); b.addMouseListener(this); } public void actionPerformed(ActionEvent event) { JButton aButton = (JButton) event.getSource(); if(aButton == redB) canvas.setForeground(Color.red); else if (aButton == blueB) canvas.setForeground(Color.blue); else canvas.setForeground(Color.green); } public void mouseEntered(MouseEvent me) { JButton aButton = (JButton) me.getComponent(); if(aButton == redB) redB.setBackground(Color.red); else if (aButton == blueB) blueB.setBackground(Color.blue); else greenB.setBackground(Color.green); } public void mouseExited(MouseEvent me) { JButton aButton = (JButton) me.getComponent(); if(aButton == redB) redB.setBackground(Color.lightGray); else if (aButton == blueB) blueB.setBackground(Color.lightGray); else greenB.setBackground(Color.lightGray); } // implement dummies since MouseListener was implemented. public void mouseClicked(MouseEvent me) {} public void mousePressed(MouseEvent me) {} public void mouseReleased(MouseEvent me) {} } } // end of Hello585Swing class