Question
Could someone help me with this program? In this activity you will work with two JButtons that control one electrical switch. Specifically you will write
Could someone help me with this program?
In this activity you will work with two JButtons that control one electrical switch. Specifically you will write the code to perform the following operations.
1) If the user clicks on the OPEN button, open the switch.
2) If the user clicks on the CLOSE button, close the switch.
Here are the given java files:
/* Circuit - animates the circuit Anderson, Franceschi */
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;
@ SuppressWarnings ("unused") 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 ); } } }
/* JButtonPractice, Programming Activity 1 Anderson, Franceschi */
import java.awt.*; import javax.swing.*; //import java.awt.event.*;
public class JButtonPractice extends JFrame { public static final long serialVersionUID = 1234L; 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
// 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( );
// 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 ); } }
For this assignment you only need to edit JButtonsPractice.java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started