在其他 class 中更改颜色 "lamp"
Changing color "lamp" in other class
情况:
我制作了一个 Java 可以打开和关闭灯的应用程序。现在我需要改变灯光,这必须发生在 class
ColorLamp 中。正常的"yellow"灯放在class
Lamp里。 class
Colorlamp 是 class
Lamp.[=25 的 subclass
=]
问题:
如何才能通过 class
ColorLamp 中的一些代码更改 Lamp 颜色?
问题更新:
如何使用 class
ColorLamp 更改 Lamp 的颜色?
代码:
这里的代码来自 class
Lamp(更新):
public class Lamp
{
protected Color kleur = Color.YELLOW;
public static final boolean AAN = true;
public static final boolean UIT = false;
// instance variable
protected boolean aanUit;
// constructor
public Lamp()
{
// init instance variable
this.aanUit = UIT;
}
public void setAanUit(boolean aanUit)
{
this.aanUit = aanUit;
}
// switch
public void switchAanUit()
{
this.aanUit = !this.aanUit;
}
public boolean getAanUit()
{
return this.aanUit;
}
public String toString()
{
String res = "Lamp: ";
if (aanUit)
{
res = res + "AAN";
}
else
{
res = res + "UIT";
}
return res;
}
public void teken(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(5));
g2.drawOval(208, 100, 50, 50); // ronde lamp
g2.drawLine(220, 150, 220, 175); // linker kant
g2.drawLine(245, 150, 245, 175); // rechter kant
g2.drawLine(220, 175, 235, 200); // linksonder hoek
g2.drawLine(235, 200, 245, 175); // rechtsonder hoek
if(aanUit == true)
{
ColorLamp kleurlamp = new ColorLamp();
g.setColor(kleurlamp.getColor());
}
else
{
g.setColor(Color.WHITE);
}
g.fillOval(208, 100, 50, 50);
g.setColor(Color.BLACK);
}
}
这里是当前 class
ColorLamp 的代码(有效,但不是这样):
public class ColorLamp extends Lamp
{
protected Color kleur = Color.GREEN;
public Color getColor()
{
return kleur;
}
}
可能正确的代码class
颜色Lamp:
package lamp;
import java.awt.*;
public class ColorLamp extends Lamp
{
protected Color kleur = Color.GREEN;
public ColorLamp(Color kleur)
{
super();
this.kleur = kleur;
}
public Color getKleur()
{
return this.kleur;
}
public void setKleur(Color kleur)
{
this.kleur = kleur;
}
public String toString()
{
String res = "Lamp: ";
if(super.getAanUit())
{
res = res + "ÄAN";
}
else{
res = res + "UIT";
}
return res + kleur.toString();
}
}
你应该做的是让所有 Lamp
个对象都有一个 Color
。 Lamp
class 本身将具有 Color.Yellow
,并且无法从其他 class 中更改。
public class Lamp
{
protected Color kleur = Color.YELLOW;
/// Other things...
public void teken(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// Draw the lamp parts
if(aanUit == true)
{
g.setColor(this.kleur); // Set color here
}
else
{
g.setColor(Color.WHITE);
}
g.fillOval(208, 100, 50, 50);
g.setColor(Color.BLACK);
}
}
然后在ColorLamp
中,你可以移除private Color kleur;
并使用继承的protected Color kleur
字段。
要在绘制 Lamp 后更改颜色,您需要 repaint
组件。
情况:
我制作了一个 Java 可以打开和关闭灯的应用程序。现在我需要改变灯光,这必须发生在 class
ColorLamp 中。正常的"yellow"灯放在class
Lamp里。 class
Colorlamp 是 class
Lamp.[=25 的 subclass
=]
问题:
如何才能通过 class
ColorLamp 中的一些代码更改 Lamp 颜色?
问题更新:
如何使用 class
ColorLamp 更改 Lamp 的颜色?
代码:
这里的代码来自 class
Lamp(更新):
public class Lamp
{
protected Color kleur = Color.YELLOW;
public static final boolean AAN = true;
public static final boolean UIT = false;
// instance variable
protected boolean aanUit;
// constructor
public Lamp()
{
// init instance variable
this.aanUit = UIT;
}
public void setAanUit(boolean aanUit)
{
this.aanUit = aanUit;
}
// switch
public void switchAanUit()
{
this.aanUit = !this.aanUit;
}
public boolean getAanUit()
{
return this.aanUit;
}
public String toString()
{
String res = "Lamp: ";
if (aanUit)
{
res = res + "AAN";
}
else
{
res = res + "UIT";
}
return res;
}
public void teken(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(5));
g2.drawOval(208, 100, 50, 50); // ronde lamp
g2.drawLine(220, 150, 220, 175); // linker kant
g2.drawLine(245, 150, 245, 175); // rechter kant
g2.drawLine(220, 175, 235, 200); // linksonder hoek
g2.drawLine(235, 200, 245, 175); // rechtsonder hoek
if(aanUit == true)
{
ColorLamp kleurlamp = new ColorLamp();
g.setColor(kleurlamp.getColor());
}
else
{
g.setColor(Color.WHITE);
}
g.fillOval(208, 100, 50, 50);
g.setColor(Color.BLACK);
}
}
这里是当前 class
ColorLamp 的代码(有效,但不是这样):
public class ColorLamp extends Lamp
{
protected Color kleur = Color.GREEN;
public Color getColor()
{
return kleur;
}
}
可能正确的代码class
颜色Lamp:
package lamp;
import java.awt.*;
public class ColorLamp extends Lamp
{
protected Color kleur = Color.GREEN;
public ColorLamp(Color kleur)
{
super();
this.kleur = kleur;
}
public Color getKleur()
{
return this.kleur;
}
public void setKleur(Color kleur)
{
this.kleur = kleur;
}
public String toString()
{
String res = "Lamp: ";
if(super.getAanUit())
{
res = res + "ÄAN";
}
else{
res = res + "UIT";
}
return res + kleur.toString();
}
}
你应该做的是让所有 Lamp
个对象都有一个 Color
。 Lamp
class 本身将具有 Color.Yellow
,并且无法从其他 class 中更改。
public class Lamp
{
protected Color kleur = Color.YELLOW;
/// Other things...
public void teken(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// Draw the lamp parts
if(aanUit == true)
{
g.setColor(this.kleur); // Set color here
}
else
{
g.setColor(Color.WHITE);
}
g.fillOval(208, 100, 50, 50);
g.setColor(Color.BLACK);
}
}
然后在ColorLamp
中,你可以移除private Color kleur;
并使用继承的protected Color kleur
字段。
要在绘制 Lamp 后更改颜色,您需要 repaint
组件。