Note you may assume that the parameters tocheck and

This preview shows page 13 - 17 out of 22 pages.

Note: you may assume that the parameters toCheck and rects are not null. You may also assume that the rects array contains at least one element and the elements in the rects array are not null. private boolean overlapsAll (Rectangle toCheck, Rectangle[] rects) { } (6 marks)
Image of page 13

Subscribe to view the full document.

- 14 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 9 (10 marks) The JPanel defined on the next page contains two components: • A JTextField which displays a String , • The "CHANGE" JButton . Below is a screenshot of the JPanel when it is first displayed. The JTextField is initially empty and is big enough to contain 10 characters. Whenever the user presses the "CHANGE" JButton , a randomly-selected word is displayed in the JTextField . The random word is obtained from the words array of String s, which is an instance variable of the JPanel class: private String[] words = { "aglow", "bright", "enjoy", "content", "bliss","elated", "happy", "radiant", "joyful" }; You are required to complete the JPanel definition on the next page so that the JPanel behaves as described above. You must use the variables given in the code. The three screenshots below show the JPanel when the user presses the CHANGE button:
Image of page 14
- 15 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED (10 mark) import java.awt.* import javax.swing.*; public class AJPanel extends JPanel { private String[] words = {"aglow", "bright", "enjoy", "content", "bliss", "elated", "happy", "radiant", "joyful"}; private JButton changeB; private JTextField wordT; public AJPanel() { wordT = new JTextField(10); changeB = new JButton("CHANGE"); } public void actionPerformed(ActionEvent e) { int rand; String word; int numWords = words.length; } }
Image of page 15

Subscribe to view the full document.

- 16 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 10 (10 marks) The following JPanel responds to KeyEvent s and MouseEvent s. The JPanel displays a row of square bricks. Initially the JPanel displays a single square red brick, 30 pixels width and height, with top left position 60, 20. The user controls the number of bricks which are displayed by pressing the LEFT and the RIGHT arrow keys. When the user presses the LEFT arrow key, the number of bricks displayed decreases by 1 and when the user presses the RIGHT arrow key, the number of bricks displayed increases by 1. Below are some screenshots of the JPanel in action. To the left is a screenshot of the JPanel when it is first displayed. In the middle is a screenshot of the JPanel after the user has pressed the RIGHT arrow key seven times. On the right is a screenshot of the JPanel after the user has pressed the LEFT arrow key three times. The following numbered statements are part of the code. Place the correct number in the correct place in the AJPanel definition below and on the next page so the AJPanel executes as described above. 1. addMouseListener(this); 2. implements KeyListener, MouseListener 3. repaint(); 4. requestFocusInWindow(); 5. numberOfBricks = 1; 6. if (e.getKeyCode() == KeyEvent.VK_LEFT) { numberOfBricks--; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT){ numberOfBricks++; } 7. for (int i=0; i<numberOfBricks; i++) { 8. public void mouseReleased(MouseEvent e) {} 9. g.fillRect(x, y, SIZE, SIZE); 10. x = x + SIZE; 11. addKeyListener(this); 12. KeyEvent e 13. g.setColor(Color.BLACK);
Image of page 16
Image of page 17
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Following, Christopher Nolan, Tank, public void, JPanel

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