我正在尝试打开和关闭灯。
为此,我想将颜色更改为黑色并返回原始颜色。但是if
语句没有做任何事情。
我的灯不会改变颜色。
这是代码:
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class Light extends JPanel
{
//Class variables go here
private int xLoc= 550, yLoc;
private int wide = 100, high =100;
private Color originalC, currentC;
//default constructor
public Light()
{
xLoc =100;
yLoc =100;
wide=50;
high=50;
originalC= currentC = Color.red;
}
//Secondary Constructor so you can draw lights of a different color at specified locations
public Light(Color c,int y)
{
yLoc =y;
originalC = currentC = c;
}
//mutator or settor methodsh
//If the light is not black (it has a color) then turn it off by setting it to black
public void LightA()
{
if(currentC==originalC)
{
currentC=Color.black;
}
else
{
currentC=originalC;
}
}
//If the light is off (current color is black) turn it on by checking it's position and setting the color appropriately
//gettor or accessor methods - think about what information the Light Panel needs to know!
//a helper method called from the LightPanel class that takes the Graphics g argument
//and draws a light at the current class attributes
public void drawLight(Graphics g)
{
g.setColor(currentC); //replace Color.red with the class attribute that is the color for a light
g.fillOval(xLoc,yLoc,wide,high);//replace the numbers with the class attributes so you can draw lights
}
}