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

时间:2012-10-29 00:00:18

标签: java multithreading image graphics awt

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

另外一个奇怪的事情是,当我按下按钮调用它的工作方法但不是当线程运行时...

1 个答案:

答案 0 :(得分:0)

删除双斜杠。

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