Hello everybody,
I have some problems with my school assignment. I need to display a picture in a jlabel and i have already some code. Can someone give me some advice how i could get it to work.
Thanks in advance.
Greetz MeandJava
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class LoadAnImage {
public ImageIcon ImageIcon(String filename){
filename = "";
ImageIcon icon = new ImageIcon(filename);
return icon;
}
public Image getImage(String path){
Image image = null;
// TODO OOP_PO2_A1: Insert code here
return image;
}
public static void main(String[] args) throws IOException {
LoadAnImage app = new LoadAnImage();
Image image = app.getImage("chicks.jpg"); // TODO OOP_PO2_A2: Fill in the path to an existing image
JLabel label = new JLabel(new ImageIcon(image));
JFrame gallery = new JFrame();
gallery.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gallery.getContentPane().add(label);
gallery.pack();
gallery.setLocation(200,200);
gallery.setVisible(true);
if(image != null && image instanceof Image){
System.out.println("Successfully loaded the image file.");
}
else{
System.out.println("Nothing loaded. Try again.");
}
}
}