Java 2D对象重复

Java 2D对象重复,java,2d,Java,2d,我使用Javag.drawstring()方法和Breseham的圆圈算法构建了一个Java 2d圆圈,我用它画了一个笑脸,但当我移动笑脸时,它会在整个屏幕上重复,我知道画图组件(Graphics g)方法会在指定的不同位置重新绘制simley,但如何更正此逻辑错误是我的问题。这是我写的代码 public class Midsemester extends JPanel implements ActionListener { // objects class static Movement mo

我使用Java
g.drawstring()
方法和Breseham的圆圈算法构建了一个Java 2d圆圈,我用它画了一个笑脸,但当我移动笑脸时,它会在整个屏幕上重复,我知道
画图组件(Graphics g)
方法会在指定的不同位置重新绘制simley,但如何更正此逻辑错误是我的问题。这是我写的代码

public class Midsemester extends JPanel implements ActionListener {
// objects class
static Movement move = new Movement();
public int x = 0, y = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0;
private int inix = 0, iniy = 0;
static String[] st = {"xy","x1y1","x2y2","x3y3","x4y4"};
static shapes shaper = new shapes();//object class contain the algorithms used to draw the circles using the Breseham's Circle algorithm  


public Midsemester()
{

}
/****/


@Override
public void paintComponent(Graphics g)
{
   super.paintComponents(g);

   x = (int)move.x;
   y = (int)move.y;
   x1 = (int)move.x1;
   y1 = (int)move.y1;
   x2 = (int)move.x2;
   y2 = (int)move.y2;
   x3 = (int)move.x3;
   y3 = (int)move.y3;
   x4 = (int)move.x4;
   y4 = (int)move.y4;

   shaper.draw_floor(100, 350, 1350, g);
   shaper.draw_wall(100, 0, 350, g);
   shaper.create(x,y,50,g, Color.yellow);//creates the smileys in there different colors
   shaper.create(x1,y1,50,g, Color.BLUE);
   shaper.create(x2,y2,50,g, Color.pink);
   shaper.create(x3,y3,50,g, Color.magenta);
   shaper.create(x4,y4,50,g, Color.orange);

   repaint();

}


@Override
public void actionPerformed(ActionEvent ae)
{
    float[] values = move.firstscenemovement(500,200,st[0]);
    repaint();
    System.out.println("x:"+values[0] + "\ny:" + values[1]);
}
}
图像在移动时重复或跟踪可能会发布一张图像,但我需要10个声誉

如何更正此错误?提前感谢。

我认为您的
paintComponent()
代码中的
repaint()
会导致无休止的递归

重新绘制-->绘制组件-->重新绘制-->

这会弄乱图形对象

编辑:

代码中还有另一个错误,调用super.paintComponents(g)而不是
super.paintComponent(g)
。尝试在不使用s的情况下使用该方法

下面是一个简单的类示例,该类根据上次单击的位置移动字符串:


公共类测试扩展JPanel实现MouseListener{ 私人整数x=100; 私人int y=100; 公共静态void main(字符串[]args){ JFrame JFrame=新JFrame(); 测试=新测试(); jFrame.add(测试); jFrame.setBounds(0,0,800,600); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); jFrame.addMouseListener(测试); } @凌驾 公共组件(图形g){ 超级组件(g); g、 抽绳(“蓝色”,x,y); } @凌驾 公共无效mouseClicked(MouseEvent e){ this.x=e.getX(); this.y=e.getY(); 重新油漆(); } @凌驾 公共无效鼠标按下(MouseEvent e){ } @凌驾 公共无效MouseEvent(MouseEvent e){ //TODO自动生成的方法存根 } @凌驾 公共无效鼠标事件(鼠标事件e){ //TODO自动生成的方法存根 } @凌驾 公共无效mouseExited(MouseEvent e){ //TODO自动生成的方法存根 } } 我认为
paintComponent()
代码中的
repaint()
会导致无休止的递归

重新绘制-->绘制组件-->重新绘制-->

这会弄乱图形对象

编辑:

代码中还有另一个错误,调用super.paintComponents(g)而不是
super.paintComponent(g)
。尝试在不使用s的情况下使用该方法

下面是一个简单的类示例,该类根据上次单击的位置移动字符串:


公共类测试扩展JPanel实现MouseListener{ 私人整数x=100; 私人int y=100; 公共静态void main(字符串[]args){ JFrame JFrame=新JFrame(); 测试=新测试(); jFrame.add(测试); jFrame.setBounds(0,0,800,600); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); jFrame.addMouseListener(测试); } @凌驾 公共组件(图形g){ 超级组件(g); g、 抽绳(“蓝色”,x,y); } @凌驾 公共无效mouseClicked(MouseEvent e){ this.x=e.getX(); this.y=e.getY(); 重新油漆(); } @凌驾 公共无效鼠标按下(MouseEvent e){ } @凌驾 公共无效MouseEvent(MouseEvent e){ //TODO自动生成的方法存根 } @凌驾 公共无效鼠标事件(鼠标事件e){ //TODO自动生成的方法存根 } @凌驾 公共无效mouseExited(MouseEvent e){ //TODO自动生成的方法存根 } }
您是我发现的第一个在他们第一个发布的问题中调用被覆盖的
paintComponent(Graphics g)
函数中的
super.paintComponent(g)
的提问者。那么

调用
repaint()
实际上会提交一个新的绘制请求,从而再次调用
paintcomponener(Graphics g)
函数,并将
repaint()
放入
paintComponent
函数将创建一个循环,依次调用绘制函数以增加渲染堆栈。因此,从
paintComponent
函数中删除
repaint()
调用


查看教程:

您是我发现的第一个提问者,在他们第一个发布的问题中,您在覆盖的
paintComponent(Graphics g)
函数中调用了
super.paintComponent(g)
。那么

调用
repaint()
实际上会提交一个新的绘制请求,从而再次调用
paintcomponener(Graphics g)
函数,并将
repaint()
放入
paintComponent
函数将创建一个循环,依次调用绘制函数以增加渲染堆栈。因此,从
paintComponent
函数中删除
repaint()
调用


查看教程:

即使在移除paintcomponent(图形g)上的repaint()方法后,它仍然无法解决问题,但在actionlistener代码部分,即使移除paintcomponent(图形g)上的repaint()方法,它也不会移动对象它仍然无法解决问题,但在actionlistener代码部分,它不会移动对象