Hello. This is my first post! I'm having some difficulty with this code. I have a light-bulb which is attached to a powerplug. When the mouse clicks the power plug, the light-bulb turns on, and turns off when clicked again using mouse events.
Now I want to create more light-bulbs, that when the first one turns on, the second turns on as well, then the third.
The last light (4th light), should turn on/off by itself when the plug is clicked and is independent from the other 3 lights.
I have a class 'Lights', 'Plug', Mouseable interface, and a class 'LightShow'. I think I have to make another class to make this work, and supposed to use inheritance and polymorphism. Sorry if this sounds confusing, it's for school and I'm absolutely lost. It seems basic/simple to do, yet I'm lost :(
Sorry again for the long post, but I will attach what I have:
import wheels.users.*;
import java.awt.Color;
public class Light {
private Ellipse _bulb;
private Rectangle _base;
private Color _onColor;
private Color _offColor;
public Light(){
this(Color.red);
}
public Light(Color c){
_onColor = c;
_offColor = _onColor.darker();
_bulb = new Ellipse(_onColor);
_bulb.setSize(25,100);
_base = new Rectangle();
_base.setColor(Color.black);
_base.setSize(25,25);
this.setLocation(100,100);
}
public void setLocation(int x, int y){
_bulb.setLocation(x, y);
_base.setLocation(x, y+75);
}
public void toggle(){
if(_bulb.getColor().getRed() == _onColor.getRed() &&
_bulb.getColor().getGreen() == _onColor.getGreen() &&
_bulb.getColor().getBlue() == _onColor.getBlue())
_bulb.setColor(_offColor);
else
_bulb.setColor(_onColor);
}
}
import wheels.users.*;
import java.awt.Color;
import java.awt.event.*;
public class Plug implements Mouseable{
private MouseableRectangle _prong1, _prong2, _plug;
private Rectangle _cord;
private Light _light;
public Plug(Light l){
_light = l;
_plug = new MouseableRectangle(this);
_plug.setSize(50,50);
_plug.setColor(Color.black);
_prong1 = new MouseableRectangle(this);
_prong1.setSize(25,5);
_prong1.setColor(Color.black);
_prong2 = new MouseableRectangle(this);
_prong2.setSize(25,5);
_prong2.setColor(Color.black);
_cord = new Rectangle(Color.black);
_cord.setSize(225, 10);
this.setLocation(300,300);
}
public void setLocation(int x, int y){
_plug.setLocation(x,y);
_prong1.setLocation(x+50, y + 15);
_prong2.setLocation(x+50, y + 30);
_cord.setLocation(x-225, y+20);
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseDragged(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
_light.toggle();
}
}
public interface Mouseable{
public void mousePressed(java.awt.event.MouseEvent e);
public void mouseReleased(java.awt.event.MouseEvent e);
public void mouseDragged(java.awt.event.MouseEvent e);
public void mouseClicked(java.awt.event.MouseEvent e);
}
import wheels.users.*;
import java.awt.event.*;
public class MouseableRectangle extends Rectangle{
Mouseable _myCompositeObject;
public MouseableRectangle(Mouseable myObject){
_myCompositeObject = myObject;
}
public void mousePressed(MouseEvent e){
_myCompositeObject.mousePressed(e);
}
public void mouseDragged(MouseEvent e){
_myCompositeObject.mouseDragged(e);
}
public void mouseReleased(MouseEvent e){
_myCompositeObject.mouseReleased(e);
}
public void mouseClicked(MouseEvent e){
_myCompositeObject.mouseClicked(e);
}
}
import wheels.users.*;
import java.awt.Color;
public class LightShow extends Frame{
private Light _light, _light2, _light3, _light4;
private Plug _plug;
private Color _color1;
public LightShow(){
_color1 = new Color(255,255,0);
_light = new Light(_color1);
_light.setLocation(225,200);
_light2 = new Light(_color1);
_light2.setLocation(175,200);
_light3 = new Light();
_light3.setLocation(125,200);
_light4 = new Light();
_light4.setLocation(75,200);
_plug = new Plug(_light);
_plug.setLocation(300,270);
}
public static void main(String[] args){
LightShow lightshow = new LightShow();
}
}
If anyone can somewhat direct me on what to do, or how to approach on getting this accomplished, it'll be greatly appreciated. Thanks for dropping by!