Assignment 8: Swingin’ with Callbacks Interfaces are fundamental to systems that
ID: 3599955 • Letter: A
Question
Assignment 8: Swingin’ with Callbacks Interfaces are fundamental to systems that use callbacks. Remember, these are objects that we set up to ’call back’ later, when the time is right. In java’s GUI package javax.swing, we build user interfaces (not Java interfaces) out of windows and buttons. To couple our custom code with these standard GUI elements, we must implement the interfaces expected by them. When set up correctly, our code is the thing that is ’called back’ when the button is pressed or other event occurs. For this lab, we will be implementing callback classes (in Swing-terminology, listeners) and adding them to Swing components. We will specifically listen for button presses and mouse activity. We often use inner classes for callbacks, since these classes are usually relevant to only a small section of code. In this assignment, all of our listeners will be written as inner classes in the main() method. 1 Your job I’ve provided java code called SwingLab. You should compile and run this code before you make any changes to it. When run, this code sets up a panel with 3 buttons labeled Red, Yellow, and Blue. This program will compile and run without any modifications, but the buttons do nothing. Your job is to make these buttons change the panel color to the color named on the button. You’re also going to print to System.out when the mouse enters and leaves a button’s area: The mouse has entered the yellow button area. The mouse has exited the yellow button area. 2 How to do it You will need to provide implementations of the java.awt interfaces ActionListener and MouseListener. Here’s an example: class RedButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { panel.setBackground(Color.RED); } } ActionListener redListener = new RedButtonListener(); bRed.addActionListener(redListener); Please note that MouseListener and ActionListener are interfaces that already exist and are imported – you do not need to write them. Also notice how RedButtonListener is referring to panel. This assumes you are including this class as an inner class directly in the main method, because panel is a local variable in the main method. (Notice that panel must be declared final – this does not mean that the object referred to by panel is a constant – it means that your panel variable will always refer to the same object.) Notice also how this class is referring to Color.RED. You can refer to all the colors needed for this assignment in a similar way. Read more about it in the Java documentation for the Color class. Here are the interfaces you will need to implement in your callbacjs. A single class can implement multiple interfaces! Do not cut and paste this code – it already exists in the Swing package. public interface ActionListener { public void actionPerformed(ActionEvent event); } public interface MouseListener { public void mouseEntered(MouseEvent event); public void mouseExited(MouseEvent event); public void mousePressed(MouseEvent event); public void mouseReleased(MouseEvent event); public void mouseClicked(MouseEvent event); } To implement MouseListener, you have to implement all five of those methods. To ignore a mouse event, provide an “empty” implementation of the corresponding interface method (a method with empty curly braces for a body) All of your implemented methods will probably just ignore the argument that is passed in. 3 Bonus There are two possibilities of bonus points for this lab. If you do either or both of these, make sure to make this clear when you submit your assignment. 1. 10 pts Implement the requirements using only one inner class. You can use more inner classes if needed to “do something cool.” You can instantiate this inner class multiple times. 2. 10 pts Usea javax.swing.Timer to reset the panel color 5 seconds after you change it. These timers use ActionListener like the buttons. 2 4 Turn In Make sure your name appears in comments at the top of every java file. Zip up your project directory and submit the zip file via blackboard.
Explanation / Answer
Answering both Part1 and Part2.