我有这个花程序,它应该从上面放花......我需要它等待几秒钟,然后调用方法来放花。我知道该方法运行,因为我让它打印出下降的 y 值。但它不会在屏幕上绘制它。我真的不知道我做错了什么。这是一些代码...
//Ignore starting here
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
//Ignore ending here
public class RedFlower implements ActionListener{
Timer time;
ImageIcon i = new ImageIcon("graphics//flowers//red_flower.png");
Image flower = i.getImage();
int y, x;
int dy = 1;
public void dropFlower(){
Random rnd = new Random();
y = 50;
x = rnd.nextInt(1216);
time = new Timer(5, this);
time.start();
}
public void actionPerformed(ActionEvent e){// used for stoping the timer when off the screen
int checky = y++;
if(checky < 720){
y++;
}else{
time.stop();
}
System.out.println(y);
}
//everything after this is what i call in the other method
public Image getImage(){
return flower;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
现在这是我的绘图代码......
public void paintComponent(java.awt.Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(rf.getImage(), rf.getX(), rf.getY(), null);// rf is what i called the method
g2d.drawImage(sc.getImage(), sc.getX(), sc.getY(), null);//this is the basket that you control
}
还有一个奇怪的事情是当我按下按钮调用它工作的方法但不是当线程运行它时......