Java 绘制缓冲/消失

问题描述

我目前正在为学校创建一个程序,但我有 3 个问题: 1- 当我添加我的绘画方法时,秒表消失 2- 绘制圆圈的方法不允许我在添加绘制方法后使用箭头键移动它。 3- 这不是问题,但是当我删除绘画方法时,圆圈可以用箭头键很好地移动,时钟会按照我的意愿显示 我想知道的主要事情是为什么绘制方法会导致我的程序出现问题。 下面是代码

public class Mov extends JPanel implements ActionListener,KeyListener{
static JFrame window = new JFrame();
static JLabel clockLabel = new JLabel();
static int Add = 0;
static SimpleDateFormat clock = new SimpleDateFormat("H:mm:ss");
public static Date lastDate = new Date();
Random random = new Random();

Calendar c1 = Calendar.getInstance();
Date hour = new Date();
Toolkit toolkit;
Timer timer;
Timer tm = new Timer(5,this);
int x = 600,y = 200,velX = 0,velY = 0;


public void paint(Graphics e) {
int xpoints[] = {35,50,25};
int ypoints[] = {50,100,100};
int npoints = 3;
xpoints[0] = random(0,1215);
xpoints[1] = random(0,1215);
xpoints[2] = random(0,1215);
ypoints[0] = random(0,635);
ypoints[1] = random(0,635);
ypoints[2] = random(0,635);
Date dateNow = new Date();
// in milliseconds
long diff = dateNow.getTime() - lastDate.getTime();
long diffSeconds = diff / 1000 % 60;
if (diffSeconds > 5) { 
lastDate = new Date(); 
paintComponent(e);
e.setColor(Color.RED);
e.fillpolygon(xpoints,ypoints,npoints);
} 
}



public void show(){
   Timer timer = new Timer(40,new ActionListener() {

`

public void actionPerformed(ActionEvent evt) {
Calendar c1 = Calendar.getInstance();
   c1.set(1995,5,25,Add++);
   Date hour = c1.getTime();
   clockLabel.setText(clock.format(hour));
   clockLabel.repaint();
   }
   });
   timer.start();
   }

public Mov(){
tm.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}



public void paintComponent(Graphics p) {
setDoubleBuffered(false);
super.paintComponent(p);
p.setColor(Color.GRAY);
p.filloval(x,y,50);
if(x>1215){
x = 1215;
}
if(x<0){
x = 0;
}
if(y<0){
y = 0;
}
if(y>630){
y = 630;
}
}



 public void actionPerformed(ActionEvent e){
 x = x + velX;
 y = y + velY;
 x += velX;
 y += velY;
 x += velX;
 y += velY;
 x += velX;
 y += velY;
 repaint();
 }







public void keyreleased(KeyEvent e) {
velX = 0;
velY = 0;
}



 public static void main(String[]args){
    Mov panel2 = new Mov();
    window.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setBackground(Color.black);
    window.getContentPane().setLayout(new BoxLayout(window.getContentPane(),BoxLayout.Y_AXIS));
    panel2.setBackground(Color.white);
    panel2.add(clockLabel);
    window.setVisible(true);
    window.pack();
    window.add(panel2);
    SwingUtilities.invokelater(new Runnable() {
    public void run() {
      App app = new App();
      app.show();
    }  
    });
    }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)