Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lab 7 App Development Using Java Swing, Collections, and IO Frameworks We have learned various features from the Java API so far. It’s time for us try to use them in developing some interesting applications. We have succeeded in Project 2 by creating a GUI that can animate the moving and stopping of a curling rock. We will extend it into a team curling game animation, with a window as shown below. Application Context The extended GUI included titled panels (TitledBorder) displaying teams (Collections) of players with the rocks the players use. Players can be added by clicking on the headshot placeholder and selecting an image file (FileChooser and MouseAdapter) , while rock colors can be customized using a ColorChooser dialog box. In addition, a custom icon can be used to replace the default Java icon. Programming Exercises We will look up the Java API online as we go, and get some sample code as needed from the tutorials on Oracle website. 0. A Window with an Icon of Your Choice. The setIconImage method. 1. A Default Icon with an Image of Your Design. The Icon interface. 2. A Default Icon with an Image of Your Design. The Icon interface. 3. A Player Panel with the Default Icon from Last Task. The setIcon method of the JLabel class. 4. Responding to Mouse Clicks: on Left or Right Buttons. The mousePressed method of the MouseAdapter class. 5. Choose a Color from a Dialog Box. The showDialog method of the ColorChooser class. 6. Choose a File and Set It on Player Panel. The FileChooser class and the BufferedImage class. /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package curling; /** * * @author zhao_m */ import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; import import import import import import import import javax.swing.Icon; javax.swing.ImageIcon; javax.swing.JColorChooser; javax.swing.JFileChooser; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JPanel; javax.swing.border.TitledBorder; public class PlayerPanel extends JPanel { private JLabel pictureLabel; private JLabel rockLabel; private String name; public PlayerPanel(String name) { this.name = name; this.setBorder(new TitledBorder(name)); this.setSize(100, 250); prepareLabels(); } private void prepareLabels() { pictureLabel = new JLabel(); pictureLabel.setIcon(new DefaultPlayerIcon()); this.add(pictureLabel); pictureLabel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { System.out.println("pressed"); switch (e.getButton()) { case 1: JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); ImageIcon picture = new ImageIcon(file.getPath()); Image originalPicture = picture.getImage(); BufferedImage adjustedPicture = new BufferedImage(100, 140, BufferedImage.TYPE_BYTE_GRAY); adjustedPicture.getGraphics(). drawImage(originalPicture, 0, 0, 100, 140, null); System.out.println(file.getPath()); JFrame testFrame = new JFrame("Test"); testFrame.add(new JLabel(picture)); testFrame.pack(); testFrame.setVisible(true); pictureLabel.setIcon(new ImageIcon(adjustedPicture)); PlayerPanel.this.repaint(); } break; case 3: Color newColor = JColorChooser.showDialog(null, "Choose a Color..", Color.LIGHT_GRAY); JFrame testFrame = new JFrame("Test"); testFrame.add(new JLabel(new DefaultPlayerIcon(newColor))); testFrame.pack(); testFrame.setVisible(true); break; default: break; } } }); } public static void main(String[] args) { JFrame f = new JFrame("Player Test"); f.add(new PlayerPanel("Bob")); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } class DefaultPlayerIcon implements Icon { Color faceColor = Color.LIGHT_GRAY; DefaultPlayerIcon() { } DefaultPlayerIcon(Color faceColor) { this.faceColor = faceColor; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 140; } @Override public int getIconWidth() { // TODO Auto-generated method stub return 100; } @Override public void paintIcon(Component arg0, Graphics g, int arg2, int arg3) { // TODO Auto-generated method stub g.setColor(faceColor); g.fillOval(20, 20, 60, 100); } } }