为什么我在Swing中的代码只返回黑屏?

时间:2017-09-07 06:04:16

标签: java swing

所以,我正在尝试创建一个简单的循环,其中一张图片将从框架上方开始并且在我的健康状况高于零时落在它下面,这总是因为我没有添加任何方法来减少它。问题是,每次运行它时,NetBeans都会返回黑屏。这是我的代码或IDE的问题吗?到目前为止,这是我的代码。感谢。

import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FastFoodGame extends JPanel  
{
    static int y = -100;
    static int health = 3;
    Random ran = new Random(1023);
    Random run = new Random(99);
    Image burger;
    public static void fall()
    {
        y = y + 5;
    }
    @Override
    public void paint(Graphics g)
    {
        while(health > 0)
        {
            y = -100;
            try 
            {
                TimeUnit.SECONDS.sleep(1);
            } 
            catch (InterruptedException ex) 
            {
                Logger.getLogger(FastFoodGame.class.getName()).log(Level.SEVERE, null, ex);
            }
            if(run.nextInt() <= 74)
            {
                burger = new ImageIcon("Classic.jpg").getImage();
                g.drawImage(burger, ran.nextInt(), y, 100, 100, this);
            }
            else
            {
                burger = new ImageIcon("Moldy.jpg").getImage();
                g.drawImage(burger, ran.nextInt(), y, 100, 100, this);
            }

        }
    }
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Fast Food Game");
        frame.add(new FastFoodGame());
        frame.setVisible(true);
        frame.setSize(1024, 768);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        while(health > 3)
        {
            fall();
        }
    }              
}

0 个答案:

没有答案