How to implement .png pics in a JFrame?

443 Views Asked by At

Hi I want to make a window, a GUI, and put an image in it.

I watched a YT tutorial (https://www.youtube.com/watch?v=Ap20Qw77TzY) and copied everything similar but the Window I make has no image at all. I tried different file types like .jpg and different window sizes, matching the picture size but it doesn't help.

That's my code, I get no real errors, except a warning of:

The serializable class main does not declare a static final serialVersionUID field of type long,line 8

This method has a constructor,line 25

Code

package main;

import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.*;

public class main extends JFrame {
/**
 * author jan
 */
public main(String title){
    super (title);
    
} 
  
public void paint(Graphics gr) {
    super.paint(gr);
    
    
    gr.drawImage(Toolkit.getDefaultToolkit().getImage("Koppenhagen\\Pictures\\Herz.png"), 0, 0, this);           
}


public static void main(String[] args) {
    
    main window = new main("Mein Test!");
    
    window.setSize(160,160);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

}
}
2

There are 2 best solutions below

7
On BEST ANSWER
  1. Use ImageIO.read over Toolkit.getImage, it will throw a IOException of the image can't be load for some reason
  2. Check the location of the image. Your example is looking for a file in Koppenhagen\\Pictures, relative to the execution context of the program. You could use File#exists to check if the file actually exists where you think it is
  3. Don't load resources within the any paint method, loading images can take time and painting should run as fast as possible
  4. I'd discourage you from overriding paint of top level containers like JFrame. A JFrame contains a JRootPane, which contains, amongst other things, a contentPane all of which can be painted independently of its parent container. Instead, start with a JPanel and override its paintComponent method instead, then add this to an instance of JFrame
0
On

Here's a simple Swing application that draws an image.

enter image description here

You have to put the image in the same directory as the Java code.

package com.ggl.testing;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class DrawImage implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane = new JScrollPane(new ImagePanel(getImage()));
        frame.add(scrollPane);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private Image getImage() {
        try {
            return ImageIO.read(getClass().getResourceAsStream(
                    "StockMarket.png"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

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

    public class ImagePanel extends JPanel {

        private static final long serialVersionUID = -2668799915861031723L;

        private Image image;

        public ImagePanel(Image image) {
            this.image = image;
            this.setPreferredSize(new Dimension(image.getWidth(null), image
                    .getHeight(null)));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }

    }

}