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.
Printable View
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.
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
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.
May be useful:
Dual Icon « Java Tips Weblog
Compound Icon « Java Tips Weblog
db
I did this but it keeps giving me error
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();
}
What error? Which line?
It compiled successfully but when I run it
I got this
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)
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.
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
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?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);
Background Panel shows how to add an imagae as a background. Then you can add any component to the panel like normal.
Cross posted
Adding a background image (Java in General forum at JavaRanch)
db
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
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);
}
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.Quote:
why doesn't my gif moves as it does on web
db