gpt4 book ai didi

Java 2D 对象重复

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:27 25 4
gpt4 key购买 nike

我使用 Java g.drawstring() 方法和 Breseham's Circle 算法构建了一个 Java 2d 圆圈,并用它绘制了一个笑脸,但是当我移动笑脸时,它会在整个屏幕上重复,我确信 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 点声誉才能做到这一点。

如何纠正这个错误?提前致谢。

最佳答案

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

重绘-->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    }}

关于Java 2D 对象重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20453250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com