0
A
回答
1
我做了這樣的事情早就.. here's something that might help
0
不知道你的「環境」什麼......
擴展的JPanel。
添加MouseAdapter以捕獲mouseClicked方法中的座標,並將它們保存在面板的數組中。
重寫drawComponent方法繪製三角形。 喜歡的東西
class MyPanel extends JPanel {
private int count = 0;
private Point[] points = new Point[3];
MyPanel() {
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (count == points.length) {
for (int i = 1; i < points.length; i++) {
points[i-1] = points[i];
}
count -= 1;
}
points[count] = e.getPoint();
count += 1;
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gg = (Graphics2D) g;
if (count > 2) {
Polygon polygon = new Polygon();
for (int i = 0; i < count; i++) {
polygon.addPoint(points[i].x, points[i].y);
}
gg.setColor(Color.BLUE);
gg.fill(polygon);
}
}
}
這不是完整的,但是......
你是如何繪製三角形嗎? – 2009-04-08 12:49:22