Continued question 6 7 marks the jpanel defined on

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

CONTINUED Question 6 (7 marks) The JPanel defined on the next page contains two JTextField components. Below is a screenshot of the JPanel when it is first displayed. The numberT (on the left) and the wordT (on the right) JTextFields are blank. An ActionEvent is generated when the user presses 'Enter' in the numberT JTextField. The JPanel contains an instance variable, words , which is a String array containing many words: private String[] words = {"hat", "bat", "cat", ... }; In the numberT JTextField, the user enters a whole number, representing the position in the array of the word which is to be displayed in the wordT JTextField. If the number entered by the user in the numberT JTextField is out of range then the String "OUT OF RANGE" should be displayed in the wordT JTextField. 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 screenshots below show the JPanel after the user has entered various whole numbers in the numberT JTextField and pressed 'Enter'. NOTES: None of the elements in the words String array are null . In this JPanel an ActionEvent is generated when the user presses 'Enter' in the numberT JTextField. You may assume that the user only enters whole numbers into the numberT JTextField.
Image of page 18

Subscribe to view the full document.

Question/Answer Sheet - Page 19 - CompSci 101 ID: ........................................................ CONTINUED import java.awt.*; import javax.swing.*; import java.awt.event.*; public class AJPanel extends JPanel { //all the words in the String array are not shown here private String[] words = {"hat", "bat", "cat", ... }; private JTextField numberT, wordT; public AJPanel() { numberT = new JTextField(3); wordT = new JTextField(10); add(numberT); add(wordT); } public void _________________________ ( _________________ e) { } } (7 marks)
Image of page 19
Question/Answer Sheet - Page 20 - CompSci 101 ID: ........................................................ CONTINUED Question 7 (5 marks) Define the createPointsArray() method at the bottom of this page. This method is passed one parameter, an array of Rectangle objects and returns an array of Point objects. The method creates an array of Point objects with the same number of elements as the parameter array, fills the Point array with Point objects with the same x, y values as the corresponding Rectangle object. For example, consider the code segment below: Rectangle[] rectangles = {new Rectangle(20, 15, 30, 15), new Rectangle(40, 15, 30, 16), new Rectangle(20, 35, 60, 15), new Rectangle(50, 65, 40, 10) }; Point[] points = createPointsArray (rectangles); for (int i=0; i<points.length; i++) { System.out.println(points[i].x + " " + points[i].y); } If you have defined the createPointsArray() method correctly, the output from the above code segment should be: 20, 15 40, 15 20, 35 50, 65 Note : The parameter array contains no null elements. private Point[] createPointsArray (Rectangle[] rects) { Point[] pts;
Image of page 20

Subscribe to view the full document.

Question/Answer Sheet - Page 21 - CompSci 101 ID: ........................................................ CONTINUED } (5 marks)
Image of page 21
Question/Answer Sheet - Page 22 - CompSci 101 ID: ........................................................ CONTINUED Question 8 (7 marks) You are required to complete the following JPanel which uses a Timer object. The Timer object is created with a delay of 100 milliseconds. Initially the JPanel displays one black square of size 20 pixels (given by the constant, SIZE) with a left, top position of 40, 30 (given by the
Image of page 22

Subscribe to view the full document.

Image of page 23
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Method, Subroutine, ... ..., 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