I'm trying to load an animated GIF in a JLabel.
While this works:
URL urlsd;
try {
urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif");
ImageIcon imageIcon = new ImageIcon(urlsd);
JLabel progress = new JLabel(imageIcon);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
This, on the other hand, does not, and I don't want to get the GIF from an URL, since I already have the GIF. Result of loading this shows only the first frame of the GIF:
try {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif")));
JLabel progress = new JLabel(imageIcon);
imageIcon.setImageObserver(progress);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
I guess there must be a reason for this, but I cannot find it.
Thanks!
Alex
You can try loading your GIF file like that:
public class Test extends JPanel {
public Test() {
ImageIcon imageIcon =
new ImageIcon(Test.this.getClass().getResource("starzoom-thumb.gif"));
}
}
Or using Test.class.getResouce() if your context is static.
Below code worked for me, it will show the animation instead of first frame of the image.
public class Test extends JPanel {
public Test() {
ImageIcon yyyyIcon = new ImageIcon(xxx.class.getClassLoader().getResource("yyyy.gif"));
connectionLabel.setIcon(yyyy);
}
}
So there is also a simpler way of doing it. The following line is how I did it.
this.setContentPane(new JLabel(new ImageIcon("Path To Gif File")));
Related
This question already has answers here:
Show an animated BG in Swing
(3 answers)
Closed 5 years ago.
I want to show a gif in java swing.
For show gif I use from these code but in both of them the gif is not moving and it like as an image is static.
first code:
void showGif() {
try {
JPanel panel = new JPanel();
BufferedImage bufferedImage = ImageIO.read(new File("address of gif");
Icon icon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(icon);
label.setVisible(true);
panel.add(label);
} catch (IOException e) {
e.printStackTrace();
}
}
second code:
void showGif() {
try {
ImageIcon imageIcon;
BufferedImage bufferedImage = ImageIO.read(new File("address of gif"));
imageIcon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(imageIcon);
JPanel panel = new JPanel();
label.setVisible(true);
panel.add(label, TOP_ALIGNMENT);
} catch (IOException e) {
e.printStackTrace();
}
}
please help me to show gif correctly.
You ImageIcon in this case will be showing just the first frame of the GIF.
You can add the GIF to a JLabel, which you can add to the JPanel. Check this out for further help.
I am trying to make an image writer that will read a template image and then write text on it, then create a new image with the new text. For some reason I am getting errors when my code seems fine to me. Here is the code below:
public class GUI extends JFrame{
private JPanel p1 = new JPanel();//Puts tiles in, and organizes them for you
private JPanel p2 = new JPanel();//Holds trash tiles
JLabel ll = new JLabel();
//private JPanel p3 = new JPanel();//Holds trash tiles
public GUI(){
this.setTitle("Tile Game");
this.setSize(600,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
try {
createTile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.add(ll);
}
public void createTile() throws IOException{
final BufferedImage image = ImageIO.read(getClass().getResource("/src/tile.png"));
//File outPut = new File("saved.png");
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString("Hello World!", 100, 100);
g.dispose();
ImageIO.write(image, "png", new File("/src/test2.png"));
final BufferedImage image2 = ImageIO.read(new File("/src/test2.png"));
ImageIcon icon1 = new ImageIcon(image2);
ll.setIcon(icon1);
p1.add(ll); }
}
When I run the code above, I end up getting theses errors:
GUI.<init>() line: 36
Start.main(String[]) line: 7
I don't understand why I am also asked to debug the GUI constructor...It all seems to work perfectly fine to me.
your image location is not suitable! (ignore the other classes, it's just a test project ^^)
make an folder in your eclipse project BUT NOT IN YOUR SOURCE dir (!!!) and then refer to the image like this:
Image img1 = Toolkit.getDefaultToolkit().getImage("img/index.png");
public class image {
JFrame pen = new JFrame();
public image () {
pen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pen.setBounds(150, 100, 613, 231);
pen.setVisible(true);
try {
URL url = new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg");
BufferedImage bI = ImageIO.read(url);
ImageIO.write(bI, "jpg", new File("C:\\kibAr.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
I dont have error but dont work why?(I want to use BufferedImage)
And how i can set the window background this graphic?
Sory for my bad english
If by work you mean display the BufferedImage on the frame, then that's because there's no code where you're actually adding it to the frame at all!
You may wish to have a look here for some examples of how to do this.
The quickest way would probably be something along the lines of:
JLabel picLabel = new JLabel(new ImageIcon(bI));
pen.add(picLabel);
I dont know why I cant add image to my JPanel
i use code:
class PanelGlowny extends JPanel {
private JLabel adam;
private JButton henryk;
PanelGlowny(){
this.setLayout(new BorderLayout());
ImageIcon imageurl = new ImageIcon("logo.jpg");
//Image img = imageurl.getImage();
adam = new JLabel(imageurl);
add(adam, BorderLayout.NORTH);
henryk = new JButton();
add(henryk, BorderLayout.CENTER);
}
}
Image is in the same folder as class, but if I use url to image it also do not add anything.
This code adding button, but do not add image :(
The problem is probably with my JDE, or Sandbox or sth like this, because code should be fine.
Try this:
imageurl = new ImageIcon(getClass().getResource("logo.jpg"));
Check How to Use Icons tutorial.
EDIT: loading remote image
Try that to load your image from web:
public static void main(String args[]) {
try {
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(new URL("http://marinerczarter.pl/wp-content/themes/twentyten/images/headers/path.jpg")));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
I am doing slideshow of images program in java using timer.
In timer event listner i have added code to chnage image but image is not changing.
Below is the code i have written
class ImagePanel extends JPanel {
private Image backgroundImage;
public ImagePanel(Image backgroundImage) {
super();
this.backgroundImage = backgroundImage;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.backgroundImage, 0, 0, null);
}
}
public class A extends JFrame{
static int counter;
List<String> imagePaths;
int nimgpaths=0;
static A frame = new A();
public static void main(String[] args) {
frame.setSize(1024, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getPath();
/* Getting required image */
Image backgroundImage = null;
String pathToTheImage = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\civ1.JPG";
try {
backgroundImage = ImageIO.read(new File(pathToTheImage));
} catch (IOException e) {
e.printStackTrace();
}
/* Initializing panel with the our image */
ImagePanel panel = new ImagePanel(backgroundImage);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.timerEvent();
//frame.show();
}
public void timerEvent(){
Timer timer = new Timer(5000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Time event occured");
if(counter > nimgpaths)
counter=0;
String imgPath=imagePaths.get(counter);
Image backgroundImage = null;
try {
backgroundImage = ImageIO.read(new File(imgPath));
}catch (Exception e1) {
e1.printStackTrace();
}
/* Initializing panel with the our image */
frame.removeAll();
ImagePanel panel = new ImagePanel(backgroundImage);
panel.repaint();
//panel.setBackground(backgroundImage);
frame.getContentPane().add(panel);
}
});
timer.start();
}
// To get path of images
public void getPath(){
DbOps db=new DbOps();
imagePaths=db.getPath();
nimgpaths=imagePaths.size();
for(Iterator i=imagePaths.iterator();i.hasNext();){
System.out.println((String)i.next());
}
}
}
Why are you using a custom panel and painting?
Your code is simply painting the image at its preferred size. This functionality is available when you use a JLabel. Then when you use the label all you need to do is use:
label.setIcon(....);
when you want to change the image. Read the section from the Swing tutorial on How to Use Icons for more information.
The only reason to create a custom component is if you plan to scale the image or do something fancy like that. If this is the case then you can use something like the Background Panel which supports scaled images as well as a setImage() method so you can change the image dynamically.
A much better design for ImagePanel would let you just replace the image, rather than removing the component. If you do have to replace a visible component, though, you have to call validate() on its container, or the new one isn't going to show up (most of the time, anyway.) I think that's your problem here.
frame.removeAll() is not doing what you would expect - it is removing the components from the frame itself rather than removing the components from the content pane of the frame. Change the code at the end of the timer's action listener to something like this to fix it:
ImagePanel panel = new ImagePanel(backgroundImage);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.getContentPane().invalidate();
frame.getContentPane().validate();
Your concept itself is wrong.
You can refresh the panel like so:
public void refreshPanel(JPanel panel){
panel.removeAll();
panel.invalidate();
panel.validate();
}
Problem:
I see in your code that you are trying to create more than one object of the same panel, which you need to refresh.
It would be better to create one panel object and refresh that object.
ImagePanel panel = new ImagePanel(backgroundImage);
Hope you can understand what I wanted to explain to you.
If you are still confused then let me know.