Skip to main content

JPanel background image with JXLayer (newbie question)?

6 replies [Last post]
digz6666
Offline
Joined: 2007-05-20
Points: 0

Hi all,
I'd like to use jxlayer as a background image of JPanel.

How can I do that?
Could you please provide some examples?

By the way I've tried to use substance watermark but there's some issues, I can't use different watermarks on different components. Instead watermark applies to all components.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
digz6666
Offline
Joined: 2007-05-20
Points: 0

The component colors must not be affected.

I've marked this question as answered and then not answered.
So this reply added automatically.

Message was edited by: digz6666

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello digz6666

Is it what you are asking about ?

[code]
import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.plaf.AbstractLayerUI;

import javax.swing.*;
import java.awt.*;

public class BackgroundDemo {

private static void createGui() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.add(new JButton("JButton"));
panel.add(new JTextField(10));
panel.add(new JLabel("Hello"));

JXLayer layer = new JXLayer(panel);
frame.add(layer);

layer.setUI(new AbstractLayerUI() {
protected void paintLayer(Graphics2D g2, JXLayer l) {
super.paintLayer(g2, l);

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f));
g2.setPaint(new GradientPaint(0, 0, Color.YELLOW, 10, 10, Color.GREEN, true));
g2.fillRect(0, 0, l.getWidth(), l.getHeight());

}
});

frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
BackgroundDemo.createGui();
}
});
}
}
[/code]

Thanks
alexp

digz6666
Offline
Joined: 2007-05-20
Points: 0

Thank you for good typical demo.
Is it possible to not affect component colors?

I mean the graphic is behind the components.

Message was edited by: digz6666

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello digz6666

I am not sure I understand your question,
if you don't want to affect the component's colors - don't do it,
paint whatever you want (a semi-transparent image for example)

Thanks
alexp

digz6666
Offline
Joined: 2007-05-20
Points: 0

I know another alternate which is to use Substance watermark (specially image watermark).

I'd like to know is it possible in jxlayer.

Here's the screenshot of substance watermark example (never used null layout, used tablelayout, it's possible to place jlabel with image under jcomponent, i need another approach):
http://i43.tinypic.com/mmg104.jpg

And I've used this image as a background.
http://i40.tinypic.com/jq0ot4.jpg

Message was edited by: digz6666

Message was edited by: digz6666

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello digz6666

There is no ready-to-use solution with JXLayer to implement the watermarks,
you should implement a custom LayerUI for that

Thanks
alexp