首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当使用线程调用方法时,Java不会绘制图像

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

Stack Overflow用户
提问于 2012-10-29 08:00:18
回答 1查看 298关注 0票数 0

我有一个鲜花程序,它应该从上面掉下鲜花……我需要它等待几秒钟,然后调用方法来丢弃花朵。我知道这个方法之所以会运行,是因为我让它打印出下降的y值。但它不会在屏幕上绘制它。我真的不知道我做错了什么。下面是一些代码。

代码语言:javascript
运行
复制
    //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;
}
    }

这是我的绘图代码..。

代码语言:javascript
运行
复制
    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
}

还有一件奇怪的事情是,当我让按下一个按钮的方法调用它时,它可以工作,但当线程运行它时却不能。

EN

回答 1

Stack Overflow用户

发布于 2012-10-29 08:12:00

去掉双斜杠。

代码语言:javascript
运行
复制
 ImageIcon i = new ImageIcon("graphics/flowers/red_flower.png");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13114317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档