jpanel with image in background - java

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.

Related

Displaying Overlapping BufferedImages in Java

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.

Add graphic using a JLabel

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.

Dynamically resize images linked to JLabels in a GridLayout layoutmanager

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.

How to change a JPanel property without affecting the original JPanel?

I have a Java desktop application that at certain point shows up a JPanel with a GridBagLayout and others JPanels inside the grid. This JPanel is showed with a certain color. What I want is to export this JPanel to PDF (I'm using iText) with another color.
No problem with the export (the PDF is generated with the JPanel in its original color) but I'm not sure of the right way to go for changing the JPanel color on the PDF.
My first approach was to set the backgroung color of the original JPanel for the color intended to go on the PDF. I have an utilitary class to generate my PDFs with a method that receives a JPanel and on this method, I made jPanel.setBackgroundColor. This works, but the problem (maybe obvious for most) is that it also changed the original JPanel showed on the application. I thought that invoking this method passing the JPanel would be Java pass-by-value. After some reading, I now understand that it is indeed a pass-by-value, but the value of the pointer to the object JPanel, and not really the object, thus changing it's property, also changes the original.
Another approach was to clone the original JPanel. But then noticed the JPanel is not Cloneable. Also thought of recreate the original JPanel over a new JPanel, but it's getting too complex.
So I'd like to have some opinions on which would be the right way to go.
Thank you very much in advance.
Cheers!
One simple, but slightly different solution would be to wrap another panel around your panel before showing it on your GUI and before printing it.
If you use a different Panel for GUI and for printing, you can use two different background colors (and also different other settings).
Of course this is not a strict solution to the stated problem, but probably a rather good workaround.

how add different shapes partially on JPanel+image which is on JPanel

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.

Categories

Resources