Icon bug = new ImageIcon( getClass().getResource( "bug1.png" ) );
ImageIcon bug = new ImageIcon( getClass().getResource( "bug1.png" ) );
What's the difference between these two? My book always uses the way in the first line to declare an ImageIcon but wouldn't it be better to declare it the second way because more specifically it is an ImageIcon?
Icon is an Interface, whereas ImageIcon is an implementation of that interface.
The first is better because it means you can change your ImageIcon for another implementation of Icon later without needing to change the rest of your application.
Their nature and application is different. Image is an abstract superclass of all classes that represent graphical images. ImageIcon is an implementation of Icon interface that uses Image as its source.
Think of an Image as something that could be rendered and an ImageIcon as something that will be rendered as an Icon when its paintIcon() method is called.
Quoted from here
Hope this is what you're looking for ;)
You should start by taking a read through JavaDocs for ImageIcon.
You will notice that ImageIcon implements Icon, meaning that it can be used where ever an Icon is expected (such as JButton and JLabel for example) but it also provides the means by which you can obtain an instance of Image which allows you to paint it using Graphics without the need for a Component reference, which Icon needs
see
java-imageicon-vs-image-difference
It is a thread about the difference between image and imageicon, but in the comments they also talk about Icon and such. Maybe this helps.
both are similar but the first one is the best because ImageIcon is Icon but not the reverse.When i say ImageIcon is Icon ImageIcon implements Icon.
Related
I'm still pretty new to Java, and in our programming class we are working in a group. My part of the assignment is to insert an image at the top part of the GUI.
this is the code I have so far..
ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
JPanel.add(image, BorderLayout.NORTH);
but right under the .add part of the Jpanel.add is that red squiggly line telling me that I should change my Image to a component and when I do that it tells me to switch it back to an Image?? this is what I'm getting confused on why would it tell me to change it back if it won't use it the way it is now?
So I guess my question is what should I do to fix this problem?
Also exactly how would I position it, I know it goes into the layout spot in the North but will that be dead center? or does it start from the 0,0 top left and then pixel in?
Thank you in advanced!
(P.s. this is the path to the image file if it should be different please tell me otherwise it's fine -- Project 3/Images/EXTS.png)
An Icon is not a component. You need to add the Icon to a component like a JLabel:
ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
//JPanel.add(image, BorderLayout.NORTH);
JPanel.add(new JLabel(image), BorderLayout.NORTH);
Assuming that the path to the image is correct, you should use a JLabel to show the image. See How to Use Labels for more details.
I'd also consider using ImageIO to read the image instead of ImageIcon as ImageIO will throw an IOException if the image can't be loaded for some reason. See Reading/Loading an Image for more deatils
ImageIcon image = new ImageIcon(
ImageIO.read(getClass().getResource("/EXTS.png")));
JPanel.add(new JLabel(image), BorderLayout.NORTH);
Is it correct to use JLabel always when you need to insert graphic or is other way? Im using Swing.
ImageIcon icon = new ImageIcon("icon.png");
JLabel label = new JLabel(icon);
panel.add(label);
http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html
There's always more than one way to insert a graphic. It also depends on how you want to use the graphic. If its just for an Icon that is simplest and fastest way.
Normally, JLabel is the simplest method of showing an image, especially when you don't need to add any kind of effects, rotate or scale the image dynamically
You can paint the image yourself using the 2D Graphics API and performing custom painting. This is some what more complex and I would consider what it is your want to achieve first and weigh it up with the complexity involved.
I'm using JFileChooser to load images and display them on a JPanel at run time. Is using setIcon() for JLabel a good approach for this, or is there a more adequate approach?
I'd say that a JLabel and setIcon, using an ImageIcon is the easiest way to ensure that the image is actually loaded and properly displayed in your UI.
If you simply store the file-path from the selector and draw the image with a g.drawImage(...) in a paint(Graphics g)-method somewhere, you don't know that the image actually is loaded into memory yet. Of course, this can be remedied by using a MediaTracker, but the above JLabel/ImageIcon-approach does that for you.
I have an icon for a JLabel which I can see the change for only once. When blank, a new set image for the below code works as it should. But after that, the image is stuck. No new image can replace it. When I use repaint on panelPainting without revalidate(), I get no pictures at all. Thats also weird.
Here is the code, (panelMain houses panelPainting)
//get image from somewhere
JLabel imageLabel = new JLabel();
Icon imageIcon = new ImageIcon(image);
imageLabel.setIcon(imageIcon);
panelPainting.setAlignmentX(JLabel.CENTER);
panelPainting.add(imageLabel); // default center section
//my insanity starts here
panelPainting.revalidate();
panelMain.remove(panelPainting);
panelMain.revalidate();
EDIT: I double checked that the image does change every time.
use JLabel.setIcon() as standard way, then there no reason to remove, modify and add a new JComponents on runtime
in some cases there is issue with repainting Icon in the JLabel (from external sources, www sites, etc.), then you have to call,
myIcon.getImage().flush();
myLabel.setIcon(myIcon);
use CardLayout with a few views, then any action is only to switch betweens cards
otherwise
have to call container.revalidate() and container.repaint(), as last code lines, one time, after all changes are done
for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with JLabel contains ImageIcon / Icon created on fly
I have a JFrame and I want to load a image to it when I click the load image button. Once I click next button I want to change the image. Loading image to a variable is no problem. I load it to Image img. I just want to show it. Which means i want to load it to a JLable or JPanel. How can I do this. Please help me.
Let use ImageIcon & JLabel.setIcon(...)
JLabel supports Icon/ImageIcons. This means that you can create a simple JLabel, pass an ImageIcon to it, and have it display. This should probably cover what you need.
JLabel label = new JLabel(new ImageIcon(image));
Create a custom component and override the paint(Graphics g) method. Use the given Graphics object to paint the image. You could cast to Graphics2D first if you need some more advanced options. Look through the methods defined by Graphics(2D), you'll find what you need pretty soon.
A subclass of JPanel would work well. The best choice really kind of depends on how you managed the contents of the JFrame.