0

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();
        }
    }              
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Kantha
  • 1
  • 3
    Running a while in the `paint` method is NOT how custom painting should be done - you're block ing the EDT preventing it from painting anything or responding to any new events – MadProgrammer Sep 07 '17 at 06:12
  • 3
    Start by having a closer look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html), [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [How to Use Swing Timers](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for a possible solution – MadProgrammer Sep 07 '17 at 06:13
  • 3
    And some understanding of [Concurrency in Java](https://docs.oracle.com/javase/tutorial/essential/concurrency/) would't hurt – MadProgrammer Sep 07 '17 at 06:14
  • If this is not a duplicate, please [edit] your question to include a [mcve] that shows your revised approach. – trashgod Sep 07 '17 at 09:23

0 Answers0