このコードを取得して、元の Box クラスに基づいて新しいボックスを印刷しようとしていますが、立ち往生しています。Grid クラスで指定する必要がある変数がわかりません。また、次のクラスに何を入れればよいかわかりません。
public void actionPerformed(ActionEvent evt) {
// maybe do stuff here
repaint();
}
これが私のクラスの両方です。
---コードは最初の返信以降に更新されています---
ボックスクラス
import java.awt.*;
public class Box{
int upperLeftX = 0;
int upperLeftY = 0;
int height = 20;
int width = 20;
Color color = Color.RED;
//constructor
public Box(int i, int j, int k, int l, Color m) {
upperLeftX = i;
upperLeftY = j;
height = k;
width = l;
color = m;
}
// paints the box on screen
public void display(Graphics g)
{
g.setColor(color);
g.fillRect(upperLeftX,upperLeftY,width, height);
}
// getters and setters
public int getUpperLeftX() {
return upperLeftX;
}
public void setUpperLeftX(int upperLeftX) {
this.upperLeftX = upperLeftX;
}
public int getUpperLeftY() {
return upperLeftY;
}
public void setUpperLeftY(int upperLeftY) {
this.upperLeftY = upperLeftY;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Color getBoxColor() {
return color;
}
public void setBoxColor(Color color) {
this.color = color;
}
}
グリッド クラス
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Grid extends Applet implements ActionListener{
// declare variables here
public void actionPerformed(ActionEvent evt) {
// maybe do stuff here
repaint();
}
public void paint(Graphics g) {
Box box1 = new Box(0,0,40,40,Color.WHITE);
box1.display(g);
// do more stuff here
Box box2 = new Box(40,40,40,40,Color.WHITE);
box2.display(g);
}
}