简体   繁体   中英

What am I doing wrong? Java Drawing an Image to JPanel

This is for an assignment, so I'd rather not ask for help, but I can't seem to see what I'm doing wrong. The code will eventually create a window, with an image as the background, and then using information in a text file, place other images at specific points, and the user can zoom in.

At the moment I'm just trying to get an image displayed onto a JPanel inside a JFrame, and I can't seem to get it to work. Could somebody please point out what exactly I'm doing that's leading to the image not displaying?

Code for Map class:

import javax.swing.*;

public class Map extends JPanel
{   
    static final long serialVersionUID = 1;

public Map()
{
}

public JPanel createContentPane()
{
    //Creating a base JPanel to place everything on
    JPanel rootGUI = new JPanel();
    //Setting the Layout Manager to null to place everything manually
    rootGUI.setLayout(null);
    rootGUI.setOpaque(true);
    return rootGUI;
}

private static void createAndShowGUI()
{
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Test for image");
    //Create and set up the content pane
    Map demo = new Map();
    frame.setContentPane(demo.createContentPane());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setResizable(false);
    Hospital hDemo = new Hospital();
    frame.add(hDemo);
    frame.setVisible(true);
}

public static void main(String[] Args)
{
    //Schedule a job for the event-dispatching thread
    //Creating and showing this applications GUI
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            createAndShowGUI();
        }
    });
}
}

And Code for the hospital class:

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

public class Hospital extends JPanel
{
static final long serialVersionUID = 2;
public static BufferedImage hospitalImage;

public Hospital()
{
    super();
    try
    {
        hospitalImage = ImageIO.read(new File("src\\hospital.jpg"));
    }
    catch (IOException ex)
    {
        //Not handled
    }
}

@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    g.drawImage(hospitalImage, 50, 50, this);
    repaint();
}
}

You haven't defined the size of your Hospital Panel.

update to:

public Hospital()
{
    super();
    setSize( /* size */ );
    try
    {
        hospitalImage = ImageIO.read(new File("src\\hospital.jpg"));
    }
    catch (IOException ex)
    {
        //Not handled
    }
}

or use a different layoutManager in your JFrame/ContentPane like Borderlayout

in that case you could add the Hospital to your Frame as

frame.add(hDemo, BorderLayout.CENTER);

visual guide to layoutmanagers

Problem is in path you are passing here: hospitalImage = ImageIO.read(new File("src\\\\hospital.jpg"));

Easy solution would be to load your image as a stream:

InputStream stream = getClass().getResourceAsStream("hospital.jpg");
hospitalImage = ImageIO.read(stream);
stream.close();

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