Question
For 80 points, add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is
For 80 points, add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off. For 20 points, put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the numberr of seconds as it counts up from 0 to 10
please show output in java
.
mport javax.swing.*; import java.awt.*;
public class LightBulb { //----------------------------------------------------------------- // Sets up a frame that displays a light bulb image that can be // turned on and off. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Light Bulb"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LightBulbPanel bulb = new LightBulbPanel(); LightBulbControls controls = new LightBulbControls(bulb);
JPanel panel = new JPanel(); panel.setBackground(Color.black); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(Box.createRigidArea(new Dimension(0, 20))); panel.add(bulb); panel.add(Box.createRigidArea(new Dimension(0, 10))); panel.add(controls); panel.add(Box.createRigidArea(new Dimension(0, 10)));
frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.Timer;
public class LightBulbControls extends JPanel { private LightBulbPanel bulb; private JButton onButton, offButton;
//----------------------------------------------------------------- // Sets up the lightbulb control panel. //----------------------------------------------------------------- class ButtonListener implements ActionListener {
private LightBulb bulb; public ButtonListener(LightBulb b) { bulb = b; } public ButtonListener(LightBulbPanel bulb2) { // TODO Auto-generated constructor stub } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } } public LightBulbControls(LightBulbPanel bulbPanel) { bulb = bulbPanel;
onButton = new JButton("On"); onButton.setEnabled(false); onButton.setMnemonic('n'); onButton.setToolTipText("Turn it on!"); onButton.addActionListener(new OnListener());
offButton = new JButton("Off"); offButton.setEnabled(true); offButton.setMnemonic('f'); offButton.setToolTipText("Turn it off!"); offButton.addActionListener(new OffListener()); Timer t = new Timer(1000, new ButtonListener(bulb)); t.start();
setBackground(Color.black); add(onButton); add(offButton); }
//***************************************************************** // Represents the listener for the On button. //***************************************************************** private class OnListener implements ActionListener { //-------------------------------------------------------------- // Turns the bulb on and repaints the bulb panel. //-------------------------------------------------------------- public void actionPerformed(ActionEvent event) { bulb.setOn(true); onButton.setEnabled(false); offButton.setEnabled(true); bulb.repaint(); } }
//***************************************************************** // Represents the listener for the Off button. //***************************************************************** private class OffListener implements ActionListener { //-------------------------------------------------------------- // Turns the bulb off and repaints the bulb panel. //-------------------------------------------------------------- public void actionPerformed(ActionEvent event) { bulb.setOn(false); onButton.setEnabled(true); offButton.setEnabled(false); bulb.repaint(); } } }
public void actionPerformed(ActionEvent evt, boolean buttonPressed) { JButton source = (JButton)evt.getSource(); String text = source.getText(); int time = 0; double stopTime = 10.00; if (buttonPressed) { imageLabel.setIcon(lightOn); timer.start(); startTime(); } else if (time == stopTime) { imageLabel.setIcon(lightOff); timer.stop(); stopTime(); } return; }
@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } } //----------------------------------------------------------------- // Paints the panel using the appropriate image. //----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent(page);
if (on) imageLabel.setIcon(lightOn); else imageLabel.setIcon(lightOff); }
//----------------------------------------------------------------- // Sets the status of the light bulb. //----------------------------------------------------------------- public void setOn(boolean lightBulbOn) { on = lightBulbOn; } }
*
*
*
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