这个广场我是动画留下线索背后,任何人都可以明白为什么? [英] This square I'm animating is leaving a trail behind it, can anyone work out why?

查看:158
本文介绍了这个广场我是动画留下线索背后,任何人都可以明白为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢检查出这个问题。我想我只是通过我在挫折头骨划伤。
因此,我所得到的是含有的JPanel'A'的JFrame。在'的JPanel'包含这应该是移动x像素,每当我点击窗口有点色坊。

Thanks for checking out this Question. I think I've just about scratched through my skull in frustration. So what I've got is a 'JFrame' containing a 'JPanel'. The 'JPanel' contains a little colored Square which is supposed to move X pixels whenever I click the window.

好了,基本上都表现为它应该有一个例外。当蓝色方块移动到右侧,它离开它背后的其他广场的痕迹。它不应该离开的线索,然而,当我重新大小的窗口,足迹消失了。

Well, essentially everything behaves as it should, with one exception. When the blue square moves to the right, it leaves a trail of other squares behind it. It is not supposed to leave the trail, however, when I re-size the window, the trail vanishes.

Catalyst.java

Catalyst.java

package Prototype;

import java.awt.*;

public class Catalyst {

public static void main(String[] args){
    World theWorldInstance = new World("Prototype", 100,100, 600,100);  /*title,xpos,ypos,width,height*/
}

}

World.java

World.java

package Prototype;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class World extends JFrame  {

Level mainPanel;    

public World(String title, int x, int y, int width, int height) {

    setTitle(title);
    setBounds(x,y,width,height);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBackground(new Color(0x00000000));
    initLevel();

}

public void initLevel(){
    mainPanel = new Level();
    Container visibleArea = getContentPane();
    visibleArea.add(mainPanel);
    setVisible(true);
    add(mainPanel);
}



}

Level.java

Level.java

package Prototype;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Level extends JPanel implements MouseListener, ActionListener {
Square x;


public Level() {
    System.out.println("This is working correctly[JPANEL Cons]");
    addMouseListener(this);
    x = new Square();
}

public void paintComponent(Graphics g){
    x.draw(g);
}

public void actionPerformed(ActionEvent e){
}

public void mousePressed(MouseEvent e){
    requestFocus();
    x.move();
    repaint();
    System.out.println("Focus Acquired");


}

public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
}

Square.java

Square.java

package Prototype;

import java.awt.*;

public class Square {

private Point position;
private int size;
private final int displacement;

public Square(){
    position = new Point(10,10);
    size = 20;
    displacement = 5;

}

public void draw(Graphics g){
    g.setColor(Color.blue);
    g.fillRect(position.x-(size/2), position.y-(size/2), size,size );
    g.setColor(Color.black);
    g.drawRect(position.x-(size/2), position.y-(size/2), size,size );

}

public void move() {
    position.x += displacement;
}
}

这些都是我的所有文件。我希望我已经正确措辞的一切,并提供所有需要的内容。每当我已经做在过去,这从来没有发生过类似的事情。我想我失去了一些东西变小,或者我做了一些愚蠢的事。如果你能帮助我,在此先感谢!

Those are all my files. I hope I've phrased everything properly and provided all the content required. Whenever I've done something similar in the past this has never happened. I assume I'm missing something small, or I've done something stupid. If you can help me, thanks in advance!

推荐答案

有这样做的另一种方式。你可以简单地调用父对象的paintComponent方法来清除面板。

There's another way of doing this. You can simply call the parent object's paintComponent method to clear the panel.

在级别添加到您的构造函数:

Add this to your constructor in Level :

this.setBackground(Color.BLACK);

和本作中的paintComponent第一个呼叫:

And this as the first call in paintComponent:

super.paintComponent(g);

这篇关于这个广场我是动画留下线索背后,任何人都可以明白为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆