Results 1 to 6 of 6
  1. #1
    anjali.wadhwa is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default How to insert Image using ImageIcon

    Hello,

    I am new to java and m doing a project. I am having problem to insert an image in the background.

    how to do it?

    any help plzzzzz.............

  2. #2
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    Java Platform SE 6

    I think that link must help, you can find all the methods of ImageIcon there.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Do you want to add an image in JFrame or any other container? On what you want to do this...

    Anyway you have to follow two major steps if you are using Image object, which is the easiest way that I feel.

    1. Load the image into the Image object

    Java Code:
    Image myImg = Toolkit.getDefaultToolkit().createImage(your_image_path);
    ImageIO also possible to use, please refer the Java API. I'm not remember the exact methods to use.

    2. Draw the image in paint event on your component (JPanel, etc...)

    Java Code:
    public void paintEvent(Graphics gg)
    {
        // Other stuff
        gg.drawImage(myImg, 0, 0, null);
    }
    Hope it's helpful to you.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    Java Code:
    public void paintEvent(Graphics gg)
    {
        // Other stuff
        gg.drawImage(myImg, 0, 0, null);
    }
    Make that paintComponent instead of paintEvent.

    kind regards,

    Jos

  5. #5
    anjali.wadhwa is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    Now i am able to insert image in background by using the code below:
    Java Code:
    final
    		ImageIcon imageIcon = new ImageIcon("c:/CG_Design_Fly_away_dandelion_under_blue_sky.jpg");
    
    		JTextArea textArea =
    		new JTextArea() {
    
    		Image
    		image = imageIcon.getImage();
    
    		{setOpaque(
    		false);} // instance initializer
    
    		public void paintComponent (Graphics g) {
    
    		g.drawImage(
    		image, 0, 0, this);
    
    		super.paintComponent(g);
    
    		}
    		};
    but then the problem is if m adding any more components over this image na its not working .ie. if i add text field then its not editable and labels and buttons are not even displayed....
    Last edited by Fubarable; 01-16-2010 at 12:04 AM. Reason: added code tags, but I'm not sure that it will help...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by anjali.wadhwa View Post
    Now i am able to insert image in background by using the code below:...
    ....

    but then the problem is if m adding any more components over this image na its not working .ie. if i add text field then its not editable and labels and buttons are not even displayed....
    Perhaps you really don't want to draw inside of a JTextArea, but instead draw in a JPanel, since it is unlikely that you want to add a JTextField or other component on a JTextArea.

    If you are still having problems, I suggest that you create and post a small compilable program that demonstrates your problem and can be run by us all, an SSCCE (please see the link).

    Also, if you use code tags when posting your code here, your code may be easier for others to read and you'll get more help. Please see my signature below on how to use these.

    Finally, I respectfully ask you to avoid using non-standard abbreviations in your posts. This makes them harder to read especially for those here where English is not their first language. Thanks and much luck.

Similar Threads

  1. Replies: 3
    Last Post: 02-28-2009, 10:17 AM
  2. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 08:36 AM
  3. insert image to oracle using hibernate
    By Thennarasu in forum Advanced Java
    Replies: 12
    Last Post: 01-27-2009, 04:02 PM
  4. Replies: 0
    Last Post: 02-10-2008, 12:25 PM
  5. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •