L23 - Computer Science 101 SS C Lecture 23 Contents! 2!...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci101 Lecture 24 1 Computer Science 101 SS C Lecture 23 Contents Drawing dice with random values in a JPanel. Non GUI programs - program execution. Event based programs - program execution. What are ActionEvents. When do ActionEvents happen? Two examples using ActionEvents. Course Book: §16 1 2 3 4 5 6 7 8 - 17 2 Dice class defnition public class Dice { private int value; public Dice(int value) { this.value = value; } public void drawDice( Graphics g , int x, int y) { import java.awt.*; final int SIZE_DIE = 100; final int SIZE_SPOT = 20; final int GAP = (SIZE_DIE-3*SIZE_SPOT)/4; final int POS_1_LEFT = x + GAP; final int POS_2_LEFT = x + SIZE_SPOT + 2*GAP; final int POS_3_LEFT = x + 2*SIZE_SPOT + 3*GAP; final int POS_1_TOP = y + GAP; final int POS_2_TOP = y + SIZE_SPOT + 2*GAP; final int POS_3_TOP = y + 2*SIZE_SPOT + 3*GAP; 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 Dice class defnition g.drawRect(x, y, SIZE_DIE, SIZE_DIE); g.drawRect(x+1, y+1, SIZE_DIE-2, SIZE_DIE-2); if (value == 1 || value == 3 || value == 5) { g.fillOval(POS_2_LEFT, POS_2_TOP, SIZE_SPOT, SIZE_SPOT); } if (value != 1) { g.fillOval(POS_1_LEFT, POS_1_TOP, SIZE_SPOT, SIZE_SPOT); g.fillOval(POS_3_LEFT, POS_3_TOP, SIZE_SPOT, SIZE_SPOT); } if (value > 3) { g.fillOval(POS_1_LEFT, POS_3_TOP, SIZE_SPOT, SIZE_SPOT); g.fillOval(POS_3_LEFT, POS_1_TOP, SIZE_SPOT, SIZE_SPOT); } if (value == 6) { g.fillOval(POS_1_LEFT, POS_2_TOP, SIZE_SPOT, SIZE_SPOT); g.fillOval(POS_3_LEFT, POS_2_TOP, SIZE_SPOT, SIZE_SPOT); } } } 4 DiceJPanel1 defnition 1 2 3 4 5 6 7 8 9 10 11 12 13 public class DiceJPanel1 } private Dice dice1, dice2; public DiceJPanel1() { } public void paintComponent(Graphics g) { } super.paintComponent(g); dice1 = new Dice((int)(Math.random() * 6 + 1)); dice2 = new Dice((int)(Math.random() * 6 + 1)); import java.awt.*; import javax.swing.*; dice1.drawDice(g, 20, 70); dice2.drawDice(g, 50, 200); extends JPanel { NOTE: this example is NOT the correct way to handle events. 5 Ex01 - DiceJPanel1 Refer to the DiceJPanel1 class shown previously. This JPanel uses the class Dice. In the DiceJPanel1 class: a) Where are the Dice instances declared? ____ b) Where are the Dice instances created? ____ c) Name an instance method of the Dice class ____ _____________________________________________ d) Write the signature for that instance method ____ ______________________________________________ 6 Displaying dice throws There are four classes deFned for the application showing the dice throws. What is the purpose of each of these classes. Three example outputs from the DiceJPanel1. The JPanel class The J±rame class The Application class The Dice class
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CompSci101 Lecture 24 2 7 Displaying dice throws There are four classes deFned for the application showing the dice throws.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 6

L23 - Computer Science 101 SS C Lecture 23 Contents! 2!...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online