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.
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
I was wondering if anyone knows how to display multiple (probably just 2) BufferedImages overlapping each other; in other words, using BufferedImage.TYPE_INT_ARGB for the top image I want to be able to superimpose one image above the other, while still being able to view the lower image depending on alpha values.
The way I am familiar with displaying them is as an ImageIcon attached to a JLabel, ie:
JLabel myLabel = new JLabel("");
myLabel.setIcon(new ImageIcon(myBufferedImage));
As far as I know, this method doesn't seem conducive to layering BufferedImages over one another and so I am looking for an alternative method of doing this.
Alternatively if you know a way I could layer the images (without drawing one to the other) while still using the ImageIcon that would be great as well.
As far as I know, this method doesn't seem conducive to layering BufferedImages over one another and so I am looking for an alternative method of doing this.
You can add any component to a JLabel, so you could add a second label to the first label:
JLabel topLabel = new JLabel(...);
topLabel.setSize( topLabel.getPreferredSize();
topLabel.setLocation(10, 10);
myLabel.add( topLabel );
Or maybe a better approach is to use a JLayeredPane which allows you to stack multiple components on top of one another. Read the section from the Swing tutorial on How to Use Layered Panes for a working example.
Current implementation layout:
((EDIT: added
Code: ))
private JPanel panelCenter;
private List<BufferedImage> listCreatedImages;
public ChooseCircuitPanel(List<BufferedImage> listCreatedImages) {
this.listCreatedImages = listCreatedImages;
initiate();
}
private void initiate() {
setLayout(new BorderLayout(50, 50));
panelCenter = new JPanel();
LayoutManager theLayout = new GridLayout(0, 3, 0, 0);
panelCenter.setLayout(theLayout);
panelCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK));
for (BufferedImage bufferedImage : listCreatedImages) {
ImageIcon theImage = new ImageIcon(bufferedImage);
JLabel lblForImage = new JLabel(theImage);
lblForImage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panelCenter.add(lblForImage);
}
this.add(panelCenter, BorderLayout.CENTER);
}
Situation:
We want to display a race circuit here. A circuit should be displayed by placing standards tiles next to each other. It should be possible to resize the window, and with that, the circuit tiles should also resize.
((EDIT: bit more info: The race circuit data is stored on a server and the desktop application has to translate the data to a visual thing, by placing some standard tiles in the right order. ))
((EDIT: we are not allowed to use any external library. It should be doable by only using Java Swing code.))
I thought about placing the images in a JLabel and placing these JLabels in a panel with GridLayout as layout manager.
Using a GridLayout - I thought - it should be rather easy to get to a solution:
the components in the GridLayout (= JLabels) already scale and do exactly what I want.
Now, it would only be a matter of finding a way to resize the images so they fill the JLabels.
As you can see: right now, the images have a fixed size and don't scale at all.
I browsed a bit and saw lots of solutions that boil down to using Graphics2D and super.paintComponent, etc.
But most of these solutions had nothing to do with a GridLayout.
So conclusive question:
Is there an easier solution aside from using Graphics2D, etc. knowing that I use a GridLayout?
If not, I will of course use Graphics2D, etc. but I'm now just exploring my options. :)
((EDIT: SOLVED The tiles now neatly fit on each other. Don't mind the misalignments, that's our fault.))
There are no Swing components that do what you want so you will need to write your own code.
The easiest approach would be to use Darryl's Stretch Icon on your JLabel.
Or another approach is to create your own custom component that dynamically scales the image as it is painted. Something like the Background Panel which has code that allows you to scale or tile an image.
Given the nature of the view, I would recommend abandoning images altogether and instead implement the rendering in an Icon. Presuming you can make an icon scale with the label.
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.
I have a simple doubt, I need to replace this:
panel[1].setBackground(Color.red);
For an image, but I want to avoid a new jlabel for image, because I tested and I have another label inside this panel that is pushed to below
Background Panel shows two approaches. One involves custom painting, the other involves using a JLabel. The approach you use will be based on your requirement. The custom painting is more flexible but a little more complicated.