0

I'm trying to make a gui program that has a button, and when you click it, you roll a die from 1 to 6 and an image of the die comes up. But when I try to generate a number between 1 and 6, an errors saying symbol cannot be found comes up.

int rolledNumber = random.nextInt(max - min + 1) + min;

Ii already declared max and min as 6 and 1.

I'm also getting errors at the part where I try to get a random int between 1 and 6. And the code I use to open the image seems to be wrong.

`

img = ImageIO.read(new File("dice 1.jpeg"));

Here's my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;



public class Butttin {  
    public static void main(String[] args) {    
        JFrame frame = new JFrame("Rolling Dice Game");
        JPanel panel = new JPanel();
        JButton buttonRoll = new JButton("Roll!");

        buttonRoll.addActionListener(new buttonRoll()); 

        panel.setLayout(new GridLayout(5, 2, 5, 5));
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
    }

    private static class buttonRoll implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            int max = 6;
            int min = 1;
            int rolledNumber = random.nextInt(max - min + 1) + min;
            String command  = event.getActionCommand();
            if (command == "Roll!") {
                if (rolledNumber == 1) {
                    JLabel die = new JLabel();
                    img = ImageIO.read(new File("dice 1.jpeg"));
                } else if (rolledNumber == 2){
                    JLabel die = new JLabel();
                    img = ImageIO.read(new File("dice 2.jpg"));
                } else if (rolledNumber == 3){
                    JLabel die = new JLabel();
                    img = ImageIO.read(new File("dice 3.jpg"));
                } else if (rolledNumber == 4){
                    JLabel die = new JLabel();
                    img = ImageIO.read(new File("dice 4.jpg"));
                } else if (rolledNumber == 5){
                    JLabel die = new JLabel();
                    img = ImageIO.read(new File("dice 5.jpg"));
                } else if (rolledNumber == 6){
                    JLabel die = new JLabel();
                }
                return die; 
            }
        }
    }
}
Virgo
  • 37
  • 1
  • 9
  • 1
    Not related to your original query but please read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PakkuDon Feb 20 '15 at 01:16
  • And *which* symbol does it say can't be found? – user253751 Feb 20 '15 at 01:21

1 Answers1

2

random is not a valid class or variable, I think you mean java.util.Random, which you will need to declear a variable and create an instance of...

private static class ButtonRoll implements ActionListener {

    private java.util.Random random = new java.util.Random();

Next, if (command == "Roll!") { is not how you compare Strings in Java, you should be using String#equals, for example if ("Roll!".equals(command)) {

Next, img = ImageIO.read(new File("dice 1.jpeg")); suffers, partly from the same issue. img is undefined. Also, ImageIO.read throws a IOException which you are not handling. See the JavaDocs for specifics

Next, you are declaring die within a local context to the if statements

            if (rolledNumber == 1) {
                JLabel die = new JLabel();
                img = ImageIO.read(new File("dice 1.jpeg"));
            } else if (rolledNumber == 2) {
                JLabel die = new JLabel();

This means that when you try and use returndie,die` is undefined. Make sure you declare at a context level you want to use it at...

        int rolledNumber = random.nextInt(max - min + 1) + min;
        String command = event.getActionCommand();
        JLabel die = null;
        if ("Roll!".equals(command)) {
            if (rolledNumber == 1) {
                die = new JLabel();
                img = ImageIO.read(new File("dice 1.jpeg"));
            } else if (rolledNumber == 2) {

Mind you, I don't think you want to be creating a JLabel, but instead, changing a pre-existing label's icon property...

And lastly, ActionListener#actionPerformed does not define a return type (it's defined as void) which means return die; is an illegal statement...

You're going to have to figure out some way to allow the buttonRoll class to update the UI

Also, why I'm on the roll of dissecting your code, you really should make sure that you only ever create and modify your UI from within the context of the Event Dispatching Thread, see Initial Threads for more details

Because I know it's coming...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Butttin {

    public static void main(String[] args) {
        new Butttin();
    }

    public Butttin() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel output;
        private JButton roll;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            output = new JLabel("-");
            add(output, gbc);

            roll = new JButton("Roll");
            add(roll, gbc);

            roll.addActionListener(new RollActionListener(output));

        }

    }

    public class RollActionListener implements ActionListener {

        private JLabel label;
        private Random random = new Random();

        public RollActionListener(JLabel label) {
            this.label = label;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int max = 6;
            int min = 1;
            int rolledNumber = random.nextInt(max - min + 1) + min;

            label.setText(Integer.toString(rolledNumber));
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366