[英]This square I'm animating is leaving a trail behind it, can anyone work out why?
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'.
所以我得到的是一个包含'JPanel'的'JFrame'。 The 'JPanel' contains a little colored Square which is supposed to move X pixels whenever I click the window.
'JPanel'包含一个小的彩色方块,每当我点击窗口时它应该移动X像素。
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!
如果你能帮助我,请提前感谢!
You can use g.clearRect(x, y, width, height) , and provide the said coordinates, where you want the painting to be cleared from. 您可以使用g.clearRect(x,y,width,height) ,并提供所需的坐标,以便清除绘画。 Or you can give the dimensions of whole of the
JPanel/JComponent
where you are drawing, though doing this keep one thing in mind, that the said drawing is not a heavy work, else too much of cleaning will put extra burden on the painting calls. 或者你可以给出你正在绘制的整个
JPanel/JComponent
的尺寸,尽管这样做会记住一件事,上面描绘的图纸并不繁重,否则过多的清洁会给绘画调用增加额外的负担。
There's another way of doing this. 还有另一种方法。 You can simply call the parent object's paintComponent method to clear the panel.
您只需调用父对象的paintComponent方法即可清除该面板。
Add this to your constructor in Level : 将其添加到Level中的构造函数:
this.setBackground(Color.BLACK);
And this as the first call in paintComponent: 这是paintComponent中的第一个调用:
super.paintComponent(g);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.