简体   繁体   中英

Image not loading onto JPanel?

I have been trying to figure out how to add an image to a JPanel as a background, but still have complete control over the placing of JButtons, JLabels, and etc. This is one method I found, but it is crashing and not loading the image or buttons. Here is the code:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class PanelDemo extends JFrame {
private static final long serialVersionUID = 1L;

private JButton btn1 = new JButton("EASY");
private JButton btn2 = new JButton("MEDIUM");
private JButton btn3 = new JButton("HARD");
private JButton btn4 = new JButton("High Score");

public PanelDemo() {
    super("Image Panel Demo");

    JPanel panel = new ImagePanel(
            new FlowLayout(FlowLayout.CENTER, 50, 180));

    JPanel panelbtn = new JPanel(new GridLayout(4, 1));

    btn1.setBackground(new java.awt.Color(0, 0, 0));
    btn1.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn1.setForeground(new java.awt.Color(0, 255, 102));
    btn2.setBackground(new java.awt.Color(0, 0, 0));
    btn2.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn2.setForeground(new java.awt.Color(0, 255, 102));
    btn3.setBackground(new java.awt.Color(0, 0, 0));
    btn3.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn3.setForeground(new java.awt.Color(0, 255, 102));
    btn4.setBackground(new java.awt.Color(0, 0, 0));
    btn4.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn4.setForeground(new java.awt.Color(0, 255, 102));

    panel.add(panelbtn);

    panelbtn.add(btn1);
    panelbtn.add(btn2);
    panelbtn.add(btn3);
    panelbtn.add(btn4);

    add(panel, BorderLayout.CENTER);

    setSize(640, 480);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String... args) {
    new PanelDemo().setVisible(true);
   }
 }

ImagePanel.java

import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.LayoutManager;

 import javax.swing.ImageIcon;
 import javax.swing.JPanel;

 public class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;

String imageFile = "/rsc/img/background.jpg";

public ImagePanel() {
    super();
}

public ImagePanel(String image) {
    super();
    this.imageFile = image;
}

public ImagePanel(LayoutManager layout) {
    super(layout);
}

public void paintComponent(Graphics g) {
    ImageIcon imageicon = new ImageIcon(getClass().getResource(imageFile));
    Image image = imageicon.getImage();

    super.paintComponent(g);

    if (image != null)
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
  }
}

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ImagePanel.paintComponent(ImagePanel.java:27)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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)

Also, if anyone knows of a better way to put a background image on a JPanel, pease do tell. Thank you in advance.

String imageFile = "/rsc/img/background.jpg";

I don't think you should be using the leading "/". This means look for a directory "rsc" at the root of your C: drive.

See the Swing tutorial on How to Use Icons for an example of loading an image.

btn1.setFont(new java.awt.Font("Showcard Gothic", 1, 24));

As a sidenote, why do you create a new Font object for every button. You can create a single instance and then just add that instance to all your buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM