I have the following code and I want to move from one button to an other depending on the roll of the dice,for example if the dice rolls 5 i want to go to (1+5) six and to put a diferent color to it and so on.Can you help me.
button.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
class RollDice extends JPanel {
JPanel p1 = new JPanel();
private random dice = new random();
public RollDice() {
JButton rollButton = new JButton("Roll");
rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24));
rollButton.addActionListener(new RollListener());
p1.add(rollButton);
p1.add(dice);
}
private class RollListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dice.roll();
}
}
}
class random extends JPanel {
private int value;
private int diam = 9;
private static Random random = new Random();
/*\* Initialize background to 60x60 pixels. Initial roll.*/
public random() {
setPreferredSize(new Dimension(60,60));
roll();
}
public int roll() {
int val = random.nextInt(6) + 1;
setValue(val);
return val;
}
public void setValue(int spots) {
value = spots;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
switch (value) {
case 1: drawSpot(g, w/2, h/2);
break;
case 3: drawSpot(g, w/2, h/2);
case 2: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
break;
case 5: drawSpot(g, w/2, h/2);
case 4: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
break;
case 6: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
drawSpot(g, w/4, h/2);
drawSpot(g, 3*w/4, h/2);
break;
}
}
private void drawSpot(Graphics g, int x, int y) {
g.fillOval(x-diam/2, y-diam/2, diam, diam);
}
}
public class button extends JFrame
{
RollDice r = new RollDice();
JButton button[]=new JButton[100];
JPanel p2=new JPanel();
public button()
{
p2.setPreferredSize(new java.awt.Dimension(900, 400));
r.p1.add(p2);
for(int i=0;i<button.length;i++)
{
button[i]= new JButton(" " + Integer.toString(i+1));
button[i].setPreferredSize(new java.awt.Dimension(60, 30));
p2.add(button[i]);
}
Container c=getContentPane();
c.add(r.p1);
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000,400);
setVisible(true);
}
public static void main(String[]args)
{
new button();
}
}