Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Adding image to my simple window

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding image to my simple window

    Hi,

    I have made a simple window using the following code:

    import java.awt.*;
    import javax.swing.*; 
     
    class CreateWindow{
     
    	public static void main(String[]args){
     
    		//creating the window
    		JFrame frame = new JFrame("My Window");
     
    		//make the program terminate when the window is closed
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
     
    		//set the window location
    		frame.setLocationRelativeTo(null); 
     
    		//set the window size
    		frame.setSize(400,200);
     
    		//create a JLabel object (a graphical component)
    		JLabel label = new JLabel("Hello");
     
    		//get ahold of the windows content pane and add the graphical component to it
    		frame.getContentPane().add(label);
     
    		//set the window to visible
    		frame.setVisible(true);
     
     
    	}
     
     
    }

    Could you tell the simplest way to getting an image displayed in my window. I assumed I would add an image to the JLabel object, but I dont know how to do this.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,216
    Thanks
    175
    Thanked 814 Times in 757 Posts
    Blog Entries
    5

    Default Re: Adding image to my simple window

    Create an ImageIcon from the Image, make a JLabel from the ImageIcon, then add that JLabel to the GUI. See the following:
    How to Use Icons (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding image to my simple window

    Thanks, do you know of a good tutorial which shows how to make the icon move on screen when a key is pressed.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,216
    Thanks
    175
    Thanked 814 Times in 757 Posts
    Blog Entries
    5

    Default Re: Adding image to my simple window

    You might wish to read the following:
    How to Use Swing Timers (The Java Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Lesson: Working with Images (The Java Tutorials > 2D Graphics)
    Use the timer to change the icon position, then repaint. You might consider overriding paintComponent here rather than using an ImageIcon as it would give you more control over the position. Read the image using ImageIO, the draw using Graphics.drawImage within the paintComponent.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding image to my simple window

    Thanks for the links.

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,216
    Thanks
    175
    Thanked 814 Times in 757 Posts
    Blog Entries
    5

    Default Re: Adding image to my simple window

    Quote Originally Posted by TP-Oreilly View Post
    Thanks for the links.
    You are welcome. Links such as this can readily be found with a little effort using a search engine and careful choice of keywords...I'm just saying....

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding image to my simple window

    haha, i know, i have come across most of the links people have provided for me on here. I like to ask on here because people on here sometimes provide easy to understand explanations, without tooooo much technical jargon

Similar Threads

  1. Adding components to my GUI over a background image
    By derekxec in forum AWT / Java Swing
    Replies: 12
    Last Post: August 23rd, 2011, 03:01 PM
  2. Replies: 1
    Last Post: July 20th, 2011, 08:03 AM
  3. Need help adding background image (noob here)
    By OpX316 in forum AWT / Java Swing
    Replies: 18
    Last Post: July 7th, 2011, 09:10 AM
  4. image scaling on window resize
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 27th, 2010, 03:09 PM
  5. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 09:08 PM