1

I am creating a game and my background image loads after my frame and not with it. I don't want this way, please I need help. The main problem is that my background image takes a little time to come up when I run the program, I really want it to load with the frame simultaneously.

 //constructor of class that extends JFrame 
    public mousework2(){
      super("shoot-em-up");
     //p1 is a JPanel
      p1=new CreateImage();
      this.add(p1);
      ImageIcon img=new ImageIcon("ballfall3.jpg");
      Image minImage=img.getImage();
      this.setIconImage(minImage);
      this.getContentPane();
      Thread t= new recMove(this);
       t.start();
      this.setSize(Width,Height);
      this.setResizable(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.requestFocus(true);
      this.setVisible(true);
     }



    //the thread class
    class recMove extends Thread{
         JFrame b;
        public boolean running=true;
        //public boolean gameover=false;
          public recMove(JFrame b){
             this.b=b;
      }

          public void run(){
         while(running){
           b.repaint();
           try{
            Thread.sleep(100);
          }
         catch(InterruptedException e){}
          }

       }
    }
class CreateImage extends JPanel implements MouseListener{
//constructor
 public CreateImage(){
         super(true);
      //Background image
      ImageIcon pic=new ImageIcon("ballfall3.jpg");
      //pixMage is a gloal variable in the outer class of type Image 
      pixMage=pic.getImage();
       MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(pixMage,0);
            try {
              tracker.waitForID(0);
            }
            catch (InterruptedException e)
            {}

      pixMage=pixMage.getScaledInstance(200,-1,Image.SCALE_DEFAULT);
  }
   public void paintComponent(Graphics g){
           super.paintComponent(g);
         g.drawImage(pixMage,0,0,Width,Height,null);
}
Thirunavukkarasu
  • 208
  • 6
  • 26
  • 1
    Try using `ImageIO.read` instead of `ImageIcon`, it's blocking and won't return till the time is loaded and pass `this` as the `ImageObserver` to `drawImage`. How big is you background image anyway? Have a reference to it somewhere that we can use as a test? – MadProgrammer Jul 31 '15 at 07:18
  • Oh, and I'd avoid any methods which require it to scale the image, these will either run slowly or produce crappy output. Instead, scale the image manually, see [this example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) and [this example](http://stackoverflow.com/questions/25798156/resizing-icon-to-fit-on-jbutton-in-java/25798462#25798462) for some more ideas – MadProgrammer Jul 31 '15 at 07:24
  • Why did you change your image loading code from your last question: http://stackoverflow.com/q/31734399/131872 ? I would not start your background thread until the frame is visible. Again order matters. If your start sleeping threads then the code can't execute which may affect the loading of your image. – camickr Jul 31 '15 at 14:20
  • @camickr that's because even with the order I paid a great attention to it made no difference in this case. – BlueWizAngel Jul 31 '15 at 18:00

1 Answers1

0

To render any image onto a frame I make use of JLabels, you can do something like this:

ImageIcon pic = new ImageIcon("ballfall3.jpg");
JLabel image = new JLabel(pic);

this.getContentPane().add(image);

you can adjust the position of the image by making use of the method image.setBounds(), provided that the layout of the parent container is absolute (null) layout.