使用线程调用方法时,Java 不会绘制我的图像



我有这个花程序,它应该从上方掉下花。...我需要等待几秒钟,然后调用掉落花的方法。我知道该方法可以运行,因为我将其打印出下降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
}

也很奇怪的是,当我按下按钮调用该方法起作用,而不是线程运行的方法...

删除双重斜线。

 ImageIcon i = new ImageIcon("graphics/flowers/red_flower.png");

相关内容

  • 没有找到相关文章

最新更新