I've got a non-looping gif that I use as ImageIcon for two JLabels, but not at the same time.
My problem is that when I set the second JLabel's icon to be the gif, the animation has already been played, so it only show the last frame of it.
Do you know a way to get the animation when the gif is set to the second JLabel?
On the newly created icon try using:
icon.getImage().flush();
Ok,
discarded old answer. After some more searching around I found the way to do it.
ImageIcon icon = ..[the animated gif without looping]..
....
label1.setIcon(icon); //animation plays once
....
// now time to remove icon from label1 and add it to label2
label1.setIcon(null);
icon.getImage().flush(); //reset resource used by the image
label2.setIcon(icon);
....
Java Api: Image#flush()
btw. if you leave the line label1.setIcon(null); out it will still repeat the animation of the icon only for label2. label1 stays at the last frame.
Related
first time making a question here. First of all, Eclipse (as far as I'm aware of) don't let you re dimension images through the design tab directly so to solve this I made a method that convert the icon from ImageIcon to image, re dimension it and then convert it back. The problem is that when I use the re dimensioned image it looks like this in a JButton.
I have already tried to create a emptyBorder in the JButton but that only remove the border of the button, not the icon. How can I remove it?
Edit:
Just noticed that when the Window is not focused the border is not there? Image related
Just noticed that when the Window is not focused the border is not there?
button.setFocusPainted( false );
I am using the drag and drop system of netbeans to create my GUI.
I created a button and added an icon to it, my problem is that the image is to big for the size I want the button to be.
Is there any way of making this icon having the same size as the button?
Is there any way of making this icon having the same size as the button?
The size of the button is determined by the size of the Icon.
You can create a new Image by using the Image.getScaledInstance(...) method and then create an new ImageIcon from the scaled image.
Depending on your exact requirement and usage of layout managers, you might be able to use the Stretch Icon which will adjust the size of the image as the size of the button is changed.
I'm drawing an icon in another class that I call:
Image logo = new LogoRosemont().logo;
And then setting it to my JFrame :setIconImage(logo);
But the Icon is sometimes perfect, other times partial and often black.
For the record, I corrected the problem by placing the setIconImage(logo); at the end of my Constructor. The problem was that it was often not drawn completely before getting it.
It depended upon how the processing time was shared between the two tasks : JFrame drawing and Logo drawing.
Hope it helps others. ^^
Because it wont let me add an image - new account
This is utilizing the Java GUI
The above image is an image pasted onto a button, I've tried to make the button transparent so that the user can't see it but I can't seem to get rid of this blue border.
Code I have so far
boss2 = new JButton(); //declared the static button earlier on in the code
boss2.setSize(300, 300);
boss2.setLocation(315, 200);
boss2.setIcon(new ImageIcon("dragon.gif"));
boss2.setRolloverIcon(new ImageIcon("dragon.gif"));
boss2.setOpaque(false);
boss2.setContentAreaFilled(false);
boss2.setBorder(null);
Is there a way to get rid of the blue border surrounding my image?
edit - sorry for the earlier mishap, uploaded the wrong file
I would suggest that what you are seeing is the focus rectangle, used to "highlight" the button as having keyboard focus.
You can use boss2.setFocusPainted(false); to stop it from been painted.
To not have a boarder drawn for a JButton (assuming that you are using javax.swing.JButton) you can simply do:
boss2.setBorderPainted(false);
I'm trying to make a memory matching game, and I have icon images that I place onto a JButton when it is clicked. My question is, is there a way to remove the Icon from the JButton? I want to make so when a user clicks, the image is displayed, and if the second button the user clicks does not have the same image as the first button, then it disappears...any ideas?
Simple, Set the icon to null. It doesn't have an icon, so it removes it.
button.setIcon(null);
Use the following code:
JButton button = new JButton();
button.setIcon(null);
The best way to do it, is to replace the existing icon with a transparent icon of the same size. This will ensure the button does not change size, and potentially disturb the placement of other GUI elements that occur after it in the layout. E.G.
BufferedImage ourIcon = ...
BufferedImage invisibleIcon = new BufferedImage(
ourIcon.getWidth(), ourIcon.getHeight(), BufferedImage.TYPE_INT_ARGB);
Then, simply:
// use a JToggleButton instead of a JButton - it will remain pressed
JToggleButton button = new JToggleButton(new ImageIcon(ourIcon));
button.setPressedIcon(new ImageIcon(invisibleIcon));
// start a timer to change the state back, if required..