Java 2D对象重复

时间:2013-12-08 12:13:35

标签: java 2d

我使用Java g.drawstring()方法构建了一个Java 2d圈,并使用了Breseham's Circle算法,我用它来绘制一个笑脸但是当我移动笑脸时,它在屏幕上重复,我知道肯定是PaintComponent(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个声望。

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

2 个答案:

答案 0 :(得分:1)

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

重绘 - > paintComponent - >重绘 - > ...

这搞乱了Graphics对象。

修改

您的代码中存在另一个错误,即调用super.paintComponent s (g)而不是super.paintComponent(g)。尝试使用不带 s 的方法。

这是一个根据最后一次点击的位置移动字符串的类的简单示例:



public class Test extends JPanel implements MouseListener {

    private int x = 100;
    private int y = 100;

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        Test test = new Test();
        jFrame.add(test);
        jFrame.setBounds(0, 0, 800, 600);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        jFrame.addMouseListener(test);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("blub", x, y);

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        this.x = e.getX();
        this.y = e.getY();
        repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

答案 1 :(得分:0)

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

调用repaint()实际上会提交一个新的绘制请求,导致另一个paintComponenr(Graphics g)函数调用并将repaint()置于paintComponent函数中将创建一个循环,调用绘画逐个函数增加渲染堆栈。因此,请从repaint()功能中删除paintComponent来电。

查看教程:Performing custom painting