My code is supposed to show up as a slot machine type background there are images in each lane not activated and when you press spin it randomizes which ones get to be visible and if they are not supposed to be visible they are set as invisible. My problem is when you press spin the images do not show up. When I set them visible normally they do show up. So I believe it to be when i try to set them visible in the if/else statements.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Dimension;
import javax.swing.ImageIcon;
public class Slots1 extends JFrame implements ActionListener
{
JPanel panel;
JButton spin;
int wheelOne = 0;
JLabel ImgSlot1;
JLabel ImgSlot12;
JLabel ImgSlot13;
JLabel ImgSlot14;
JLabel ImgSlot15;
JLabel ImgSlot16;
JLabel ImgSlot17;
public Slots1()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
int wid = (int) width;
int hgt = (int) height;
JFrame frame = new JFrame("sesame chicken");
frame.setSize(wid,hgt);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(null);
panel.setBackground(Color.GRAY);
ImageIcon pic = new ImageIcon("Backr.png");
JLabel layout = new JLabel(pic);
layout.setLocation(0,0);
layout.setSize(1366,768);
ImageIcon Slot1 = new ImageIcon("SLu1.png");
ImageIcon Slot2 = new ImageIcon("SLu2.png");
ImageIcon Slot3 = new ImageIcon("SLu3.png");
ImageIcon Slot4 = new ImageIcon("SLu4.png");
ImageIcon Slot5 = new ImageIcon("SLu5.png");
ImageIcon Slot6 = new ImageIcon("SLu6.png");
ImageIcon Slot7 = new ImageIcon("SLu7.png");
ImgSlot1 = new JLabel(Slot1);
ImgSlot1.setLocation(75,25);
ImgSlot1.setSize(150,500);
JLabel ImgSlot12 = new JLabel(Slot2);
ImgSlot12.setLocation(75,25);
ImgSlot12.setSize(150,500);
JLabel ImgSlot13 = new JLabel(Slot3);
ImgSlot13.setLocation(75,25);
ImgSlot13.setSize(150,500);
JLabel ImgSlot14 = new JLabel(Slot4);
ImgSlot14.setLocation(75,25);
ImgSlot14.setSize(150,500);
JLabel ImgSlot15 = new JLabel(Slot5);
ImgSlot15.setLocation(75,25);
ImgSlot15.setSize(150,500);
JLabel ImgSlot16 = new JLabel(Slot6);
ImgSlot16.setLocation(75,25);
ImgSlot16.setSize(150,500);
JLabel ImgSlot17 = new JLabel(Slot7);
ImgSlot17.setLocation(75,25);
ImgSlot17.setSize(150,500);
ImgSlot1.setVisible(false);
ImgSlot12.setVisible(false);
ImgSlot13.setVisible(false);
ImgSlot14.setVisible(false);
ImgSlot15.setVisible(false);
ImgSlot16.setVisible(false);
ImgSlot17.setVisible(false);
JButton spin = new JButton("SPIN");
spin.setSize(100,50);
spin.setLocation(100, 650);
spin.setBackground(Color.green);
spin.setForeground(Color.red);
panel.add(ImgSlot1);
panel.add(ImgSlot12);
panel.add(ImgSlot13);
panel.add(ImgSlot14);
panel.add(ImgSlot15);
panel.add(ImgSlot16);
panel.add(ImgSlot17);
panel.add(spin);
panel.add(layout);
frame.add(panel);
frame.setVisible(true);
panel.setVisible(true);
spin.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == spin){
Random rand = new Random();
wheelOne = rand.nextInt(7);
}
if (wheelOne == 1){
ImgSlot1.setVisible(true);}
else if (wheelOne == 2){
ImgSlot12.setVisible(true);}
else if (wheelOne == 3){
ImgSlot13.setVisible(true);}
else if ((wheelOne == 4)){
ImgSlot14.setVisible(true);}
else if (wheelOne == 5){
ImgSlot15.setVisible(true);}
else if (wheelOne == 6){
ImgSlot16.setVisible(true);}
else if (wheelOne == 7){
ImgSlot17.setVisible(true);}
if (wheelOne != 1){
ImgSlot1.setVisible(false);}
if (wheelOne != 2){
ImgSlot12.setVisible(false);}
if (wheelOne != 3){
ImgSlot13.setVisible(false);}
if (wheelOne != 4){
ImgSlot14.setVisible(false);}
if (wheelOne != 5){
ImgSlot15.setVisible(false);}
if (wheelOne != 6){
ImgSlot16.setVisible(false);}
if (wheelOne != 7){
ImgSlot17.setVisible(false);}
}
public static void main(String[] args)
{
Slots1 run = new Slots1();
}
}
This is the code I ended up with
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Dimension;
import javax.swing.ImageIcon;
public class Slots1 extends JFrame implements ActionListener
{
JPanel panel;
JButton spin;
JLabel ImgSlot1;
JLabel ImgSlot2;
JLabel ImgSlot3;
JLabel ImgSlot4;
JLabel ImgSlot5;
JLabel ImgSlot6;
JLabel ImgSlot7;
int wheelOne = 1;
public Slots1()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
int wid = (int) width;
int hgt = (int) height;
JFrame frame = new JFrame("sesame chicken");
frame.setSize(wid,hgt);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(null);
panel.setBackground(Color.GRAY);
ImageIcon pic = new ImageIcon("Backr.png");
JLabel layout = new JLabel(pic);
layout.setLocation(0,0);
layout.setSize(1366,768);
ImageIcon Slot1 = new ImageIcon("SLu1.png");
ImageIcon Slot2 = new ImageIcon("SLu2.png");
ImageIcon Slot3 = new ImageIcon("SLu3.png");
ImageIcon Slot4 = new ImageIcon("SLu4.png");
ImageIcon Slot5 = new ImageIcon("SLu5.png");
ImageIcon Slot6 = new ImageIcon("SLu6.png");
ImageIcon Slot7 = new ImageIcon("SLu7.png");
ImgSlot1 = new JLabel(Slot1);
ImgSlot1.setLocation(75,25);
ImgSlot1.setSize(150,500);
ImgSlot2 = new JLabel(Slot2);
ImgSlot2.setLocation(75,25);
ImgSlot2.setSize(150,500);
ImgSlot3 = new JLabel(Slot3);
ImgSlot3.setLocation(75,25);
ImgSlot3.setSize(150,500);
ImgSlot4 = new JLabel(Slot4);
ImgSlot4.setLocation(75,25);
ImgSlot4.setSize(150,500);
ImgSlot5 = new JLabel(Slot5);
ImgSlot5.setLocation(75,25);
ImgSlot5.setSize(150,500);
ImgSlot6 = new JLabel(Slot6);
ImgSlot6.setLocation(75,25);
ImgSlot6.setSize(150,500);
ImgSlot7 = new JLabel(Slot7);
ImgSlot7.setLocation(75,25);
ImgSlot7.setSize(150,500);
ImgSlot1.setVisible(false);
ImgSlot2.setVisible(false);
ImgSlot3.setVisible(false);
ImgSlot4.setVisible(false);
ImgSlot5.setVisible(false);
ImgSlot6.setVisible(false);
ImgSlot7.setVisible(false);
spin = new JButton("SPIN");
spin.setSize(100,50);
spin.setLocation(100, 650);
spin.setBackground(Color.green);
spin.setForeground(Color.red);
panel.add(ImgSlot1);
panel.add(ImgSlot2);
panel.add(ImgSlot3);
panel.add(ImgSlot4);
panel.add(ImgSlot5);
panel.add(ImgSlot6);
panel.add(ImgSlot7);
panel.add(spin);
panel.add(layout);
frame.add(panel);
frame.setVisible(true);
panel.setVisible(true);
spin.addActionListener(this);
System.out.println(spin);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
System.out.println(source);
System.out.println(spin);
System.out.println();
if (source.equals(spin)){
Random rand = new Random();
wheelOne = rand.nextInt(7);}
if (wheelOne == 0){
ImgSlot1.setVisible(true);}
else if (wheelOne != 0){
ImgSlot1.setVisible(false);}
if (wheelOne == 1){
ImgSlot2.setVisible(true);}
else if (wheelOne != 1){
ImgSlot2.setVisible(false);}
if (wheelOne == 2){
ImgSlot3.setVisible(true);}
else if (wheelOne != 2){
ImgSlot3.setVisible(false);}
if ((wheelOne == 3)){
ImgSlot4.setVisible(true);}
else if (wheelOne != 3){
ImgSlot4.setVisible(false);}
if (wheelOne == 4){
ImgSlot5.setVisible(true);}
else if (wheelOne != 4){
ImgSlot5.setVisible(false);}
if (wheelOne == 5){
ImgSlot6.setVisible(true);}
else if (wheelOne != 5){
ImgSlot6.setVisible(false);}
if (wheelOne == 6){
ImgSlot7.setVisible(true);}
else if (wheelOne != 6){
ImgSlot7.setVisible(false);}
}
public static void main(String[] args)
{
Slots1 run = new Slots1();
}
}
The first problem is how you are using the Random class. If you instantiate it again and again and then call random.nextInt(7)
, you will always get the same value back (probably 0).
Instead you need to instantiate it ones in constructor and assign it to an instance variable.
The second problem is that nextInt(7)
doesn't return an int value between 1 and 7 as you assume, but it returns an int value between 0 and 6.