Variable and initialised in the init method when the

This preview shows page 8 - 13 out of 24 pages.

variable and initialised in the init() method. When the mouse is pressed inside any of the rectangles, that rectangle should be drawn with a cross inside it. If the rectangle already has a cross when the mouse is pressed inside it, the cross should disappear. For example, in the screenshot below the user has pressed the mouse inside one of the rectangles, and a cross is drawn inside it:
Image of page 8

Subscribe to view the full document.

Question/Answer Sheet - Page 9 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED If the mouse lies inside more than one rectangle when it is pressed, they all should have crosses displayed. For example, in the screenshot below the user has pressed the mouse at a location which is the intersection of two rectangles. Because they both contain the mouse press location, a cross is drawn inside both of them: If the mouse is pressed inside a rectangle which is currently drawn with a cross inside it, then that cross should disappear. The screenshot below shows the applet after the user presses the mouse inside one of the rectangles that had just had a cross drawn inside it from the previous screenshot: Notice that the cross is no longer drawn inside that rectangle.
Image of page 9
Question/Answer Sheet - Page 10 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED The screenshot below shows the applet when all except one of the rectangles have crosses displayed: You need to complete the paint() and mousePressed() methods for this applet. You should make use of the following variables: The array: Rectangle[] boxes; is used to store the locations of the rectangles, and is initialised in the init() method (which makes use of the randomRect() method to generate the rectangle's positions) and the array: boolean[] crosses; to store whether or not a particular rectangle should have a cross displayed inside it. This array has also been initialised in the init() method for you. import java.awt.*; import java.applet.*; import java.awt.event.*; public class Q5 extends Applet implements MouseListener { public static final int NUM_RECTANGLES = 10; private Rectangle[] boxes; private boolean[] crosses; private Rectangle randomRect() { final int SIZE = 30; int x = (int)(Math.random() * (200 - SIZE)); int y = (int)(Math.random() * (200 - SIZE)); return new Rectangle(x, y, SIZE, SIZE); }
Image of page 10

Subscribe to view the full document.

Question/Answer Sheet - Page 11 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED public void init() { boxes = new Rectangle[NUM_RECTANGLES]; for (int i = 0; i < NUM_RECTANGLES; i++) boxes[i] = randomRect(); crosses = new boolean[NUM_RECTANGLES]; addMouseListener(this); } public void mousePressed(MouseEvent e) { } public void paint(Graphics g) { } public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
Image of page 11
Question/Answer Sheet - Page 12 - CompSci 101 AC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 6 (5 marks) What is the output of the following applet? public class Q6 extends Applet { public void init() { Point p = new Point(100, 100); Rectangle r1 = new Rectangle(10, 105, 100, 50); Rectangle r2 = new Rectangle(115, 50, 50, 60); Rectangle r3 = new Rectangle(100, 100, 25, 15); p.move(-3, 12); p.translate(110, 100); if (r1.contains(p)) System.out.println("A"); if (r2.contains(p)) System.out.println("B"); if (r3.contains(p)) System.out.println("C"); if (r1.intersects(r2)) System.out.println("D"); if (r1.intersects(r3)) System.out.println("E"); if (r2.intersects(r3)) System.out.println("F"); } } HINT: The diagram below shows what the rectangles r1 , r2 and r3 would look like if drawn to the screen.
Image of page 12

Subscribe to view the full document.

Image of page 13
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Following, ........., Rectangle, Given name

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