import java.awt.*; import java.awt.event.*; import java.applet.*; public class Opdracht13_04 extends Applet { Button b1; Panel p1; Dobbelsteen d1; public void init() { setLayout(new BorderLayout()); b1 = new Button("Gooi"); p1 = new Panel(); d1 = new Dobbelsteen(50, 10); b1.addActionListener(new ButtonHandler()); p1.add(b1); add(p1, BorderLayout.SOUTH); add(d1, BorderLayout.CENTER); } public class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { d1.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 update(Graphics g) { paint(g); } 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; } } }