重绘 - 立即绘画和删除

TJR*_*TJR 1 java swing repaint runnable

我有一个乒乓球项目(种类),它的工作原理,但run()函数有问题.如果我用我写入面板的功能绘制框架(它们工作,我检查)它给出了图形的问题,如果我使用重绘(如我所说)它绘制框架并立即删除它,每个解决方案将有所帮助(更好的是我的代码级别中的一个简单的):

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.");
    }
Run Code Online (Sandbox Code Playgroud)

ten*_*ica 5

您不应该使用getGraphics()自定义绘画,因为它是一个临时缓冲区,可在下次重绘时回收.你画paintComponent().

有关更多信息和示例,请参阅执行自定义绘画.A部分仔细研究Paint Mechanism有一个很好的总结paint()paintComponent()方法.另见AWT和Swing中的绘画.

编辑:

程序的逻辑和结构不适合Swing的绘画过程.您需要重构您的程序,以便它可以插入该过程并在正确的时间绘制正确的东西.通常,您可以通过覆盖组件来自定义组件paintComponent().在那个方法中,所有的绘画都发生了 这个方法应该尽可能快,避免在那里放任何/太多的应用程序逻辑.

一旦状态发生变化,你应该保持一些绘制对象的状态(即坐标,颜色等)repaint().这将调度重绘,最终Swing将paint()在将调用的组件上执行paintComponent().

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

编辑:

关于线程的另一个注释 Swing有一个单线程绘画模型.所有UI交互和绘画都在Swing的事件调度线程(EDT)上进行.有关EDT的更多信息,请参阅Swing中的Concurrency.请注意,该方法animate()不在EDT上执行.你没有展示什么img.Move(dx, dy),但以这种方式执行可能不安全.invokeLater可以帮助确保代码在EDT上执行.但是,在这种特殊情况下,使用Swing计时器可能更容易,这可以确保在EDT上执行操作.