0
我正在嘗試做關於事件監聽器的練習。要繪製三角形我使用drawPolygon方法,但我不知道如何將它添加到buttonListener來執行actionListener。 這是代碼的一部分:如何將drawPolygon()添加到按鈕偵聽器?
public class Triangle extends Figure {
int x[];
int y[];
int n;
Triangle(boolean f, Color c, int x, int y, int n) {
super(f, c, x, y);
int xPoints[];
int yPoints[];
int nPoints[];
}
@Override
public void drawMySelf(Graphics g) {
int xPoints[] = {80, 150, 80};
int yPoints[] = {80, 150, 150};
int nPoints = 3;
Polygon P = new Polygon(x, y, n);
if (filled)
g.fillPolygon(P);
else g.drawPolygon(P);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String kommando = e.getActionCommand();
int width = drawPanel.getWidth();
int heigth = drawPanel.getHeight();
if (kommando.equals("Cercle")) {
theFigures.add(new Circle(false, Color.BLUE, 20, 20, 50));
} else if (kommando.equals("Rektangle")) {
theFigures.add(new Rectangle(false, Color.BLACK, 300, 100,
150, 100));
} else if (kommando.equals("Triangele")) {
theFigures.add(___my problem is here___);
} else if (kommando.equals("Cercle rempli")) {
theFigures.add(new Circle(true, Color.YELLOW, 50, 100, 100));
} else if (kommando.equals("Rektangle rempli")) {
theFigures.add(new Rectangle(true, Color.GREEN, 500, 50,
100, 250));
}
這是一個很好的例子,但我的要求是如何添加drawPolygone()her:theFigures.add(new Triangle(.......));看看我的代碼。我已經創建了按鈕和一切。當我按下按鈕時,我想顯示一個三角形。否則thx爲你的答案。 – Kurt16
那麼:theFigures.add(new Triangle(false,Color.YELLOW,50,100,100)); – KCD