drjava - Java not realizing Int inside if has changed -
my code supposed show slot machine type background there images in each lane not activated , when press spin randomizes ones visible , if not supposed visible set invisible. problem when press spin images not show up. when set them visible show up. believe when try set them visible in 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 code ended 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 how using random class. if instantiate again , again , call random.nextint(7)
, same value (probably 0).
instead need instantiate ones in constructor , assign instance variable.
the second problem nextint(7)
doesn't return int value between 1 , 7 assume, returns int value between 0 , 6.
Comments
Post a Comment