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.
Related
I want to be able to select part of an image in an ImageIcon on JLabel and fill it with color.
Is this possible I have become a little confused as I have read that an ImageIcon is not selectable but I am not sure if that means I have to find another way of displaying the image?
Possible, yes, difficult, yes.
You need to start with a BufferedImage, which you can then wrap in a ImageIcon and apply to a JLabel.
You would then need to register a MouseMotionListener and MouseListener to the label to detect the area which was selected, you would then modify the BufferedImage accordingly and repaint everything.
Having said that, I wouldn't use a JLabel, as you can't accurately calculate the location that the label renders the icon, instead, I'd make myself a custom component, extending from JPanel and encapsulate the functionality within it and use custom painting to paint the image (and the selection area)
Start by having a look at How to Write a Mouse Listener, Performing Custom Painting, 2D Graphics and possibly Reading/Loading an Image, Writing/Saving an Image
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.
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.
I'm using Java Swing to make a GUI.
I need to present to the user some information printed on images (which are generated at run time as BufferedImage objects).
What I am doing: I put a JPanel on my JFrame, and when the system calls the paint(Graphics g) method I draw the image on g -> (g.drawImage(buffImg,0,0,null)).
The thing I do not like is: When I resize the frame, the image remains the same, I only expand the field of view. I'd like instead to make the image "stretch" with the frame when I resize it.
Is there an efficient way of doing it? (I thought I could create a new resized image, the size of the frame, each time I refresh the graphics, but I'm updating the image several times per second, so it would be a really heavy task..)
Change:
g.drawImage(BuffImg,0,0,null):
To:
g.drawImage(BuffImg,0,0,getWidth(),getHeight(),this):
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.