Question
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
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. Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like.
// Demonstrates mnemonics and tool tips. //********************************************************************
import 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); } }
// Represents the control panel for the LightBulb program. //********************************************************************
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class LightBulbControls extends JPanel { private LightBulbPanel bulb; private JButton onButton, offButton;
//----------------------------------------------------------------- // Sets up the lightbulb control panel. //----------------------------------------------------------------- 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());
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(); } } }
// Represents the image for the LightBulb program. //********************************************************************
import javax.swing.*; import java.awt.*;
public class LightBulbPanel extends JPanel { private boolean on; private ImageIcon lightOn, lightOff; private JLabel imageLabel;
//----------------------------------------------------------------- // Constructor: Sets up the images and the initial state. //----------------------------------------------------------------- public LightBulbPanel() { lightOn = new ImageIcon("lightBulbOn.gif"); lightOff = new ImageIcon("lightBulbOff.gif");
setBackground(Color.black);
on = true; imageLabel = new JLabel(lightOff); add(imageLabel); }
//----------------------------------------------------------------- // 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; } }
Olype here to search 10-33 PM 6/201RStep 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