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.
Related
I was looking for a way to rotate JLabel vertically and I found that several posts related to this topic suggest to use Graphics2d. But, in this way, the size of my JLabel is inconsistent (widht & height inverted).
I found also that another user, here, suggested this code.
Actually, the code works, but there are no indications about how to align the text of the JLabel, and this is what i get:
Can anyone help with any of the two methods (controlling size in method one or 1 aligning text in method 2)?
Thank you very much in advance.
One way is to create an Icon of the text and rotate the Icon then add the Icon to the label. Then the size of the label will be calculated normally.
Check out the Rotated Icon class for an example of this approach. You will also need the TextIcon class.
These two classes may seem like extra work, but it is an example of how to create reusable classes to you don't do custom painting all the time.
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.
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 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.
My Project is in Java Swing.
I have a JPanel on which I am adding some images with .png extension (which are on JLabels) at center.
Now I want to add a line which will be partially on the JPanel & partially on that image.
Currently when I am adding a line, JPanel shows the line but when I resize the image & drag it to the image, the image hides the line.
What can be done so that the image doesn't hide my line & shows it on image?
You're probably better off drawing the image yourself and drawing the line over the top in the same control. Create a class that extends Canvas and in the paint method write your own code to paint the image and then draw the line.
Another option is to use JLayeredPane instead of JPanel as your main container and place a non-opaque (setOpaque(false)) JPanel on a higher layer
Use JLayeredPane.setLayer(yourPanel, highNumber) and fill your JLayeredPane using something like GridBagLayout or a simple custom LayoutManager.
You can then implement the custom painting on that panel.
You could try using JXLayer and defining a custom LayerUI for it that would draw the lines. These would then appear above the components you need to draw over.
This is a little more advanced and involves using a 3rd party (open source) custom component but will allow you to change you mind about what Swing component you use to render your images later.
I think this article best describes how to achieve what you want.
I've solved many similar issues to this in the past in a variety of ways and none have had the flexibility and maintainability of JXLayer.