user1817988
user1817988

Reputation: 233

Java swing, cannot get an image to work

I have wrote a simple program just to draw an image, I cannot get it to work at all. It should just show 1 picture within a pane within a frame. p.s. there are prolly imports i do not need, i tried many different things.

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;


import javax.imageio.ImageIO;

public class ShowImage {
private Graphics g;
private BufferedImage lionImage=null;
private JFrame frame;
private JPanel totalGUI,values;

 public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }

   });
   }
 public JPanel createContentPane(){
     totalGUI = new JPanel();


        totalGUI.setLayout(null);

        values = new JPanel();
        values.setLayout(null);
        values.setLocation(10, 10);
        values.setSize(490, 290);
        values.setBackground(Color.WHITE);
        totalGUI.add(values);
        getImage();
        Graphics g = values.getGraphics();

        g.drawImage(lionImage,100,100,null);

        totalGUI.setOpaque(true);
        return totalGUI;
 }


    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Calculator");

        ShowImage demo = new ShowImage();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setVisible(true);

}

void getImage(){
    try{
    lionImage =ImageIO.read(new File("imgres.jpg"));// *see note
    }catch (IOException e){}
    }

}

I get the error. Which I have no idea what the problem is.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ShowImage.createContentPane(ShowImage.java:43)
at ShowImage.createAndShowGUI(ShowImage.java:55)
at ShowImage.access$0(ShowImage.java:50)
at ShowImage$1.run(ShowImage.java:23)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Upvotes: 0

Views: 143

Answers (2)

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13900

public static void main (String [] args) throws Exception 
{
    final BufferedImage lionImage = ImageIO.read (new File ("imgres.jpg"));

    JComponent image = new JComponent () 
    {
        @Override
        protected void paintComponent (Graphics g) 
        {
            super.paintComponent (g);

            g.drawImage (lionImage, 0, 0, null);
        }

        @Override
        @Transient
        public Dimension getPreferredSize () 
        {
            return new Dimension (lionImage.getWidth (), lionImage.getHeight ());
        }
    };
    image.setOpaque (true);

    JFrame frame = new JFrame ("Image");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane ().setLayout (new BorderLayout ());
    frame.getContentPane ().add (image, BorderLayout.CENTER);
    frame.pack ();
    frame.setVisible (true);
}

Upvotes: 1

Dan D.
Dan D.

Reputation: 32391

This is because your g is null. This happens when the path to the JPanel is not yet displayed on screen.

In order to draw an image inside a JPanel, you would usually create a new class that extends JPanel and override its paintComponent(Graphics g) method. See this for an example, just that you would need to call the drawImage() method.

I suggest you never catch an Exception and don't handle it in any way. You do this in the getImage() method where you try to load the image.

Upvotes: 1

Related Questions