swing doesn't show png icons on jbuttons - java

I have written a simple JFrame in java and added a button.
Then I added a .png icon to the button but it keeps giving me an exception.
(I don't have any problem with .jpg icons and it works well)
Here are my code and the exceptions.
public class Test
{
static JFrame mainFrame;
public static void main(String[] args)
{
mainFrame = new JFrame("a");
mainFrame.setSize(300, 300);
mainFrame.setLocation(50, 50);
JButton btn = new JButton();
ImageIcon icon = new ImageIcon("C:\\a.png");
btn.setIcon(icon);
btn.setSize(100, 100);
btn.setLocation(50, 50);
mainFrame.add(btn);
mainFrame.setVisible(true);
}
}
Exceptions :
sun.awt.image.PNGImageDecoder$PNGException: Broken file
at sun.awt.image.PNGImageDecoder.pngassert(PNGImageDecoder.java:94)
at sun.awt.image.PNGImageDecoder.handleChunk(PNGImageDecoder.java:107)
at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:726)
at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:252)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
sun.awt.image.PNGImageDecoder$PNGException: Broken file
at sun.awt.image.PNGImageDecoder.pngassert(PNGImageDecoder.java:94)
at sun.awt.image.PNGImageDecoder.handleChunk(PNGImageDecoder.java:107)
at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:726)
at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:252)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

Code is fine, there's problem with your image file

Related

How do I import an image in intellij (JAVA)

I'm fairly new to coding and was trying to make a simple GUI with JAVA Swing. I wanted to place an image in a label but for some reason does the image does not show when I run the Code. Are there some requirements I don't know of or did I do something wrong?
package Part2Lables;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//Label = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("Kakashi.png");
JLabel label = new JLabel(); // create a label
label.setText("This is text in a label"); // set text to the label
label.setIcon(image);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.add(label);
}
}
This is the code I used (I did use imports but they are not visible in the pic.)

Java cannot see jlabel in jframe nor imageIcon

I'm writing a simple Java code that shows a text inside a frame and substitutes the standard ImageIcon with a custom one (.png file), stored in the same directory of this Java file.
However, whenever I run the code no text appears inside the frame and I also noticed that no Image is shown as Icon.
I'm running Java on VsCode and the code is as follows:
public class Example{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(420,420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("icon.png");
frame.setIconImage(image.getImage());
Jlabel label = new Label();
label.setText("Hello there!");
frame.setVisible(true);
}
}
Needless to say that I'm importing javax.swing.ImageIcon, javax.swing.JFrame and javax.swing.JLabel.
Whenever I run the above code this is the frame that appears:
May this be a problem due to Virtualization not being set on on my computer?

JFrame don't work when adding an Image

I'm having a problem that seems a little bit strange. When I'm adding a new ImageIcon and try to run the program it just gives me a gray screen and no objects are added.
public class Ctester {
public Ctester(){
Frame();
}
public void Frame(){
JFrame fr = new JFrame();
fr.setVisible(true);
fr.setSize(500, 500);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setResizable(false);
JPanel p = new JPanel(new GridBagLayout());
ImageIcon icon = new ImageIcon(getClass().getResource("zippo.jpg"));
JLabel l = new JLabel(icon)
JButton bm1 = new JButton("hellu");
p.add(l);
p.add(bm1);
fr.add(p);
}
public static void main(String[]args){
new Ctester();
}
}
But if I remove the line:
ImageIcon icon = new ImageIcon(getClass.getResource("zippo.jpg"));
then it works perfect.
I'm not getting any error messages and i been looking for a while but I could only find that the problem might be something with the gridbaglayout.
How can i solve it or do I have to change layout?
(this is just a simple code based of the original as an example so any solutions that not include having to change layout is highly appreciated)
Most of the code is wrong:
Swing components should be create on the Event Dispatch Thread (EDT).
The frame should be made visible AFTER all the components have been added to the frame.
You are attempting to use a GridBagLayout, but you aren't using any GridBagConstraints when you add the components.
Method names (Frame) should NOT start with an upper case character.
Read the Swing Tutorial for Swing basics.
You can find working examples in:
How to Use GridBagLayout
How to Use Icons
so any solutions that not include having to change layout is highly appreciated
Start with the working examples and make changes for your requirements. If you start with better structured code you will have less problems.
If something draws correctly after a window resize or minimize/maximize that is a sure sign of a race condition because you are not starting your GUI on the event dispatcher thread. Your main problem is you are calling setVisible() way to early, don't call setVisible() until after you have added all components to your top-level container. The other problem is you are not starting your GUI on the event dispatcher thread. Please see the main method below in the fixed code:
public class Ctester {
public Ctester() {
Frame();
}
public void Frame() {
JFrame fr = new JFrame();
fr.setSize(500, 500);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setResizable(false);
JPanel p = new JPanel(new GridBagLayout());
JLabel l = new JLabel("label");
JButton bm1 = new JButton("hellu");
p.add(l);
p.add(bm1);
fr.add(p);
fr.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Ctester();
}
});
}
}
Try this code you might want to put that first line of code in a try catch just in case that it doesn't find the image.
URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
fr.setIconImage(icon.getImage());
Also use a .ico file if you are only using this program on Windows but use a .png if it is going to be multi-platform

How can i change the speed of GIF file

public class Main {
public static void main(String[] args) throws MalformedURLException {
URL url= new URL("<file path>");
Icon icon= new ImageIcon(url);
JLabel label= new JLabel(icon);
JFrame mainFrame = new JFrame("Test");
mainFrame.getContentPane().add(label);
mainFrame.setSize(800, 600);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
}
}
how can i change the speed of Gif file ?
i tried to find how to change the speed of JFrame frame but i didn't find anything
You can't. All you can do is create another gif based on your current one. Use ImageMagick for that
Detect current speed:
identify -verbose your.gif | grep Delay
Delay: 5x100
...
Create a new gif:
convert -delay 10x100 your.gif your_slow.gif

Why does Windows LookAndFeel make fonts too small?

The native Windows LookAndFeel in Java 6 seems to incorrectly size some fonts.
Test program:
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Test {
public static void main(String[] arg) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
final JMenuBar mb = new JMenuBar();
final JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
mb.add(file);
final JToolBar toolbar = new JToolBar();
final JButton button = new JButton("Button");
toolbar.add(button);
final JLabel label = new JLabel("Basic Colors");
final JPanel panel = new JPanel(new BorderLayout());
panel.add(toolbar, BorderLayout.PAGE_START);
panel.add(label, BorderLayout.CENTER);
final JFrame frame = new JFrame();
frame.setJMenuBar(mb);
frame.add(panel);
frame.setTitle("Test");
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Output, compared with a native Windows app on Vista:
While the text in the test app menu bar is sized correctly, the rest of the text is smaller than in the native app next to it. Zoomed in, you can see that the text in the test app JLabel is 1px shorter than in the native app:
Is this a bug in the Windows LaF, or are we misusing it? If it's a bug, is there a known workaround?
Java 6 uses its own font renderer, including the implementation of subpixel antialiasing / hinting whatsit. While the output is meant to be similar to Windows' rendering, either the top of the B being at a pixel boundary, or it being rounded, or both, throws the Java renderer off. The Windows font renderer decides to place the top of the letter above the boundary, while the Java one places it below. The 'l' looks like it's at the same height in both samples, so it doesn't seem like the renderer gets the height of every letter wrong. Maybe try comparing with some letters where the top is a straight line, like a T or an E?

Categories

Resources