Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to edit this program so that the lightbulb stays on for 10 sec after clicking it and one needs to be able to

image text in transcribed

image text in transcribed

I need to edit this program so that the lightbulb stays on for 10 sec after clicking it and one needs to be able to see the timer on the panel as well as it counts down from 10.

I have the project file! This is what I have so far!

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); } }

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions