Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重新绘制-立即绘制和删除_Java_Swing_Repaint_Runnable - Fatal编程技术网

Java 重新绘制-立即绘制和删除

Java 重新绘制-立即绘制和删除,java,swing,repaint,runnable,Java,Swing,Repaint,Runnable,我有一个乒乓球项目(有点),它可以工作,但是run()函数中有一个问题。如果我使用我写在面板上的函数(它们可以工作,我检查过)绘制框架,它会出现图形问题,如果我使用重新绘制(正如我所做的那样)它绘制框架并立即删除它,每个解决方案都会有帮助(最好是在我的代码级别使用简单的解决方案): import java.awt.*; 导入java.awt.event.MouseEvent; 导入java.awt.event.MouseListener; 导入java.util.logging.Level; 导

我有一个乒乓球项目(有点),它可以工作,但是run()函数中有一个问题。如果我使用我写在面板上的函数(它们可以工作,我检查过)绘制框架,它会出现图形问题,如果我使用重新绘制(正如我所做的那样)它绘制框架并立即删除它,每个解决方案都会有帮助(最好是在我的代码级别使用简单的解决方案):

import java.awt.*;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JPanel;
导入java.util.Random;
导入javax.swing.*;
导入sun.org.mozilla.javascript.internal.Kit;
公共类图片扩展JPanel实现MouseListener,可运行{
私有int k=0;
线程读取;
私人提款img;
私有矩形r1,r3;
公共图片(DrawPic img,矩形rect1,矩形rect3){
超级();
此设置位置(0,85);
this.setLayout(新的FlowLayout());
这个.setSize(12801024);
这个。addMouseListener(这个);
此参数为.setFocusable(true);
这是1.r1=1;
这1.r3=rect3;
this.img=img;
this.MyThread=新线程(this);
MyThread.start();
此.setVisible(true);
}
公共空白绘制矩形(矩形矩形,图形g){
k=3;
矩形绘制矩形(g);
矩形填充矩形(g);
}
公共空白绘制矩形(矩形矩形,图形g){
k=1;
矩形绘制矩形(g);
矩形填充矩形(g);
}
公共虚空绘画图像(DrawPic img、Graphics g){
k=2;
//g、 clearRect(0,0,this.getWidth(),this.getHeight());
img.drawing(g,this);
}
公共无效变更(int k1){
k=k1;
}
@凌驾
公共无效mouseClicked(MouseEvent e){
//抛出新的UnsupportedOperationException(“尚未支持”);
}
@凌驾
公共无效鼠标按下(MouseEvent e){
//抛出新的UnsupportedOperationException(“尚未支持”);
点p=r3.FindCenter();
双dx,dy;
dy=e.getY()-p.getY();
r3.移动(0,dy);
this.getGraphics().clearRect(0,0,this.getWidth(),this.getHeight());
this.paintRectangleL(r3,this.getGraphics());
this.paintRectangleR(r1,this.getGraphics());
this.paintImage(img,this.getGraphics());
}
@凌驾
公共无效MouseEvent(MouseEvent e){
//抛出新的UnsupportedOperationException(“尚未支持”);
}
@凌驾
公共无效鼠标事件(鼠标事件e){
//抛出新的UnsupportedOperationException(“尚未支持”);
}
@凌驾
公共无效mouseExited(MouseEvent e){
//抛出新的UnsupportedOperationException(“尚未支持”);
}
public void animate(){
双dx=0,dy=2;
如果((this.img.getX()+160+this.r1.RightPoint().getX()-this.r1.LeftPoint().getX()>this.getWidth()| | this.img.getX()this.getHeight()| | this.img.getY()<0){
dy=-1*dy;
}
img.Move(dx,dy);
//this.getGraphics().clearRect(0,0,this.getWidth(),this.getHeight());
//this.paintImage(img,this.getGraphics());
//this.paintRectangleL(r3,this.getGraphics());
//this.paintRectangleR(r1,this.getGraphics());
重新油漆();
}
@凌驾
公开募捐{
颜色颜色;
while(true){
制作动画();
试一试{
阅读、睡眠(35);
}捕获(中断异常例外){
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
//抛出新的UnsupportedOperationException(“尚未支持”);
}

您不应该使用
getGraphics()
进行自定义绘制,因为它是一个临时缓冲区,在下次重新绘制时可以循环使用。是否在
paintComponent()
中绘制

有关更多信息和示例,请参阅。第节对
paint()
paintComponent()
方法进行了很好的总结。另见

编辑:

程序的逻辑和结构不适合Swing的绘制过程。您需要重构您的程序,以便它能够插入该过程,并在正确的时间绘制正确的内容。 通常,您可以通过覆盖组件的
paintComponent()
来自定义组件。所有的绘画都是用这种方法完成的。此方法应尽可能快,避免在其中放入任何/太多应用程序逻辑

一旦绘制对象的状态发生变化,您应该保持绘制对象的某种状态(即坐标、颜色等)。
repaint()
。这将安排重新绘制,最终Swing将对调用
paintComponent()
的组件执行
paintComponent()

在你的情况下,你有一个定时器,定期点火。您可以覆盖您使用的
JPanel
paintComponent
。您已经有了计算坐标的逻辑。将这些坐标存储在成员变量中。然后,发出
repaint()
。在
paintComponent
中,根据计算出的坐标绘制图像

编辑:

关于线程的另一个注释。Swing具有单线程绘制模型。所有UI交互和绘制都在Swing的事件调度线程(EDT)上进行。有关EDT的更多信息,请参阅。请注意,方法
animate()import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import java.util.Random;
import javax.swing.*;
import sun.org.mozilla.javascript.internal.Kit;



public class Picture extends JPanel implements MouseListener, Runnable{

    private int k = 0;
    Thread MyThread;
    private DrawPic img;
 
    private Rectangle r1, r3;

    public Picture(DrawPic img, Rectangle rect1, Rectangle rect3) {
        super();
        this.setLocation(0, 85);
        this.setLayout(new FlowLayout());
        this.setSize(1280, 1024);
          
        this.addMouseListener(this);
        this.setFocusable(true);
      
        this.r1 = rect1;
    
        this.r3 = rect3;
        this.img = img;
        this.MyThread = new Thread(this);
        MyThread.start();
        this.setVisible(true);
    
    }
    
    

 
    public void paintRectangleL(Rectangle rect, Graphics g) {
        k = 3;
        
        rect.DrawRectangle(g);
        rect.FillRectangle(g);


    }
    public void paintRectangleR(Rectangle rect, Graphics g) {
        k = 1;
      
        rect.DrawRectangle(g);
        rect.FillRectangle(g);

    }
    public void paintImage(DrawPic img, Graphics g) {
        k = 2;
     
        //g.clearRect(0, 0, this.getWidth(), this.getHeight());
        img.DrawImg(g, this);
        


    }
    public void changeK(int k1){
        k = k1;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
      
        
       
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        
            Point p = r3.FindCenter();
            double dx, dy;

            dy = e.getY() - p.getY();
            r3.Move(0, dy);
              this.getGraphics().clearRect(0, 0, this.getWidth(), this.getHeight());
            this.paintRectangleL(r3, this.getGraphics());
            this.paintRectangleR(r1, this.getGraphics());
            this.paintImage(img, this.getGraphics());
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //   throw new UnsupportedOperationException("Not supported yet.");
    }
    public void animate(){
        double dx = 0, dy = 2;
        if ((this.img.getX() + 160 + this.r1.RightPoint().getX() - this.r1.LeftPoint().getX() > this.getWidth() || this.img.getX() < this.r3.RightPoint().getX() - this.r3.LeftPoint().getX())) {
                    dx = -1 * dx;
                    

                }
                if (this.img.getY() + 120> this.getHeight() || this.img.getY() < 0 ) {
                    dy = -1 * dy;
                }
                img.Move(dx, dy);
             //   this.getGraphics().clearRect(0, 0, this.getWidth(), this.getHeight());
              //  this.paintImage(img, this.getGraphics());
              //  this.paintRectangleL(r3, this.getGraphics());
              //  this.paintRectangleR(r1, this.getGraphics());
                repaint();            
    }

    @Override
    public void run() {
        
        Color col;
        while (true) {
                 animate();

                try {
                    MyThread.sleep(35);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        //   throw new UnsupportedOperationException("Not supported yet.");
    }