{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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 definition 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 definition 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); } } } DiceJPanel1 definition 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 defined 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 JFrame class The Application class The Dice class
Image of page 1

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

View Full Document Right Arrow Icon
CompSci101 Lecture 24 2 7 Displaying dice throws There are four classes defined for the application showing the dice throws. Q. When does a change of display happen?
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern