我想讓Java swing程序使用鼠標繪製不重疊的n邊多邊形,圓形和橢圓形。在buffrered圖像上使用鼠標在JPanel上繪製不重疊的n邊多邊形,圓形和橢圓
多邊形不應該添加最後一個點,並且首先指向自己,直到用戶創建它。
我想讓Java swing程序使用鼠標繪製不重疊的n邊多邊形,圓形和橢圓形。在buffrered圖像上使用鼠標在JPanel上繪製不重疊的n邊多邊形,圓形和橢圓
多邊形不應該添加最後一個點,並且首先指向自己,直到用戶創建它。
這是一個非常基本的例子
public class DrawPolygon {
public static void main(String[] args) {
new DrawPolygon();
}
public DrawPolygon() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PolyPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class PolyPane extends JPanel {
private MouseHandler mouseHandler;
private Path2D currentShape;
private List<Path2D> lstPloys;
private Point lastPoint;
private Point currentPoint;
public PolyPane() {
lstPloys = new ArrayList<>();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void addNotify() {
super.addNotify();
addMouseListener(getMouseHandler());
addMouseMotionListener(getMouseHandler());
}
@Override
public void removeNotify() {
removeMouseListener(getMouseHandler());
removeMouseMotionListener(getMouseHandler());
super.removeNotify();
}
public MouseHandler getMouseHandler() {
if (mouseHandler == null) {
mouseHandler = new MouseHandler();
}
return mouseHandler;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (lastPoint != null) {
g2d.setColor(Color.RED);
g2d.fillOval(lastPoint.x - 2, lastPoint.y - 2, 4, 4);
}
if (currentShape != null) {
g2d.setColor(Color.RED);
g2d.draw(currentShape);
if (lastPoint != null && currentPoint != null) {
System.out.println(lastPoint + " - " + currentPoint);
g2d.setColor(new Color(255, 0, 0, 64));
g2d.draw(new Line2D.Float(lastPoint, currentPoint));
}
}
g2d.setColor(Color.BLACK);
for (Shape shape : lstPloys) {
g2d.draw(shape);
}
g2d.dispose();
}
public class MouseHandler extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (e.getClickCount() == 1) {
Point p = e.getPoint();
lastPoint = p;
if (currentShape == null) {
currentShape = new Path2D.Float();
currentShape.moveTo(p.x, p.y);
} else {
currentShape.lineTo(p.x, p.y);
}
repaint();
} else if (e.getClickCount() == 2) {
currentShape.closePath();
lstPloys.add(currentShape);
currentShape = null;
lastPoint = null;
repaint();
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
if (currentShape != null) {
currentPoint = e.getPoint();
repaint();
} else {
currentPoint = null;
}
}
}
}
}
你會希望時間通過
012閱讀由於這些將涵蓋你需要知道的基本知識,以實現你正在嘗試做的事情。
更新
我已經更新的例子,包括表示利用線下一個點MosueMotionListener
嗨MadProgrammer謝謝你的幫助。但這不是我正在尋找的線被繪製時,我點擊下一點,但我想線要開始繪製,因爲鼠標光標從點a移動到pointb。另外請添加圈子繪製到iot和重疊部分。請幫助 – user1844273
您掌握了基本知識。陷印重疊是一個簡單的過程,用於檢查形狀列表並使用「形狀」API重疊並丟棄重疊的形狀。至於「實時」反饋,您需要實現'MouseHandler'的'mouseMoved'方法並將鼠標移動監聽器連接到'PolyPane'。閱讀我提供的鏈接並嘗試一些內容。 – MadProgrammer
嗨,我一直在努力從上個星期。我不打包在這裏發佈我的代碼,以便你可以看到我到現在爲止嘗試了什麼。所以請有信心,看看你能否提供一些幫助 – user1844273
[你嘗試過什麼?](http://www.whathaveyoutried.com/ )爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –
嘿,有人請回答我上面的問題。我嘗試了很多鏈接,但它不起作用 – user1844273
示例程序繪製圓形我試圖向其添加多邊形功能 – user1844273