import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ImplementatieDobbelsteen extends JFrame {
public static void main(String[] args) {
JFrame frame = new ImplementatieDobbelsteen();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setResizable(false);
frame.setContentPane(new Paneel());
frame.setVisible(true);
}
}
class Paneel extends JPanel {
Dobbelsteen dobbelsteen1, dobbelsteen2; //dobbelstenen maken
JButton gooi;
public Paneel(){
dobbelsteen1 = new Dobbelsteen();
gooi = new JButton("Gooi");
gooi.addActionListener(new GooiHandler());
gooi.setBounds(getVisibleRect()); //in het midden zetten
add(gooi);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int a = dobbelsteen1.getAantal(); //a krijgt de waarde van dobbelsteen
g.drawRect(100, 100, 100, 100);
/*a word gebruikt voor de ogen van de dobbelsteen*/
if(a == 1) {
g.fillOval(140, 140, 20, 20);
}
if(a == 2) {
g.fillOval(120, 120, 20, 20);
g.fillOval(160, 160, 20, 20);
}
if(a == 3) {
g.fillOval(120, 120, 20, 20);
g.fillOval(140, 140, 20, 20);
g.fillOval(160, 160, 20, 20);
}
if(a == 4) {
g.fillOval(120, 120, 20, 20);
g.fillOval(160, 120, 20, 20);
g.fillOval(120, 160, 20, 20);
g.fillOval(160, 160, 20, 20);
}
if(a == 5) {
g.fillOval(140, 140, 20, 20);
g.fillOval(120, 120, 20, 20);
g.fillOval(160, 120, 20, 20);
g.fillOval(120, 160, 20, 20);
g.fillOval(160, 160, 20, 20);
}
if(a == 6) {
g.fillOval(120, 115, 20, 20);
g.fillOval(120, 140, 20, 20);
g.fillOval(120, 165, 20, 20);
g.fillOval(160, 115, 20, 20);
g.fillOval(160, 140, 20, 20);
g.fillOval(160, 165, 20, 20);
}
}
class GooiHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
dobbelsteen1.gooi();
repaint();
System.out.println(dobbelsteen1.getAantal());
}
}
}