Number repaint public void g final

This preview shows page 18 - 22 out of 34 pages.

number--; } repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); final int DISTANCE_APART = 50; drawCommandAreas(g); int height = 50; int x = 50; int y = 300; for(int i = 0; i < number; i++) { g.drawLine(x, y, x, y - height); height = height + 50; x = x + DISTANCE_APART; } }
Image of page 18

Subscribe to view the full document.

Question/Answer Sheet - 19 - CompSci 101 ID: ........................................................... CONTINUED private void drawCommandAreas(Graphics g) { g.setColor(Color.YELLOW); g.fillRect(AREA1_X, 0, 50, 50); g.fillRect(AREA2_X, 0, 50, 50); g.setColor(Color.BLACK); g.drawRect(AREA1_X, 0, 50, 50); g.drawRect(AREA2_X, 0, 50, 50); g.drawString("1.", AREA1.x + 10, 35); g.drawString("2.", AREA2.x + 10, 35); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} } Note: the grid lines have been drawn in the window to help you. Initially, the JPanel displays a yellow filled square area labelled "1." on the left side of the JPanel and a yellow filled square area labelled "2." on the right side of the JPanel (see the JPanel shown below). a) Show the state of the JPanel after the user has pressed the mouse TWICE inside the filled square area labelled "1." on the left side of the JPanel.
Image of page 19
Question/Answer Sheet - 20 - CompSci 101 ID: ........................................................... CONTINUED b) Continuing from part a) above, show the state of the JPanel after the user has pressed the mouse three more times in the following positions: position 40, 40, position 45, 45, position 60, 10 c) Continuing from parts a) and b) above, show the state of the JPanel after the user has pressed the mouse three more times in the following positions: position 40, 30, position 430, 35, position 430, 30 (9 marks)
Image of page 20

Subscribe to view the full document.

Question/Answer Sheet - 21 - CompSci 101 ID: ........................................................... CONTINUED Question 10 (10 marks) The JPanel defined below contains the following components: promptT a JTextField which is initially empty, guessT a JTextField which will be used by the user to enter their guess, nextB a JButton displaying the String, "NEXT". checkB a JButton displaying the String, "CHECK", scoreT a JTextField which initially displays the String, "0 / 0". As well, the JPanel has three instance variables: computerNumber stores a random number between 0 and 2, userScore stores the number of correct guesses the user has made so far, totalGuesses stores the total number of guesses the user has made. Below is a screenshot of the JPanel when it first appears. Initially the guessT textfield and the promptT textfield are empty. Whenever the user presses the NEXT button, the program: • clears the guessT textfield, • generates a random number between 0 and 2 inclusive and stores it in the instance variable, computerNumber , and, • displays the String, "?" in the promptT textfield. When the user enters their guess in the guessT textfield and presses the CHECK button, the program: • increases the total number of guesses made by 1, • checks if the user guess is the same as the computerNumber , and increases the userScore by 1 if the user guess is correct, • clears the promptT textfield, and, • displays the current score information (see below).
Image of page 21
Image of page 22
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Method, ........., JPanel, Mutator method

{[ 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