Continued 10 marks version 1 30 compsci 101

This preview shows page 29 - 33 out of 33 pages.

CONTINUED } } (10 marks)
Image of page 29

Subscribe to view the full document.

VERSION 1 - 30 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED 28. Consider the following application which displays a collection of randomly sized and positioned Rectangle objects on the screen. Every time the mouse is pressed, any rectangle (or rectangles) which contain the position of the mouse should disappear from the screen. For example, in the screenshot on the left below, the mouse is positioned inside one of the larger rectangles and is about to be pressed. The screenshot on the right shows the window just after the mouse button has been pressed. Notice that the rectangle which contained the position of the mouse press has disappeared.
Image of page 30
VERSION 1 - 31 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED As another example, in the screenshot on the left below the mouse is positioned inside several rectangles and is about to be pressed. The screenshot on the right shows the window just after the mouse button has been pressed. This time, all the rectangles that contained the position of the mouse have disappeared. You have to complete the mousePressed() and the paintComponent() method for the JPanel class. The constructor method has already been completed for you. The Rectangle objects are stored in an array called rects . HINT: To make a Rectangle disappear, you should move the Rectangle to a location outside the visible area. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyJPanel extends JPanel implements MouseListener { private Rectangle[] rects; public MyJPanel() { setBackground(Color.white); rects = new Rectangle[15]; for (int i = 0; i < rects.length; i++) { int rx = (int)(Math.random()*200); int ry = (int)(Math.random()*200); int rw = (int)(Math.random()*200); int rh = (int)(Math.random()*200); rects[i] = new Rectangle(rx, ry, rw, rh); } addMouseListener(this); }
Image of page 31

Subscribe to view the full document.

VERSION 1 - 32 - COMPSCI 101 Question/Answer Sheet ID ........................................ CONTINUED public void mousePressed(MouseEvent e) { repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); } public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} } (10 marks)
Image of page 32
VERSION 1 - 33 - COMPSCI 101 Question/Answer Sheet ID ........................................ OVERFLOW PAGE (If you have used this page, please indicate clearly under the relevant question that you have overflowed to this page)
Image of page 33
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, ........., Types of chocolate, mouse press

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