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();  
    }  
}

Recommended Answers

All 3 Replies

Your GUI problem aside, you've got some code design issues to deal with. Why is your button class (not going to get started on the Java Naming Conventions either) a JFrame? It's not. Your random class is not a JPanel... These are things you need to fix before you start worrying about the actual gameplay.

This is the code and i have no problem i'n its execution.Can you help me for what i am asking

I want to move from one button to an other depending on the roll of the dice

Can you explain what you mean by "move"?
What is a user supposed to do when using your program?
What is the program supposed to do?

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.