0

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!

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by quuba
0

To start, work with the simplest logic, no gui, no color , no shapes.

public class LightSimple {

    boolean switched;// state of light

    public LightSimple() {
        switched = false; // default state - false
    }

    public void switchOn() {
        switched = true;
    }

    public void switchOff() {
        switched = false;
    }

    public boolean isSwitched() {
        return switched;
    }

    public String toString() {
        if (switched) {
            return "ON";
        } else {
            return "OFF";
        }
    }

    public static void main(String[] args) {
        //array to store LightSimple
        // three lamps -->size of array = 3
        int lightNumber = 3;
        LightSimple[] lsArray = new LightSimple[3];
        //create new lights and store to array

        lsArray[0] = new LightSimple();
        lsArray[1] = new LightSimple();
        lsArray[2] = new LightSimple();
        //
        //check
        for (int i = 0; i < lightNumber; i++) {
            System.out.print(i + " - " + lsArray[i] + "      ");
        }
        System.out.println();
        //
        lsArray[0].switchOn();
        for (int i = 0; i < lightNumber; i++) {
            System.out.print(i + " - " + lsArray[i] + "      ");
        }
        System.out.println();
        //
        lsArray[1].switchOn();
        for (int i = 0; i < lightNumber; i++) {
            System.out.print(i + " - " + lsArray[i] + "      ");
        }
        System.out.println();
        //
        lsArray[2].switchOn();
        for (int i = 0; i < lightNumber; i++) {
            System.out.print(i + " - " + lsArray[i] + "      ");
        }
        System.out.println();
        //
        lsArray[0].switchOff();
        for (int i = 0; i < lightNumber; i++) {
            System.out.print(i + " - " + lsArray[i] + "      ");
        }
        System.out.println();
        //
        System.out.println(lsArray[0].isSwitched());
        System.out.println(lsArray[1].isSwitched());
        System.out.println(lsArray[2].isSwitched());
    }
}

Remark light and switch have same behavior. Both can be switchedOn or switchedOff.
Write interface Switchable with methods from class LightSimple .
Write "public class LightSimple implements Switchable{".
Write own class SwitchSimple implements Switchable.
...

Edited by quuba: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.