/** * CSE 143 * Anthony Messina * Homework 3 * Julia Letchner * Section AE */ import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import uwcse.graphics.*; //Cannot use java.awt.*; as it conflicts with uwcse.graphics.Shape, so I must import objects as needed import java.awt.Container; import java.awt.GridLayout; import java.awt.Component; import java.awt.Color; import java.awt.Font; /** * A (hopefullly) cool program that implements a variety of Swing components, such as buttons, menus, and text fields. */ public class CoolSwing { public static void main(String[] args) { CoolSwingHelper h = new CoolSwingHelper(); } } class CoolSwingHelper { public CoolSwingHelper() { JFrame frame = new JFrame("Cool Swing"); GWindow window = new GWindow("Cool Swing Window", 400, 400); Container content = frame.getContentPane(); //setting up the panels of the main screen JPanel headPanel = getHeadPanel(); JPanel bodyPanel = getBodyPanel(); JPanel legPanel = getLegPanel(); JPanel textPanel = new JPanel(); JTextField field = new JTextField(); field.requestFocus(); textPanel.add(new JLabel("Character's Name:")); textPanel.add(field); textPanel.setLayout(new GridLayout(2,1)); JPanel exitPanel = new JPanel(); exitPanel.add(new QuitButton()); //creating a functioning menu JMenu charMenu = new JMenu("Species"); charMenu.setMnemonic(KeyEvent.VK_S); ButtonGroup charGroup = new ButtonGroup(); JRadioButtonMenuItem rbmi; rbmi = new JRadioButtonMenuItem("Human", true); rbmi.setActionCommand("Human"); charGroup.add(rbmi); charMenu.add(rbmi); rbmi = new JRadioButtonMenuItem("Sheep"); rbmi.setActionCommand("Sheep"); charGroup.add(rbmi); charMenu.add(rbmi); rbmi = new JRadioButtonMenuItem("Cat"); rbmi.setActionCommand("Cat"); charGroup.add(rbmi); charMenu.add(rbmi); JMenu helpMenu = new JMenu("Help"); helpMenu.setMnemonic(KeyEvent.VK_H); helpMenu.add(new FAQItem()); helpMenu.add(new AboutItem()); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); fileMenu.add(new DisplayItem(field, window, headPanel, bodyPanel, legPanel, charGroup)); fileMenu.add(new QuitItem()); JMenuBar menuBar = new JMenuBar(); menuBar.add(fileMenu); menuBar.add(charMenu); menuBar.add(helpMenu); frame.setJMenuBar(menuBar); content.add(headPanel); content.add(bodyPanel); content.add(legPanel); content.add(textPanel); content.add(exitPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); content.setLayout(new GridLayout(5,1)); frame.pack(); frame.setVisible(true); } /* * Creates a panel for the shape and colors of the head */ public JPanel getHeadPanel() { JPanel headPanel = new JPanel(); JPanel headShapePanel = new JPanel(); ButtonGroup headGroup = new ButtonGroup(); JPanel headColorPanel = new JPanel(); headPanel = getGenericPanel("Head", headPanel, headShapePanel, headColorPanel, headGroup); return headPanel; } /* * Creates a panel for the shape and colors of the body */ public JPanel getBodyPanel() { JPanel bodyPanel = new JPanel(); JPanel bodyShapePanel = new JPanel(); ButtonGroup bodyGroup = new ButtonGroup(); JPanel bodyColorPanel = new JPanel(); bodyPanel = getGenericPanel("Body", bodyPanel, bodyShapePanel, bodyColorPanel, bodyGroup); return bodyPanel; } /* * Creates a panel for the shape and colors of the legs */ public JPanel getLegPanel() { JPanel legPanel = new JPanel(); JPanel legShapePanel = new JPanel(); ButtonGroup legGroup = new ButtonGroup(); JPanel legColorPanel = new JPanel(); legPanel = getGenericPanel("Legs", legPanel, legShapePanel, legColorPanel, legGroup); return legPanel; } /* * Helper method that creates panels * @param title * mainPanel panel as a whole * shapePanel the radiobutton panel * colorPanel the checkboxes panel * buttonGroup the radiobutton group of shapes */ public JPanel getGenericPanel(String title, JPanel mainPanel, JPanel shapePanel, JPanel colorPanel, ButtonGroup buttonGroup) { mainPanel.setBorder(BorderFactory.createTitledBorder(title)); //shapes JRadioButton radioButton; shapePanel.add(radioButton = new JRadioButton("Circle", true)); radioButton.setActionCommand("Circle"); buttonGroup.add(radioButton); shapePanel.add(radioButton = new JRadioButton("Rectangle")); radioButton.setActionCommand("Rectangle"); buttonGroup.add(radioButton); shapePanel.add(radioButton = new JRadioButton("Triangle")); radioButton.setActionCommand("Triangle"); buttonGroup.add(radioButton); //colors colorPanel.add(new JCheckBox("Red")); colorPanel.add(new JCheckBox("Blue")); colorPanel.add(new JCheckBox("Green")); mainPanel.add(shapePanel); mainPanel.add(colorPanel); mainPanel.setLayout(new GridLayout(2,1)); return mainPanel; } } /* * Retrieves the users choices while drawing the shapes onto a GWindow */ class DisplayItem extends JMenuItem { public DisplayItem(JTextField name, GWindow window, JPanel headPanel, JPanel bodyPanel, JPanel legPanel, ButtonGroup buttonGroup) { super("Display"); setMnemonic(KeyEvent.VK_D); setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.ALT_MASK + KeyEvent.CTRL_MASK)); final JTextField charName = name; final GWindow canvas = window; final JPanel mainHeadPanel = headPanel; final JPanel mainBodyPanel = bodyPanel; final JPanel mainLegPanel = legPanel; final ButtonGroup speciesGroup = buttonGroup; addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { canvas.erase(); Font f = new Font("Serif", java.awt.Font.BOLD, 30); canvas.add(new TextShape(charName.getText(), 25, 25, f)); if(speciesGroup.getSelection().getActionCommand() == "Human") { canvas.add(makeGenericPart(mainBodyPanel, 200, 200, 100, 125)); canvas.add(makeGenericPart(mainHeadPanel, 200, 110, 50, 100)); canvas.add(makeGenericPart(mainLegPanel, 175, 290, 40, 100)); canvas.add(makeGenericPart(mainLegPanel, 225, 290, 40, 100)); } else if(speciesGroup.getSelection().getActionCommand() == "Sheep") { canvas.add(makeGenericPart(mainLegPanel, 150, 290, 40, 100)); canvas.add(makeGenericPart(mainLegPanel, 250, 290, 40, 100)); canvas.add(makeGenericPart(mainBodyPanel, 200, 225, 200, 100)); canvas.add(makeGenericPart(mainHeadPanel, 100, 200, 100, 75)); } else if(speciesGroup.getSelection().getActionCommand() == "Cat") { canvas.add(makeGenericPart(mainBodyPanel, 200, 225, 100, 225)); canvas.add(makeGenericPart(mainLegPanel, 150, 290, 40, 100)); canvas.add(makeGenericPart(mainLegPanel, 250, 290, 40, 100)); canvas.add(makeGenericPart(mainHeadPanel, 200, 125, 100, 75)); } }}); } /* * Creates a part of the creature * @param mainPanel part of the creature to draw * x x location (center) * y y location (center) * width width of the part * height height of the part * @return Shape part of the creature to be drawn */ public Shape makeGenericPart(JPanel mainPanel, int x, int y, int width, int height) { Shape genericShape = new Oval(); Component[] mainComponents = mainPanel.getComponents(); Component[] mainShapeComponents = ((JPanel)mainComponents[0]).getComponents(); Component[] mainColorComponents = ((JPanel)mainComponents[1]).getComponents(); final ButtonGroup bg = new ButtonGroup(); int r = 0; int g = 0; int b = 0; for(int i = 0; i < mainColorComponents.length; i++) { JCheckBox jcb = (JCheckBox)mainColorComponents[i]; if(jcb.isSelected()) { if (i == 0) { r = 255; } else if (i == 1) { b = 255; } else g = 255; } } Color shapeColor; if(r == 255 && r == b && r == g) { shapeColor = new Color(200, 200, 200); } else { shapeColor = new Color(r, g, b); } for(int i = 0; i < mainShapeComponents.length; i++) { bg.add((JRadioButton)mainShapeComponents[i]); } if(bg.getSelection().getActionCommand() == "Circle") { genericShape = new Oval(x - width / 2, y - height / 2, width, height, shapeColor, true); } else if(bg.getSelection().getActionCommand() == "Rectangle") { genericShape = new Rectangle(x - width / 2, y - height / 2, width, height, shapeColor, true); } else if(bg.getSelection().getActionCommand() == "Triangle") { genericShape = new Triangle(x, y - height / 2, x - width / 2, y + height / 2, x + width / 2, y + height / 2, shapeColor, true); } return genericShape; } } /* * A FAQ for first-time users of the program accessed through the menu */ class FAQItem extends JMenuItem { public FAQItem() { super("FAQ"); setMnemonic(KeyEvent.VK_A); setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK + KeyEvent.ALT_MASK)); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { final JFrame faqFrame = new JFrame("FAQ"); Container faqContent = faqFrame.getContentPane(); JLabel faqTitle = new JLabel("FAQ", SwingConstants.CENTER); JLabel faqQuestion0= new JLabel(" Q: I don't get it. What am I supposed to do?"); JLabel faqAnswer0 = new JLabel(" A: Choose which species you want from the menu, and the shape and color for its head, body and legs, then go to 'File' and click 'Display'. "); JLabel faqQuestion1= new JLabel(" Q: I'm pushing 'Display', but nothing happens. What am I doing wrong?"); JLabel faqAnswer1 = new JLabel(" A: Nothing. Simply move the main screen window to see the paint results."); JLabel faqQuestion2= new JLabel(" Q: My cat doesn't look like a cat. What's the deal?"); JLabel faqAnswer2 = new JLabel(" A: Due to time and knowledge limitations, the cat form is not ideal. It will be upgraded in version 2.0."); JLabel faqQuestion3= new JLabel(" Q: Wow, I've never seen anything cooler in my life! How did you do get it to be so cool?"); JLabel faqAnswer3 = new JLabel(" A: Don't question the coolness. You'll jinx it."); JPanel exitPanel = new JPanel(); JButton exitButton = new JButton("Exit"); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { faqFrame.dispose(); }}); exitPanel.add(exitButton); faqContent.add(faqTitle); faqContent.add(faqQuestion0); faqContent.add(faqAnswer0); faqContent.add(faqQuestion1); faqContent.add(faqAnswer1); faqContent.add(faqQuestion2); faqContent.add(faqAnswer2); faqContent.add(faqQuestion3); faqContent.add(faqAnswer3); faqContent.add(exitPanel); faqContent.setLayout(new GridLayout(10,1)); faqFrame.pack(); faqFrame.setVisible(true); }}); } } /* * Information about the program seen from the menu */ class AboutItem extends JMenuItem { public AboutItem() { super("About"); setMnemonic(KeyEvent.VK_A); setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK + KeyEvent.ALT_MASK)); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { final JFrame aboutFrame = new JFrame("About"); Container aboutContent = aboutFrame.getContentPane(); JLabel aboutTitle = new JLabel("About", SwingConstants.CENTER); JPanel exitPanel = new JPanel(); JButton exitButton = new JButton("Exit"); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { aboutFrame.dispose(); }}); exitPanel.add(exitButton); aboutContent.add(aboutTitle); aboutContent.add(new JLabel(" Course: CSE 143")); aboutContent.add(new JLabel(" Name: Anthony F. Messina ")); aboutContent.add(new JLabel(" Homework: # 3")); aboutContent.add(new JLabel(" TA: Julia Letchner")); aboutContent.add(new JLabel(" Section: AE")); aboutContent.add(new JLabel(" Version: 1.0")); aboutContent.add(exitPanel); aboutContent.setLayout(new GridLayout(8,1)); aboutFrame.pack(); aboutFrame.setVisible(true); }}); } } /* * Useful button to exit the program from the menu */ class QuitItem extends JMenuItem { public QuitItem() { super("Exit"); setMnemonic(KeyEvent.VK_Q); setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK + KeyEvent.ALT_MASK)); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); }}); } } /* * Useful button to exit the program with a mouseclick */ class QuitButton extends JButton { public QuitButton() { super("Exit"); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); }}); } }