Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat am I doing wrong? Java Drawing an Image to JPanel
    primarykey
    data
    text
    <p>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.</p> <p>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?</p> <p>Code for Map class:</p> <pre><code>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(); } }); } } </code></pre> <p>And Code for the hospital class:</p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload