我有一个乒乓球项目(有点),它可以工作,但是 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.");
}