Jump to content

add image to a JFrame

erty176

Hi all, 

 

I am really stuck but I feel like this should be really easy. I am a complete java noob and this is my first JFrame. This is what I have so far and I also have that image somewhere else that I can reference too. Also using netbeans.

 package hardware.jframe; import javax.swing.*;import java.awt.*;import javax.swing.JLabel; import javax.swing.ImageIcon;    public class HardwareJframe extends javax.swing.JFrame{           public static void main ( String [] args )    {        // frame        JFrame frame = new HardwareJframe();        frame.setSize ( 1000, 1000 );        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );        frame.setTitle( "Hardware Quiz" );        frame.setVisible( true );                ImageIcon image = new ImageIcon("C:/eclipse7/workspace/Jeopardy/psu2");         JLabel imageLabel = new JLabel(image);             }                }
Edited by alpenwasser
added code tags

Does not having a second parenthesis around something bother anyone else as much as it does me? (Like if this statement was missing a second side)

Link to comment
Share on other sites

Link to post
Share on other sites

Just learn more of the language and you'll figure it out. :)

But I think maybe you're missing a few ;

that's the only thing that looks like it is missing. I'm NOT a Java coder though..... so I'm not 100% sure.

Link to comment
Share on other sites

Link to post
Share on other sites

Hi all, 

 

I am really stuck but I feel like this should be really easy. I am a complete java noob and this is my first JFrame. This is what I have so far and I also have that image somewhere else that I can reference too. Also using netbeans.

 

 package hardware.jframe; import javax.swing.*;import java.awt.*;import javax.swing.JLabel; import javax.swing.ImageIcon;    public class HardwareJframe extends javax.swing.JFrame{           public static void main ( String [] args )    {        // frame        JFrame frame = new HardwareJframe();        frame.setSize ( 1000, 1000 );        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );        frame.setTitle( "Hardware Quiz" );        frame.setVisible( true );                ImageIcon image = new ImageIcon("C:/eclipse7/workspace/Jeopardy/psu2");         JLabel imageLabel = new JLabel(image);             }                }

I might be wrong because I don't know Java, but I think you have to have the image extension on the end of the file path. So if it's a jpeg put .jpg etc.

Link to comment
Share on other sites

Link to post
Share on other sites

So the idea is to add a paint component to the object.
so for example lets say you have a label and you'd want to add a image on top of it then you'd create a new JLabel holding a paint component like so.
 

int imageXwithinLabel = 0;int imageYwithinLabel = 0;ImageIcon someImage = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("image/location/from/resource.folder")));;JLabel someLabel = new JLabel("Text in label"){	public void paintComponent(Graphics g) {		g.drawImage(someImage , imageXwithinLabel), imageYwithinLabel, null);		super.paintComponent(g);	}};

So you'd basically have a label called someLabel and inside of it you have a paintComponent which would be the graphics handler. There you manage the graphics such as drawing shapes, images, strings etc.... and then you assign the graphics by calling super.paintComponent(g);

 

If you want to read more about the graphics handler then have a look at the oracle tutorials

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so I now have this:

 

package hardware.jframe;
 
import javax.swing.*;
import java.awt.*;
import javax.swing.JLabel; 
import javax.swing.ImageIcon; 
 
 
 
public class HardwareJframe extends javax.swing.JFrame
{   
    
    public static void main ( String [] args )
    {
        // frame
        JFrame frame = new HardwareJframe();
        frame.setSize ( 1000, 900 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Hardware Quiz" );
        frame.setVisible( true );
                
    }
    
    public void ImageImport()
    {
        JLabel label = new JLabel();  
        label.setIcon(new ImageIcon("Images/psu2.jpg"));// your image here  
        panel.add(label); 
        this.setLocationRelativeTo(null);
        label.setVisible(true);
        
    }
}
 
 
and there is no errors but the image still wont display

Does not having a second parenthesis around something bother anyone else as much as it does me? (Like if this statement was missing a second side)

Link to comment
Share on other sites

Link to post
Share on other sites

 

Ok so I now have this:

 

package hardware.jframe;
 
import javax.swing.*;
import java.awt.*;
import javax.swing.JLabel; 
import javax.swing.ImageIcon; 
 
 
 
public class HardwareJframe extends javax.swing.JFrame
{   
    
    public static void main ( String [] args )
    {
        // frame
        JFrame frame = new HardwareJframe();
        frame.setSize ( 1000, 900 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Hardware Quiz" );
        frame.setVisible( true );
                
    }
    
    public void ImageImport()
    {
        JLabel label = new JLabel();  
        label.setIcon(new ImageIcon("Images/psu2.jpg"));// your image here  
        panel.add(label); 
        this.setLocationRelativeTo(null);
        label.setVisible(true);
        
    }
}
 
 
and there is no errors but the image still wont display

 

 

So the idea is to add a paint component to the object.

so for example lets say you have a label and you'd want to add a image on top of it then you'd create a new JLabel holding a paint component like so.

 

int imageXwithinLabel = 0;int imageYwithinLabel = 0;ImageIcon someImage = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("image/location/from/resource.folder")));;JLabel someLabel = new JLabel("Text in label"){	public void paintComponent(Graphics g) {		g.drawImage(someImage , imageXwithinLabel), imageYwithinLabel, null);		super.paintComponent(g);	}};

So you'd basically have a label called someLabel and inside of it you have a paintComponent which would be the graphics handler. There you manage the graphics such as drawing shapes, images, strings etc.... and then you assign the graphics by calling super.paintComponent(g);

 

If you want to read more about the graphics handler then have a look at the oracle tutorials

 

if you still don't get it then i could quickly build you a sample app

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×