eclipse - JAVA BEGINNERS Create and roll Dice -


i want create dice rolling simulator. i've gotten far.

public class rolldice {     private static class rolldice extends jpanel {         public void paintcomponent(graphics g) {             super.paintcomponent(g);             // custom draw code goes here         }     }      public static void main(string[] args) {          jlabel message = new jlabel ("\nroll die!\n", jlabel.center);          message.setforeground(color.black);          message.setbackground(color.white);          message.setfont(new font("courier", font.plain, 25));         message.setopaque(true);          jbutton roll = new jbutton("roll");          jpanel content = new jpanel();          content.setlayout(new borderlayout());          content.add(message, borderlayout.north);         content.add(roll, borderlayout.south);          jframe window = new jframe("roll dice");          window.setcontentpane(content);          window.setsize(250,300);          window.setlocation(300,300);          window.setvisible(true);     } } 

i have gotten jframe, jlabel, , button says roll, simple things.

what trying figure out how create 2 dice in jpanel, , how make roll when button "roll" clicked, using math.random , graphics.

i appreciate if simple possible, since not advanced in programming world , started. appreciate if try explain detailed possible before give me answer, have chance try , figure out myself beforehand.

thanks!

like fd. said, need components die. let's use jlabel components. can arranged this:

+==============================+ |        roll die!         | |   +---------+  +---------+   | |   |         |  |         |   | |   |  dice1  |  |  dice2  |   | |   |         |  |         |   | |   +---------+  +---------+   | | +--------------------------+ | | |           roll           | | | +--------------------------+ | +==============================+ 

you can set imageicon on label (check out: java: how add image jlabel?), create 6 images of different positions of dice. when button pushed, random number (between 1 , 6) generated (using math.random). each number represents image. set image of jlabel based on number.

for happen need actionlistener. create custom actionlistener below (note did 1 die):

public class rolldiceactionlistener implements actionlistener {      private jlabel dice;      public rolldiceactionlistener(jlabel dice) {         this.dice = dice;     }      @override     public void actionperformed(actionevent e) {         int rnd = (int)(math.random() * 6) + 1;          switch (rnd) {         case 1:             dice.seticon(new imageicon("/path/to/dice_1.png"));             break;         case 2:             dice.seticon(new imageicon("/path/to/dice_2.png"));             break;         case 3:             dice.seticon(new imageicon("/path/to/dice_3.png"));             break;         case 4:             dice.seticon(new imageicon("/path/to/dice_4.png"));             break;         case 5:             dice.seticon(new imageicon("/path/to/dice_5.png"));             break;         case 6:             dice.seticon(new imageicon("/path/to/dice_6.png"));             break;         }     } }  

each time button pressed, actionperformed method invoked , randomly change icons of each jlabel, simulating roll of die.

to add custom actionlistener button:

roll.addactionlistener(new rolldiceactionlistener(die)); 

the actionlistener needs modify jlabels representing dice, don't forget add parameter constructor of listener.

hope helps. luck!


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -