rendered paste bodyimport java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Opdracht13_05 extends Applet {
Button b1, b2;
Panel p1;
Dobbelsteen d1, d2;
public void init() {
setLayout(new BorderLayout());
b1 = new Button("Gooi 1");
b2 = new Button("Gooi 2");
d1 = new Dobbelsteen(50, 10);
d2 = new Dobbelsteen(150, 10);
p1 = new Panel();
b1.addActionListener(new ButtonHandler());
b2.addActionListener(new ButtonHandler());
p1.add(b1);
p1.add(b2);
add(p1, BorderLayout.SOUTH);
add(d1, BorderLayout.CENTER);
}
public class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == b1 ){
d1.gooiDobbelsteen();
}
if ( e.getSource() == b2 ) {
d2.gooiDobbelsteen();
}
}
}
}
class Dobbelsteen extends Canvas {
private int aantalOgen = 0;
private int xDice = 0;
private int yDice = 0;
private int widthDice = 80;
private int heightDice = 80;
private int arcWidthDice = 25;
private int arcHeightDice = 25;
private int xEye = 0;
private int yEye = 0;
private int widthEye = 10;
private int heightEye = 10;
private int arcWidthEye = 180;
private int arcHeightEye = 180;
public Dobbelsteen(int xDice, int yDice) {
this.xDice = xDice;
this.yDice = yDice;
}
private void setAantalOgen() {
this.aantalOgen = (int)(6 * Math.random() + 1);
}
protected void gooiDobbelsteen() {
this.setAantalOgen();
repaint();
}
public void paint(Graphics g) {
g.setColor(Color.black);
switch(this.aantalOgen) {
case 1:
g.fillRoundRect(this.xDice, this.yDice, this.widthDice, this.heightDice, this.arcWidthDice, this.arcHeightDice);
g.setColor(Color.red);
this.xEye = this.widthDice / 2 + this.xDice - 5;
this.yEye = (this.yDice + this.heightDice) / 2;
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
break;
case 2:
g.fillRoundRect(this.xDice, this.yDice, this.widthDice, this.heightDice, this.arcWidthDice, this.arcHeightDice);
g.setColor(Color.red);
this.xEye = this.xDice + 10;
this.yEye = this.yDice + 10;
for(int i = 0; i < 2; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
xEye += 50;
yEye += 50;
}
break;
case 3:
g.fillRoundRect(this.xDice, this.yDice, this.widthDice, this.heightDice, this.arcWidthDice, this.arcHeightDice);
g.setColor(Color.red);
this.xEye = xDice + 10;
this.yEye = yDice + 10;
for(int i=0; i < 3; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.xEye += 25;
this.yEye += 25;
}
break;
case 4:
g.fillRoundRect(this.xDice, this.yDice, this.widthDice, this.heightDice, this.arcWidthDice, this.arcHeightDice);
g.setColor(Color.red);
// Zou ook in een lusje kunnen, controleren of i restant heeft. Zo ja: dan bijv. onderaan plaatsen
// Maar twee lusjes is makkelijker te lezen :- )
this.xEye = this.xDice + 10;
this.yEye = this.yDice + 10;
for (int i=0; i < 2; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.xEye += 50;
}
this.xEye = this.xDice + 10;
this.yEye += 50;
for (int i=0; i < 2; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.xEye += 50;
}
break;
case 5:
g.fillRoundRect(this.xDice, this.yDice, this.widthDice, this.heightDice, this.arcWidthDice, this.arcHeightDice);
this.xEye = this.xDice + 10;
this.yEye = this.yDice + 10;
g.setColor(Color.red);
for (int i=0; i < 2; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.xEye += 50;
}
this.xEye = this.xDice + 10;
this.yEye += 50;
for (int i=0; i < 2; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.xEye += 50;
}
this.xEye = this.widthDice / 2 + this.xDice - 5;
this.yEye = (this.yDice + this.heightDice) / 2;
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
break;
case 6:
g.fillRoundRect(this.xDice, this.yDice, this.widthDice, this.heightDice, this.arcWidthDice, this.arcHeightDice);
g.setColor(Color.red);
// Zou ook in een lusje kunnen, controleren of i restant heeft. Zo ja: dan bijv. onderaan plaatsen
// Maar twee lusjes is makkelijker te lezen :- )
this.xEye = this.xDice + 10;
this.yEye = this.yDice + 10;
for (int i=0; i < 3; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.yEye += 25;
}
this.xEye = this.xDice + 60;
this.yEye = this.yDice + 10;
for (int i=0; i < 3; i++) {
g.fillRoundRect(this.xEye, this.yEye, this.widthEye, this.heightEye, this.arcWidthEye, this.arcHeightEye);
this.yEye += 25;
}
break;
default:
g.drawString("Er is nog niet gegooid."+this.aantalOgen, 30, 40);
break;
}
}
}