Question: I have this almost completed, my code its producing a gui that changes the image randomly for the computer and for the user it changes

I have this almost completed, my code its producing a gui that changes the image randomly for the computer and for the user it changes it on button selection. My issue its that i cant figure out a method to change the result label.

CLASS CREATES GUI

public class GameGui extends JFrame { JLabel lblResult; JButton btnImageRight; JButton btnImageLeft; rockPaperScissors rock = rockPaperScissors.ROCK; rockPaperScissors scissors = rockPaperScissors.SCISSORSS; rockPaperScissors paper = rockPaperScissors.PAPER; private JPanel contentPane; private JPanel panel_images_1;

/** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { GameGui frame = new GameGui(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

/** * Create the frame. */ public GameGui() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 600); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel_images = createImagesPanel(); contentPane.add(panel_images, BorderLayout.CENTER); JLabel lblUser = createUserLabel(); lblUser.setHorizontalAlignment(SwingConstants.LEFT); JLabel lblComputer = createComputerLabel(); lblComputer.setHorizontalAlignment(SwingConstants.LEFT); JPanel panelLabels = new JPanel(); contentPane.add(panelLabels, BorderLayout.NORTH); panelLabels.setLayout(new BorderLayout(0, 0)); JLabel lblTttleL = new JLabel("ROCK, PAPER, SCISSORS"); createTittleLabel(lblTttleL); panelLabels.add(lblTttleL, BorderLayout.NORTH); JPanel panelSelection = createSelectionButtonsPanel(); contentPane.add(panelSelection, BorderLayout.SOUTH); }

private JLabel createUserLabel() { JLabel lblUser = new JLabel("USER"); lblUser.setOpaque(true); lblUser.setFont(new Font("Times New Roman", Font.BOLD, 26)); lblUser.setBounds(62, 303, 74, 50); panel_images_1.add(lblUser); return lblUser; }

private JLabel createComputerLabel() { JLabel lblComputer = new JLabel("COMPUTER"); lblComputer.setOpaque(true); lblComputer.setFont(new Font("Times New Roman", Font.BOLD, 26)); lblComputer.setBounds(381, 303, 159, 50); panel_images_1.add(lblComputer); return lblComputer; }

private void createTittleLabel(JLabel lblSomeText) { lblSomeText.setBorder(new EmptyBorder(0, 0, 30, 0)); lblSomeText.setFont(new Font("Sitka Subheading", Font.PLAIN, 28)); lblSomeText.setHorizontalAlignment(SwingConstants.CENTER); }

private JPanel createSelectionButtonsPanel() { JPanel panelSelection = new JPanel(); panelSelection.setBorder(new EmptyBorder(20, 0, 20, 0)); panelSelection.setLayout(new GridLayout(1, 0, 40, 0)); JButton btnRock = new JButton("Rock"); btnRock.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //change the icon to rock image, on the user and //change the icon to something random on computer //change the label winner result to the winner of the combination btnImageLeft.setIcon(rock.getImage()); btnImageRight.setIcon(rockPaperScissors.randomChoice().getImage()); } }); panelSelection.add(btnRock); JButton btnPaper = new JButton("Paper"); btnPaper.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnImageLeft.setIcon(paper.getImage()); btnImageRight.setIcon(rockPaperScissors.randomChoice().getImage()); //change the label result using .setText(); } }); panelSelection.add(btnPaper); JButton btnScissors = new JButton("Scissors"); btnScissors.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnImageLeft.setIcon(scissors.getImage()); btnImageRight.setIcon(rockPaperScissors.randomChoice().getImage()); } }); panelSelection.add(btnScissors); return panelSelection; }

private JPanel createImagesPanel() { panel_images_1 = new JPanel(); panel_images_1.setBackground(new Color(107, 142, 35)); panel_images_1.setBorder(new EmptyBorder(20, 5, 20, 5)); panel_images_1.setLayout(null); btnImageLeft = new JButton(""); btnImageLeft.setBounds(14, 25, 183, 266); btnImageLeft.setIcon(new ImageIcon(GameGui.class.getResource("/a08/images/paperBall.png"))); btnImageLeft.setBackground(new Color(255, 215, 0)); btnImageLeft.setBorder(new MatteBorder(10, 10, 10, 10, (Color) new Color(0, 0, 0))); panel_images_1.add(btnImageLeft); JLabel lblNewLabel = createResultLabel(panel_images_1); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); btnImageRight = new JButton(""); btnImageRight.setBounds(371, 25, 183, 266); btnImageRight.setIcon(new ImageIcon(GameGui.class.getResource("/a08/images/paperBall.png"))); btnImageRight.setBackground(new Color(255, 215, 0)); btnImageRight.setBorder(new MatteBorder(10, 10, 10, 10, (Color) new Color(0, 0, 0))); panel_images_1.add(btnImageRight); return panel_images_1; }

private JLabel createResultLabel(JPanel panel_images) { lblResult = new JLabel("IT'S A TIE!"); lblResult.setBackground(new Color(148, 0, 211)); lblResult.setBounds(212, 99, 144, 33); lblResult.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 26)); panel_images.add(lblResult); return lblResult; } }

CLASS TO LOAD THE IMAGES AND DECIDE THE WINNER

import java.util.Random;

import javax.swing.ImageIcon;

public enum rockPaperScissors { ROCK(new ImageIcon(rockPaperScissors.class.getResource("/a08/images/rock1.png"))), PAPER(new ImageIcon(rockPaperScissors.class.getResource("/a08/images/paperBall.png"))), SCISSORSS(new ImageIcon(rockPaperScissors.class.getResource("/a08/images/scissors.png"))); private ImageIcon image; private rockPaperScissors(ImageIcon image){ this.image = image; } public ImageIcon getImage(){ return image; } /* * decide winner * @return String */ public static String whoWins(){ //return some string to pass into the result label } /** * @return a random enum */ public static rockPaperScissors randomChoice(){ return values()[(int)(Math.random()*values().length)]; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!