Re: What is the absolute easiest way to display an image in Swing?

Never mind, fixed that path error. Now it is correctly reading the image, but the image will not show up. I read all your replies. But could someone just clearly explain (from here) exactly what I have to do to have it displayed? Thanks

Re: What is the absolute easiest way to display an image in Swing?

I just ran your program with my own image and it worked fine. The only difference was that I used a JPEG image. Are you certain you are not getting an error on the console. If you are using Windows do you need to put a drive letter in front of the file name path?

Re: What is the absolute easiest way to display an image in Swing?

Use the image getWidth and getHeight methods. Then use setPreferredSize on your class. Then do a f.pack(). And after you do a f.pack() you can have it centered on the screen with f.setLocationRelativeTo(null). After you do all that, then make the frame visible (otherwise, it will jump) from upper left to center.

Re: What is the absolute easiest way to display an image in Swing?

Re: What is the absolute easiest way to display an image in Swing?

Here you go hope it helps

Java Code:

import javax.swing.*;
public class myGame extends JFrame { //creates JFrame
public static void main(String[] args) {
JFrame f = new JFrame("Peter Griffin");
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageDisplayer tester = new ImageDisplayer(); //creates object Image Displayer
tester.addImage();//call method addImage inside Image Displayer class
f.add(tester);//add Image Displayer
f.setVisible(true); //let us see screen
}
}
class ImageDisplayer extends JPanel{ //creates a JPanel to display Image I refrence it as its own object in a new class out of prefrence i just find it easier
ImageIcon petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png");//Image Icon is a Image that is meant to be added to a object
JLabel petergriffinImg = new JLabel(petergriffin);//JLabels work great for holding Image Icons so we create one that holds the Image Icon
public void addImage(){
petergriffinImg.setBounds(0,0,150,150); //Set were we want it with the parameteres (x,y,width,height)
add(petergriffinImg);//adds it to the jpanel called Image displayer
}
}

Re: What is the absolute easiest way to display an image in Swing?

Originally Posted by Daryn

Here you go hope it helps

Java Code:

import javax.swing.*;
public class myGame extends JFrame { //creates JFrame
public static void main(String[] args) {
JFrame f = new JFrame("Peter Griffin");
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageDisplayer tester = new ImageDisplayer(); //creates object Image Displayer
tester.addImage();//call method addImage inside Image Displayer class
f.add(tester);//add Image Displayer
f.setVisible(true); //let us see screen
}
}
class ImageDisplayer extends JPanel{ //creates a JPanel to display Image I refrence it as its own object in a new class out of prefrence i just find it easier
ImageIcon petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png");//Image Icon is a Image that is meant to be added to a object
JLabel petergriffinImg = new JLabel(petergriffin);//JLabels work great for holding Image Icons so we create one that holds the Image Icon
public void addImage(){
petergriffinImg.setBounds(0,0,150,150); //Set were we want it with the parameteres (x,y,width,height)
add(petergriffinImg);//adds it to the jpanel called Image displayer
}
}

This opens the window, but does not show the image. Also, since this is a game, how would I set x and y coords for the jlabel?