ChrisWeldonEvans
ChrisWeldonEvans

Reputation: 21

Having trouble loading images in java, Can someone see what is wrong?

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class MainWindow extends JFrame{

    public static void main(String[] args) {

        JFrame mainWindow = new JFrame("Siege Arena");
        mainWindow.setResizable(false);
        mainWindow.setVisible(true);
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setSize(500, 500);
        loadpics();

    }   

    Image bg;

    public void loadpics(){
        bg = new ImageIcon("C:\\test\\Background.png").getImage();
        repaint();

    }
    public void paint(Graphics g){
        g.drawImage(bg,0,0,null);

    }



}

All I am trying to do is create a JFrame and load a simple image on to it, How exactly do i do that. Here I tried, and failed. Can someone help me?

Upvotes: 0

Views: 43

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Can someone see what is wrong?

  • Extendng from JFrame, you're not adding any new functionality and you're locking yourself into a single use of the component
  • Override paint of top level containers, like JFrame, it's far to easy (like you have) to break the paint chain which gives no end of problems
  • Not calling super.paint and breaking the paint chain
  • Not passing a ImageObserver to ImageIcon
  • Referencing the wrong instance of MainWindow when you try and load the image.

If I "guess" correctly, you are trying to paint a background image into a window. If so, then the way you've tried is DEFIANTLY not the way to go about it.

Instead...

  • Start with a custom component which extends from something JPanel
  • Use the paintComponent method to draw the image
  • Consider using ImageIO instead of ImageIcon. ImageIO will throw an exception if the image can't be loaded.

For example...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new BackgroundPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BackgroundPane extends JPanel {

        private BufferedImage bg;

        public BackgroundPane() {
            try {
                bg = ImageIO.read(new File("C:\\test\\Background.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bg == null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - bg.getWidth()) / 2;
                int y = (getHeight() - bg.getHeight()) / 2;
                g2d.drawImage(bg, x, y, this);
                g2d.dispose();
            }
        }

    }

}

Take a look at:

for more details...

Upvotes: 2

Related Questions