Results 1 to 12 of 12
  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default Adding another icon to an icon

    Hey there, I would like to know is it possible to add an icon to a jlabel on top of another icon?
    I have added an image as a background and I want to another image on top of the background.



    any help would be very much appreciated.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Presuming the images are coming from an Image (Java Platform SE 6), read both images, get the Graphics of the background, paint the second image, and dispose of said graphics
    Java Code:
    Image image1 = //read image;
    Image image2 = //read image;
    Graphics g = image1.getGraphics();
    g.drawImage(image2, 0,0,null);
    g.dipose();
    //create image icon from image1.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  4. #4
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I did this but it keeps giving me error


    Java Code:
    try
    		{
            File file = new File("image1.png");
    File file1 = new File("colour.gif");
    Image image1 = ImageIO.read(file);
    Image image2 = ImageIO.read(file1);
    Graphics g = image1.getGraphics();
    g.drawImage(image2, 0,0,null);
    g.dispose();}
    
    catch (IOException e)
    		{
    		         e.printStackTrace();
    		}

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

  6. #6
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    It compiled successfully but when I run it

    I got this


    Java Code:
    javax.imageio.IIOException: Can't read input file!
    	at javax.imageio.ImageIO.read(ImageIO.java:1275)
    	at Fishy.main(Fishy.java:12)
    	at __SHELL10.run(__SHELL10.java:6)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at bluej.runtime.ExecServer$3.run(ExecServer.java:774)

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    That error should be pretty self explanatory...can't find the file. Make sure the file is in the correct location where you are trying to read it from.

  8. #8
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    Okay I think I get it. It compiled. what confuses me that I have located the files and they are exactly in the same folder. I tried to run it in Blue J it doesn't output anything, it just stops loading

    But what if the second image I want to take it from a url instead. So basically, I have a background image and an animated gif I got from the net on top of the background image

    I know I have to use this

    Java Code:
            URL url = new URL("http://www.floatingbanana.com/artbackwash/bulltrout.gif");
            Icon icon = new ImageIcon(url);
            JLabel label = new JLabel(icon);
     
            JFrame f = new JFrame("testing");
            f.getContentPane().add(label);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    If I run this, I would only get the animation itself. How do I put another image as a background to this behind this image?

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Background Panel shows how to add an imagae as a background. Then you can add any component to the panel like normal.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  11. #11
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    Ok so I tried a different way of doing this. but I don't whether this is the right approach to it.

    but I have one more question, why doesn't my gif moves as it does on web

    Java Code:
    Image image1,image2;
     public Testing ()
     {
         
          try
         {
             File file = new File("photo1.png");
             URL url = new URL("http://noncon4mist.com/wp-content/uploads/2009/08/Triple-Spiral-Labyrinth-animated.gif");
                //File file1 = new File(url);
             image1 = ImageIO.read(file);
             image2 = ImageIO.read(url);
             Graphics g = image1.getGraphics();
             g.drawImage(image2, 0,0,null);
             g.dispose();
         }
    
         catch (IOException e)
            {
                     e.printStackTrace();
            }
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new FlowLayout(FlowLayout.LEFT));
            JPanel panel1 = new JPanel();
    
            Icon icon = new ImageIcon(image1);
            JLabel label = new JLabel(icon, JLabel.LEFT);
     
       
            panel1.add(label);
            panel1.add(button1);
           getContentPane().add(label);   
    }
    
        public static void main(String[] args) 
          {
              new Testing().setVisible(true);
         }

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    why doesn't my gif moves as it does on web
    ImageIO#read returns a BufferedImage. To load an animated image, use Toolkit and MediaTracker, or construct an ImageIcon with a String or url. To paint an animated background without using ImageIcon, you will have to monitor the image with an ImageObserver.

    db

Similar Threads

  1. Icon on Jar file
    By justbeller in forum AWT / Swing
    Replies: 4
    Last Post: 03-18-2011, 08:04 PM
  2. Image icon in SWT
    By ashin in forum SWT / JFace
    Replies: 3
    Last Post: 07-11-2009, 02:21 AM
  3. set.Icon
    By safiya in forum NetBeans
    Replies: 2
    Last Post: 10-17-2008, 06:46 PM
  4. Yes No Icon MessageBox in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:57 PM
  5. icon
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2008, 07:34 PM

Posting Permissions

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