Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4 QUESTION 2: ADDING VERY SIMPLE EVENT HANDLING Now add the capability for the GUI elements to control the actions of the program. The get

4 QUESTION 2: ADDING VERY SIMPLE EVENT HANDLING Now add the capability for the GUI elements to control the actions of the program. The get methods in the GUIelement superclass will allow the contents of an input box or the current status of a checkbox or radio button to be determined, which is all that is needed for those classes. But Button objects need the capability to trigger actions in a program. This will be done by implementing a Handler superclass, and then one subclass of Handler for each type of action that needs to be triggered by a button. (In real Java, the classes are EventListener and ActionListener but those are too complex for COMP 1020. Take a look at them if youre curious.) Look at the supplied program TestA3Q2.java. It creates 3 buttons labelled Red, Green, and Blue. It contains three methods drawRed(), drawGreen(), and drawBlue(). Your job is to link each button to the correct method so that when a button is clicked, the corresponding method will be called. 4.1 Define a Handler class. It will contain no instance variables, have no constructor, and contain only the method void doIt(), which will do nothing. Its only purpose is to be a superclass, and allow polymorphism. 4.2 Define three subclasses of the Handler class: HandleRed, HandleGreen, and HandleBlue. In each one, provide a void doIt() method which will call the corresponding drawRed, drawGreen, or drawBlue method in TestA3Q2. These handlers will provide the link between Button objects and the methods in the main program. 4.3 Modify your Button class as follows: a. Save a new copy of your class as Button2.java and change the name of the class and the constructor. You must leave the Button class from Question 1 untouched or else the TestA3Q1 program wont run and youll lose a lot of marks for Question 1. b. Add a new instance variable of type Handler that will store a reference to the Handler that should be used when the button is clicked. Add another parameter to the constructor (at the end) to set this variable. c. Modify the handleClick method so that it uses the doIt() method of this Handler when the button is clicked. This should always be done after the mouse is released. Allow for the possibility that the Handler variable may have been set to null, in which case nothing should be done.

4.4 Modify TestA3Q2 so that it uses Button2 objects and not Button objects. Also, every time a Button2 is created, a new Handler object of the correct subclass must be created and passed

COMP 1020 Winter 2018 Assignment 3 Page 7 of 7 to its constructor. Run the program. It should now work correctly, drawing a big coloured circle in response to button clicks. 4.5 HAND IN: Your files Button2.java, Handler.java, HandleBlue.java, HandleRed.java, and HandleGreen.java, as well as your modified version of TestA3Q2.java.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions