我正在尝试制作类似于 Paint 的东西,但遇到了一些障碍。基本上,您在调用paintComponent() 的JPanel 中进行绘制。但是,当程序运行时,左上角会出现一个不需要的点。我认为这是因为在程序开始时调用了paintComponent()。有什么办法可以禁用它吗?下面列出了代码(我删除了导入),感谢您的阅读。这是paintBoard类:
public class PaintBoard extends JPanel implements MouseListener
{
Image img;
//PaintBoard panel;
int circleX = 25;
int circleY = 25;
int x;
int y;
public PaintBoard()
{
this.addMouseListener(this);
}
public void paintComponent(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x, y, circleX, circleY);
}
public void mouseClicked(MouseEvent arg0)
{
}
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
public void mousePressed(MouseEvent arg0)
{
x = arg0.getX();
y = arg0.getY();;
repaint();
}
public void mouseReleased(MouseEvent arg0)
{
}
}
这是主要课程:
public class Main
{
JFrame mainFrame = new JFrame("Painting test");
PaintBoard board = new PaintBoard();
public Main() throws IOException
{
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(1750, 1000);
mainFrame.setLayout(new GridLayout(2, 2));
mainFrame.add(board);
//mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) throws IOException
{
new Main();
}
}