I am trying to generate a random image everytime someone clicks spin. I am not able to generate a random picture. Some help would be really nice. Thanks in advance. Here's my code so far :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class Gui {

    private JFrame frmCarlAlphoncesLab;
    private JLabel _lblNewLabel_1;
    private JLabel _lblNewLabel_2;
    private JLabel _lblNewLabel;
    private JButton btnSpin;

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

    /**
     * Create the application.
     */
    public Gui() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmCarlAlphoncesLab = new JFrame();
        frmCarlAlphoncesLab.setTitle("Carl Alphonce\u2019s Lab 8");
        frmCarlAlphoncesLab.setBounds(100, 100, 450, 300);
        frmCarlAlphoncesLab.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmCarlAlphoncesLab.getContentPane().setLayout(null);

        btnSpin = new JButton("SPIN");
        btnSpin.setBounds(159, 124, 89, 23);
        frmCarlAlphoncesLab.getContentPane().add(btnSpin);

        _lblNewLabel_1 = new JLabel("");
        Image img1 = new ImageIcon(this.getClass().getResource("/Purple.png")).getImage();
        _lblNewLabel_1.setIcon(new ImageIcon(img1));
        _lblNewLabel_1.setBounds(184, 48, 40, 40);
        frmCarlAlphoncesLab.getContentPane().add(_lblNewLabel_1);

        _lblNewLabel_2 = new JLabel("");
        Image img2 = new ImageIcon(this.getClass().getResource("/Green.png")).getImage();
        _lblNewLabel_2.setIcon(new ImageIcon(img2));
        _lblNewLabel_2.setBounds(83, 48, 40, 40);
        frmCarlAlphoncesLab.getContentPane().add(_lblNewLabel_2);

        _lblNewLabel = new JLabel("");
        Image img = new ImageIcon(this.getClass().getResource("/Red.png")).getImage();
        _lblNewLabel.setIcon(new ImageIcon(img));
        _lblNewLabel.setBounds(285, 48, 40, 40);
        frmCarlAlphoncesLab.getContentPane().add(_lblNewLabel);

        EventHandler event = new EventHandler();
        btnSpin.addActionListener(event);

    }

    public class EventHandler implements ActionListener {
        private String[] Images = { "Green.png", "Purple.png", "Red" };
        Random rand = new Random();

        public EventHandler(){

        }
        @Override
        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==btnSpin){
                Integer a = (int) (Math.random() * 3);
                Image myImg = Toolkit.getDefaultToolkit().getImage(Images[a]);
            System.out.println("HERE");
            }
        }

    }

}

Exactly what help do you need?

Need help with the code in event handler

Are you getting random numbers? You should System.out.println() that to see if that is the issue or something else.

I need to generate random pictures when spin button is clicked not numbers

In your function it generates a (hopefully) random number. I noted you need to check that first. Have you?

I need to generate random pictures when spin button is clicked not numbers

Pictures (in digital form) are nothing but numbers. If you are generating random numbers then you can generate random pictures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.