Question: 1. Name your button handler class ButtonHandler. 2. Your button handler class must implement the ActionListener interface. import java.awt.Graphics; import javax.swing.JFrame; import java.awt.Color; public class

1. Name your button handler class ButtonHandler. 2. Your button handler class must implement the ActionListener interface. import java.awt.Graphics; import javax.swing.JFrame; import java.awt.Color; public class Circuit { private int xStart = 100; private int yStart = 100; private boolean switch1; private Color circuitColor; public Circuit( ) { switch1 = false; circuitColor = Color.BLUE; } public void open( ) { switch1 = false; } public void close( ) { switch1 = true; } public void draw( Graphics g ) { // draw Circuit but not the switch // set color to blue drawSwitchAndBulb( g ); g.setColor( Color.BLUE ); g.drawLine( xStart, yStart, xStart, yStart + 205 ); g.drawLine( xStart, yStart + 205, xStart + 100, yStart + 205 ); g.drawLine( xStart + 160, yStart + 205, xStart + 300, yStart + 205 ); g.drawString( "Switch", xStart + 115, yStart + 230 ); g.drawLine( xStart + 300, yStart + 205, xStart + 300, yStart + 120 ); g.drawLine( xStart + 300, yStart + 80, xStart + 300, yStart ); g.drawLine( xStart + 300, yStart, xStart + 150, yStart ); g.drawLine( xStart + 140, yStart, xStart, yStart ); g.drawLine( xStart + 140, yStart - 10, xStart + 140, yStart + 10 ); g.drawLine( xStart + 150, yStart - 5, xStart + 150, yStart + 5 ); g.drawString( "Power source", xStart + 120, yStart - 20 ); // Draw the switch g.drawOval( xStart + 280, yStart + 80, 40, 40 ); g.drawString( "Light bulb", xStart + 330, yStart + 100 ); } public void drawSwitchAndBulb( Graphics g ) { if ( switch1 ) g.setColor( Color.GREEN.darker( ) ); else g.setColor( Color.RED ); g.drawOval( xStart + 100, yStart + 200, 10, 10); g.drawOval( xStart + 150, yStart + 200, 10, 10); if ( switch1 ) { g.drawLine( xStart + 105, yStart + 200, xStart + 155, yStart + 200 ); g.setColor( Color.YELLOW ); g.fillOval( xStart + 280, yStart + 80, 40, 40 ); } else { g.drawLine( xStart + 105, yStart + 200, xStart + 155, yStart + 180 ); } } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JButtonPractice extends JFrame { Container contents; // GUI components private JButton open; private JButton close; private Circuit circuit; private static JButtonPractice app; private boolean firstTime = true; public JButtonPractice( ) { super( "Choose your activity" ); contents = getContentPane( ); contents.setLayout( new FlowLayout( ) ); circuit = new Circuit( ); open = new JButton( "OPEN" ); contents.add( open ); close = new JButton( "CLOSE" ); contents.add( close ); // ***** 1. Student code starts here // declare and instantiate the button handler // and register it on the buttons ButtonHandler bh = new ButtonHandler( ); open.addActionListener( bh ); close.addActionListener( bh ); // end of task 1 setSize( 500, 375 ); setVisible( true ); } // ***** 2. Student code restarts here // Code a private class to implement the correct Listener // and its required method // To open the switch, call the open method with the statement // open( ); // To close the switch, call the close method with the statement // close( ); // The last statement of the method should be // animate( ); private class ButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { if ( e.getSource( ) == open ) { open( ); } else if ( e.getSource( ) == close ) { close( ); } animate( ); } } // end of task 2 public void open( ) { circuit.open( ); } public void close( ) { circuit.close( ); } private void animate( ) { try { repaint( ); Thread.sleep( 200 ); } catch ( InterruptedException e ) { System.out.println( "IE Exception " + e.getMessage( ) ); System.out.println( e.toString( ) ); } } public void paint( Graphics g ) { if ( firstTime ) firstTime = false; super.paint( g ); circuit.draw( g ); } public static void main( String [] args ) { app = new JButtonPractice( ); app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!