Question: Exercise 2A [5%] Create a class called MyComponent. Extend this class from JComponent. Create a class called MyMain. Add a main method to this class.
Exercise 2A [5%] Create a class called MyComponent. Extend this class from JComponent. Create a class called MyMain. Add a main method to this class. Exercise 2B [5%] Instantiate a JFrame in your main method and add a variable of type MyComponent to the JFrame. Ensure the window is visible and will exit the program upon closing. Set the size of the window to a dimension of your choosing. Exercise 2C [20%] Add a mouse listener and a key listener to MyComponent. To verify that the events are being processed print the mouse button and the key that was pressed to the command line. 2 Exercise 2D [20%] Whenever a mouse button is clicked or key on the keyboard pressed determine in which quadrant of the window the mouse cursor is. The top left is quadrant one, top right quadrant two, bottom right quadrant three and bottom left quadrant four. Store number of times an event occurred over a certain quadrants in a Map data structure. Whenever an event occurs print the Map to the command line. For example if a mouse click or keyboard button press event occurs over quadrant one 3 times, quadrant two 2 times, quadrant 3 five times and quadrant four once: {1=1} {1=2} {1=3} {1=3, 2=1} {1=3, 2=2} {1=3, 2=2, 3=1} {1=3, 2=2, 3=2} {1=3, 2=2, 3=3} {1=3, 2=2, 3=4} {1=3, 2=2, 3=5} {1=3, 2=2, 3=5, 4=1} If the sequence is different but events occur the same amount of times over the same quadrants the end result will be the same: {3=1} {1=1, 3=1} {1=2, 3=1} {1=3, 3=1} {1=3, 2=1, 3=1} {1=3, 2=1, 3=1, 4=1} {1=3, 2=2, 3=1, 4=1} {1=3, 2=2, 3=2, 4=1} {1=3, 2=2, 3=3, 4=1} {1=3, 2=2, 3=4, 4=1} {1=3, 2=2, 3=5, 4=1} See Figure 1 for an example of the quadrants. You window does not need to display the quadrants.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
