Point at centre of circlesquare constructor public

This preview shows page 23 - 30 out of 39 pages.

/** point at centre of circle/square */ /** constructor. */ public Shapes () { int x, y; if (Math.random() < 0.5) shapeType = CIRCLE; else shapeType = SQUARE; if (Math.random() < 0.5) filledShape = true; else filledShape = false; x = (int)(Math.random()*(300-80-SIZE))+80+SIZE/2; y = (int)(Math.random()*(220-SIZE))+SIZE/2; centrePt = new Point(x,y); } //draw the shape public void drawShape(Graphics g) { if (shapeType == CIRCLE){ if(filledShape) g.fillOval(centrePt.x-SIZE/2,centrePt.y-SIZE/2,SIZE,SIZE); else g.drawOval(centrePt.x-SIZE/2,centrePt.y-SIZE/2,SIZE,SIZE); } else if (shapeType == SQUARE){ if(filledShape) g.fillRect(centrePt.x-SIZE/2,centrePt.y-SIZE/2,SIZE,SIZE); else g.drawRect(centrePt.x-SIZE/2,centrePt.y-SIZE/2,SIZE,SIZE); } } /** returns true if shape is a circle */ public boolean isCircleShape() { if (shapeType == CIRCLE) return true; else return false; } }//end of Shapes class
Image of page 23

Subscribe to view the full document.

Question/Answer Sheet - 24 - 415.101ST Surname: ................................ Forenames: ................................ CONTINUED The applet which uses the Shapes class contains an array of Shapes objects and whenever the user presses the 'ADD SHAPE' button a new Shapes object is added to the array (see previous screen shots). When the user presses the 'ALL SHAPES' button all the Shapes objects in the array are displayed. When the user presses the 'CIRCLES' button all the circular Shapes objects (filled and unfilled) are displayed.
Image of page 24
Question/Answer Sheet - 25 - 415.101ST Surname: ................................ Forenames: ................................ CONTINUED When the user presses the 'RESET' button a new array is created containing no Shapes objects. Complete the applet code in the boxes provided so that the applet executes as described. import java.awt.*; import java.applet.*; import java.awt.event.*; public class ArrayQ extends Applet implements ActionListener { /** Buttons for user input */ private Button showCirclesB, showAllB, addShapeB, resetB; /** booleans to store required display */ private boolean showAll, showCircles; /** private constant values */ private static final int MAX_NO_OF_SHAPES = 200; /** array and the array counter */ private Shapes[] allTheShapes; private int upToInArray;
Image of page 25

Subscribe to view the full document.

Question/Answer Sheet - 26 - 415.101ST Surname: ................................ Forenames: ................................ CONTINUED /** add the buttons to the applet and create the array*/ public void init() { setLayout(null); setUpAllButtons(); showAll = false; showCircles = false; //create array and initialise the counter for the array } /** when the buttons are pressed do the appropriate action*/ public void actionPerformed(ActionEvent e) { showAll = false; //set the booleans to false showCircles = false; if (e.getSource() == addShapeB){ //add a new Shapes object to the array showAll = true; } else if (e.getSource() == showCirclesB){ showCircles = true; } else if (e.getSource() == showAllB){ showAll = true; } else if (e.getSource() == resetB){ //initialise the array and the counter for the array
Image of page 26
Question/Answer Sheet - 27 - 415.101ST Surname: ................................ Forenames: ................................ CONTINUED } repaint(); }
Image of page 27

Subscribe to view the full document.

Question/Answer Sheet - 28 - 415.101ST Surname: ................................ Forenames: ................................ CONTINUED /** draws each shape from the array of shapes */ public void paint( Graphics g ) { if (showCircles == true){ //display all the circle Shapes objects stored in the array } else if (showAll == true){ //display all the Shapes objects stored in the array } } /** add all the Buttons to the Applet */ public void setUpAllButtons() { //code not included here } }//end of ArrayQ applet (12 marks)
Image of page 28
Question/Answer Sheet - 29 - 415.101ST Surname: ................................
Image of page 29

Subscribe to view the full document.

Image of page 30
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Subroutine, ........., Eye class

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