So, I'm trying to create a simple loop in which a picture will start above the frame and fall below it while my health is above zero, which is always as I haven't added any way to decrease it. The problem is that every time I run it, NetBeans just returns a black screen. Is this a problem with my code or with the IDE? Here's my code so far. Thanks.
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();
}
}
}