0

alright, so basically i have created 5 jbuttons, each of these should display an image randomly when the throw button is pressed.. the following code below shows what i beleive are my 6 die classes with the random image generator code...

import javax.swing.*;
import java.util.*;
public class Dice {

    Die die1 = new Die();
    Die die2 = new Die();
    Die die3 = new Die();
    Die die4 = new Die();
    Die die5 = new Die();
    Die die6 = new Die();

    public void someFunc() {
        die1.setImage("1.png");
        die2.setImage("2.png");
        die3.setImage("3.png");
        die4.setImage("4.png");
        die5.setImage("5.png");
        die6.setImage("6.png");
    }

    public void roll() {
        ArrayList list = new ArrayList();
        Random posGen = new Random(5);

        for (int i = 0; i < 5; i++)
        switch (1 + posGen.nextInt()) {
            case 1:
                list.add(die1);
                break;
            case 2:
                list.add(die2);
                break;
            case 3:
                list.add(die3);
                break;
            case 4:
                list.add(die4);
                break;
            case 5:
                list.add(die5);
                break;
            case 6:
                list.add(die6);
                break;
        }

    }

}

so let me explain again, the above class created 6 instances of die objects. i then assigned each die to an image. then the roll method should add a random image into the array list..

now the following is my GUI code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Coursework2 {

    Dice mydice = new Dice();

    class MyActionListener implements ActionListener {

        JFrame frame;
        JButton die1;

        MyActionListener(JFrame f) {
            frame = f;

        }
        public void actionPerformed(ActionEvent e) {


            //die1.setIcon(null);

        }
    }
    public void gui() {

        JFrame frame = new JFrame("SimpleSwingExample");
        JPanel buttons = new JPanel();
        buttons.setLayout(null);
        JButton button = new JButton("throw the fucking die");
        frame.setContentPane(buttons);
        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton die1 = new JButton("die 1");
        JButton die2 = new JButton("die 2");
        JButton die3 = new JButton("die 3");
        JButton die4 = new JButton("die 4");
        JButton die5 = new JButton("die 5");

        button.addActionListener(new MyActionListener(frame));

        frame.getContentPane().add(die1);
        frame.getContentPane().add(die2);
        frame.getContentPane().add(die3);
        frame.getContentPane().add(die4);
        frame.getContentPane().add(die5);

        buttons.setBackground(Color.blue);

        die1.setBounds(200, 50, 200, 200);
        die2.setBounds(400, 50, 200, 200);
        die3.setBounds(600, 50, 200, 200);
        die4.setBounds(800, 50, 200, 200);
        die5.setBounds(1000, 50, 200, 200);

        button.setBounds(10, 300, 200, 100);

        frame.setSize(1500, 800);
        frame.setVisible(true);

    }


}

alright, now what i want to achieve is, when the jbutton labeled "throw" is clicked, each of the jbuttons labeled die 1-6 that i have created should then display an image at random. get it?

how can i do this, i need explanation with code...

thanks .

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Azzie
  • 49
  • 7
  • 5
    *i need explanation with code...* And we need **your** code properly indented whenever you add a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – Frakcool Dec 11 '15 at 18:42
  • yes sorry about that, i will indent it next time – Azzie Dec 11 '15 at 18:52
  • 3
    Or, you could edit it and indent it now. – GrizzlyManBear Dec 11 '15 at 18:55
  • 2
    No, don't leave it for the next time. Read the link I provided and post your Running Code here and indent it when you do so – Frakcool Dec 11 '15 at 18:55
  • why do you need it indented anyway ? its set out so clearly and you can obviously read it...plz just solve the problem – Azzie Dec 11 '15 at 18:57
  • 2
    *why do you need it indented anyway ? its set out so clearly and you can obviously read it* - Why do you need it to display a random image anyway? It's fine that way and you can obviously solve it your self... *plz just solve the problem* - Please just post your [Running Example](http://stackoverflow.com/help/mcve) and indent your code when you do it. We want to help you, please, help us to help you. Without a Running and Readable code we can't do much. We're volutiers, if you want someone to write the code for you, hire someone then. – Frakcool Dec 11 '15 at 19:07
  • Use your IDE to format the code for you. It's not set out clearly at all with tons of unneeded spaces. – user1803551 Dec 11 '15 at 19:13
  • Or an [online formatter](http://codebeautify.org/javaviewer). We want to help you solving your problem, make it easier for us. We want to help you, not having the problem to indent the code for you. – Frakcool Dec 11 '15 at 19:15
  • 3
    `why do you need it indented anyway ?` - because that is the way code should be written. Code should be written so it is readable and easy to maintain. There is no excuse for saying I will write readable code the next time. It is something you should do every time. The code is not set out clearly. There are plenty of blank lines making it hard to read all the code without scrolling to see what the code does. If you want people to help you and read your code, then follow the standards used by people in the programming industry. – camickr Dec 11 '15 at 19:18
  • but do you even know how to help me with my problem? its not that difficult to scroll down..... – Azzie Dec 11 '15 at 19:28
  • 2
    *but do you even know how to help me with my problem?* But do you even know [how to ask a good question](http://stackoverflow.com/help/how-to-ask) It's not that difficult to read and maybe [Take the tour](http://stackoverflow.com/tour) Please again, check the links provided and post a [Running Example](http://stackoverflow.com/help/mcve) which contains the minimum code so we can copy-paste and see the error you see (Or in this case your GUI) so we can help you to adapt it to do whatever you want. And indent your code (Don't forget it) – Frakcool Dec 11 '15 at 19:30
  • 1
    @Azzie Its even easier to fix the code so we don't have to scroll down. I see your last question also got put on "hold", so I see you haven't learned anything about asking a reasonable question. Good luck. – camickr Dec 11 '15 at 19:33
  • i am new to this website, can someone give me a break? – Azzie Dec 11 '15 at 19:38
  • 1
    Welcome to StackOverflow, please read the 3 links I provided in my last comment, follow them up and you'll get faster and better responses if you follow them up. Until then, good luck :) If you ever decide to edit your post and make it a good question, tag me with @ Frakcool (Remove the space between @ and F), if it has improved I'll try to help you – Frakcool Dec 11 '15 at 19:42
  • ok i think its been improved, i tried to save as much space as i could. – Azzie Dec 11 '15 at 19:44
  • 2
    With the updated code I can see 1) You're using a null layout, this isn't encouraged, instead you should use a [Layout Manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) or combinations of them. For more about this [Null Layout is Evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool Dec 11 '15 at 19:46
  • ok , the null layout isnt really a major problem for me right now, thats just how its working ....the real problem is getting random images to pop up on each die face – Azzie Dec 11 '15 at 20:05
  • Working on it. Right now might not be a big problem, but avoid it's use in the future. – Frakcool Dec 11 '15 at 20:08
  • `i am new to this website` what does that have to do with anything. You have been given advice on how to improve your question. It will take you 2 minutes to format the code and repost it.,`can someone give me a break?` - you make your own breaks. All you need to do is listen to advice you have been given update the code. Why are you being so stubborn? – camickr Dec 11 '15 at 21:08

1 Answers1

3

You shouldn't use a null layout. Instead you can get a nice view with Layout Managers. You can read more about why this isn't encouraged here and here

enter image description here

Things to consider:

  • This program doesn't validate if random numbers are the same
  • The images must be all png format
  • The program at start has no images, but is up to you to change it.

The code that produces the above output is:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.util.Random;

public class CourseWork2 {
    JFrame frame;
    JButton b1, b2, b3, b4, b5, randomButton;
    JPanel buttonPane, randomPane;

    public void generateRandomImages() {
        try {
            Random rand = new Random();
            int randomNum = 0;
            int numbers[] = new int[5];
            for (int i = 0; i < 5; i++) {
                randomNum = rand.nextInt((5 - 1) + 1) + 1;
                numbers[i] = randomNum;
            }

            Image img = ImageIO.read(getClass().getResource(numbers[0] + ".png"));
            b1.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[1] + ".png"));
            b2.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[2] + ".png"));
            b3.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[3] + ".png"));
            b4.setIcon(new ImageIcon(img));

            img = ImageIO.read(getClass().getResource(numbers[4] + ".png"));
            b5.setIcon(new ImageIcon(img));

            frame.pack();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    CourseWork2() {
        frame = new JFrame("Random Images");
        b1 = new JButton();
        b2 = new JButton();
        b3 = new JButton();
        b4 = new JButton();
        b5 = new JButton();
        randomButton = new JButton("Clic for random images");

        buttonPane = new JPanel();
        randomPane = new JPanel();

        buttonPane.setLayout(new FlowLayout());
        randomPane.setLayout(new FlowLayout());

        buttonPane.add(b1);
        buttonPane.add(b2);
        buttonPane.add(b3);
        buttonPane.add(b4);
        buttonPane.add(b5);
        randomPane.add(randomButton);

        randomButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                generateRandomImages();
            }
        });

        frame.add(buttonPane, BorderLayout.PAGE_START);
        frame.add(randomPane, BorderLayout.CENTER);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new CourseWork2();
    }
}
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • thanks, this will definitely help with my assignment :) – Azzie Dec 11 '15 at 21:57
  • @Azzie I'm I'm glad it helped, if the answer solved your problem, feel free to [accept it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Frakcool Dec 12 '15 at 05:22
  • it should show images from 1-6 of the dice, also when i add my own images, it doesnt work ? – Azzie Dec 13 '15 at 19:05
  • dont worry i got it to work. now i am going to create 5 more images for the computer ... can you show me how i would add the score for each of the 5 dice rolled? for example, when i press the throw button, the 5 random images will be displayed...then whatever the values are shown on the dice, there is also then going to be a score button and that adds all the 5 dice values together – Azzie Dec 13 '15 at 19:51
  • @Azzie That's your homework, I won't do it for you. Your question was about how to add random images, that's what I did. If you have any more questions please ask them in separate questions. – Frakcool Dec 13 '15 at 19:57
  • they wont allow me to ask more questions, please help – Azzie Dec 13 '15 at 20:00
  • @Azzie why they won't allow you to ask more questions? Show what you have tried I'm not gonna do all **your** homework as I said. Ask another question with what you have tried. This question was about how to add random images, not how to add a score – Frakcool Dec 13 '15 at 20:07
  • how do i arrange every component individually as you described using the layout managers?.....i want 5 set of dice to be displayed in one position and 5 others to be displayed below, with the roll button placed at the side – Azzie Dec 13 '15 at 23:46
  • Please post an image of what you mean. – Frakcool Dec 14 '15 at 00:34