こんにちは、魅力のように機能する Java で星を描くクラスがあります。この後、Star クラスを拡張して、可能性を拡張した別の星を作成しました (この場合、色は異なる必要があります)。
クラスを呼び出してコンストラクターでパラメーターを指定すると、何らかの理由でパネルで子クラスの色のみが機能するようです。
ここに私のコードがあります
public class Star {
protected int radius;
protected int xmiddelpunt;
protected int ymiddelpunt;
protected static Color color;
public Star(int radius, int x, int y, Color color) {
xmiddelpunt = x;
ymiddelpunt = y;
this.radius = radius;
this.color = color;
}
}
そして拡張クラス
public class StarRed extends Star {
protected int x, y;
protected static Color color;
Random red = new Random();
public StarRed(int radius, int x, int y, Color color) {
super(radius, x, y, color);
this.radius = radius;
this.x = x;
this.y = y;
this.color = color;
}
}
私のパネルクラスのコンストラクタは次のとおりです。
ArrayList<Star> stars = new ArrayList<Star>();
ArrayList<StarRed> rs = new ArrayList<StarRed>();
public HeavenPanel() {
setBackground(Color.blue); // geef het paneel een blauwe kleur
this.addMouseWheelListener(this); // set de mouselistener
for(int i = 0; i < 10; i++) {
stars.add(new Star (r.nextInt(30 + 50), r.nextInt(10 + 701), r.nextInt(10 + 701), Color.yellow));
}
for(int k = 0; k < 10; k++) {
rs.add(new StarRed(40, r.nextInt(30 + 50), r.nextInt(30 + 50), Color.red));
}
}