简体   繁体   English

如何在鼠标单击Jpanel的地方绘制填充的椭圆形

[英]How to draw a filled oval where a mouse clicked on Jpanel

I want to write a code to draw a filled oval where ever the mouse is clicked inside a panel. 我想编写一个代码来绘制填充的椭圆形,只要在面板内单击鼠标即可。 I used to develop some codes but unfortunately when I tried to do the next click the whole panel blanked and new point appeared. 我曾经开发过一些代码,但不幸的是,当我尝试进行下一次单击时,整个面板都变了空白,并出现了新的点。 I want to keep the previous points and add some new ones by the next user's click on the panel. 我要保留先前的要点,并在下一个用户单击面板时单击以添加一些新的要点。 How do I implement the paint component of MyPanel ? 如何实现MyPanel的paint组件? Here is my code; 这是我的代码; it does not work properly, because it produces some small points instead of rectangle. 它不能正常工作,因为它会产生一些小点而不是矩形。

class MyPanel extends JPanel {
Point pointClicked;

public MyPanel() {
     this.addMouseListener(new MouseAdapter() {
        @Override
         public void mouseClicked(MouseEvent e) {
             pointClicked = e.getPoint();
         }
     });
}
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillRect(pointClicked.x, pointClicked.y, 1, 1);
}
}

I want to keep the previous points and add some new ones by the next user's click on the panel. 我要保留先前的要点,并在下一个用户单击面板时单击以添加一些新的要点。

You need to keep track of each oval painted and repaint all ovals each time the paintComponent() method is called. 您需要跟踪每个绘制的椭圆,并在每次调用paintComponent()方法时重新绘制所有椭圆。

Check out Custom Painting Approaches for two different ways to do this 查看“ 自定义绘画方法”中的两种不同方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM