简体   繁体   English

这个我正在制作动画的正方形背后留下了一条痕迹,任何人都可以找出原因吗?

[英]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.

相关问题 我不知道为什么我得到StackOverFlowError - I can't figure out why I'm getting StackOverFlowError 我正在使用Servlet,但似乎无法弄清楚为什么我的下载文件Servlet可以在本地运行,但不能正常运行 - I'm using Servlets and can't seem to figure out why my download file servlet will work locally, but not otherwise 谁能帮我弄清楚为什么这个while循环对“回文数”LeetCode问题不起作用? - Can anyone help me figure out why this while loop won't work for the “Palindrome Number” LeetCode problem? 无法弄清楚为什么我得到了StringIndexOutOfBoundsException - Can't figure out why I'm getting a StringIndexOutOfBoundsException 我启动时发生了一些事情,导致该应用崩溃。 如果有人可以帮助我,我会无所适从,我会很高兴 - Something in my initiation is causing the app to crash. I'm fresh out of ideas if anyone can help I will be greatfull 我正在尝试在 Java 中绘制正方形,但它不起作用 - I'm trying to draw square in java but it doesn't work 我无法弄清楚为什么在通过io运行时会收到java.lang.Nullpointerexception错误消息 - I can't work out why I'm getting a java.lang.Nullpointerexception error message when i run through my io 当我进入该程序时,对#2和#3的选择将不起作用。 谁能帮助我了解为什么它会以这种方式运行? - When I enter this program, my choices for #2 and #3 will not work. Can anyone help me understand why it is behaving this way? 处理-运动图像离开轨迹 - Processing — moving image leaving trail 谁能告诉我我在做什么错? -堆栈 - Can anyone tell me what I'm doing wrong? - Stacks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM