import 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 GridLayout( 3, 1 )); b1 = new Button("Gooi 1"); b2 = new Button("Gooi 2"); p1 = new Panel(); d1 = new Dobbelsteen(50, 10); d2 = new Dobbelsteen(50, 10); b1.addActionListener(new ButtonHandler()); b2.addActionListener(new ButtonHandler()); p1.add(b1); p1.add(b2); add(d1); add(d2); add(p1); } 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(xDice, yDice, widthDice, heightDice, arcWidthDice, arcHeightDice); g.setColor(Color.red); xEye = widthDice / 2 + xDice - 5; yEye = heightDice / 2 + yDice - 5; g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); break; case 2: g.fillRoundRect(xDice, yDice, widthDice, heightDice, arcWidthDice, arcHeightDice); g.setColor(Color.red); xEye = xDice + 10; yEye = yDice + 10; for(int i = 0; i < 2; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); xEye += 50; yEye += 50; } break; case 3: g.fillRoundRect(xDice, yDice, widthDice, heightDice, arcWidthDice, arcHeightDice); g.setColor(Color.red); xEye = xDice + 10; yEye = yDice + 10; for(int i=0; i < 3; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); xEye += 25; yEye += 25; } break; case 4: g.fillRoundRect(xDice, yDice, widthDice, heightDice, arcWidthDice, 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 :- ) xEye = xDice + 10; yEye = yDice + 10; for (int i=0; i < 2; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); xEye += 50; } xEye = xDice + 10; yEye += 50; for (int i=0; i < 2; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); xEye += 50; } break; case 5: xEye = xDice + 10; yEye = yDice + 10; g.fillRoundRect(xDice, yDice, widthDice, heightDice, arcWidthDice, arcHeightDice); g.setColor(Color.red); for (int i=0; i < 2; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); xEye += 50; } xEye = xDice + 10; yEye += 50; for (int i=0; i < 2; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); xEye += 50; } xEye = widthDice / 2 + xDice - 5; yEye = heightDice / 2 + yDice - 5; g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); break; case 6: g.fillRoundRect(xDice, yDice, widthDice, heightDice, arcWidthDice, 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 :- ) xEye = xDice + 10; yEye = yDice + 10; for (int i=0; i < 3; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); yEye += 25; } xEye = xDice + 60; yEye = yDice + 10; for (int i=0; i < 3; i++) { g.fillRoundRect(xEye, yEye, widthEye, heightEye, arcWidthEye, arcHeightEye); yEye += 25; } break; default: g.drawString("Er is nog niet gegooid.", 30, 40); break; } } }