Swing layering - transparent component ignores underlying AWT element - java

First, to get it out of the way, I absolutely NEED to use heavyweight AWT component with a swing application. I need features from both of them.
The task is simple - render a heavyweight AWT Canvas (or any other element), render OpenGL scene directly onto it, then display Swing buttons above it for the UI.
My problem is that it works half-way.
I don't seem to have problems with Z-ordering. I am using jLayeredPanes for it, and I can move Canvas between layers and it actually works, popping on top or below other elements.
The problems are with transparency.
The thing is, Swing elements have Opaque parameter, and when it's set to false (non-opaque) - it should basically be transparent and you should see the next element below it. In my case, however, the AWT Canvas gets ignored, and you instead only see the next underlying SWING element.
Here are a couple of screenshots. They are taken from a standalone test project of mine. The canvas is stretched to the size of the frame, and in the upper left there is a JLayeredPane dummy element that is a simplified version of the menu.
On the first screenshot, the JLayeredPane's Opaque setting is set to true, and you can see that it's background property is set to Blue color.
On the second screenshot, everything is exactly the same but Opaque is set to false. Instead of displaying whatever is on the Canvas - what gets drawn in empty grey jFrame background.
Lastly, on the third screenshot I have put Canvas into a jPanel instead of leaving it on its own. As you can see, the Panel's orange color is seen through the transparent jLayeredPane, but the Canvas is yet again hidden.
Here's the code for the Frame layout. I would not post my rendering/context code right now
frame = new JFrame("AWT test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(width, height));
canvas = new Canvas();
canvas.setSize(width,height);
//this part exists only in the third example
JPanel p = new JPanel();
p.setSize(width,height);
p.setBackground(Color.orange);
p.add(canvas);
// third example end
JLayeredPane pane = new JLayeredPane();
JLayeredPane paneMenu = new JLayeredPane();
JButton button = new JButton();
button.setSize(20,20);
paneMenu.setSize(200,200);
paneMenu.add(button, new Integer(1));
paneMenu.setBackground(Color.BLUE);
paneMenu.setOpaque(false); //True for the first example
pane.add(p, new Integer(1)); // canvas for the first two examples
pane.add(paneMenu, new Integer(2));
pane.setOpaque(false);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
Could anyone please explain me what is going on and how to do what I need to do.
I will repeat again - I have to use heavyweight component as render target. I am aware of solutions like JOGL's GLPanel which is a lightweight Swing-compatible component. But I tried that method and the performance is really slow, because instead of directly rendering onto it as a context target - it reads FrameBuffer from memory, flips it, and then paints it as BufferedImage. This path is not fitting for the limited resources of an embedded system that I'll be running on.
c0der said: Please post minimal reproducible example
Errrrm.... Didn't I?
Here, you can have it in complete java class form if you want, but I literally changed some variables for constants.
import javax.swing.*;
import java.awt.*;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame("AWT test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(500, 500));
Canvas canvas = new Canvas();
canvas.setSize(500,500);
canvas.setBackground(Color.RED);
//this part exists only in the third example
JPanel p = new JPanel();
p.setSize(500,500);
p.setBackground(Color.orange);
p.add(canvas);
// third example end
JLayeredPane pane = new JLayeredPane();
JLayeredPane paneMenu = new JLayeredPane();
JButton button = new JButton();
button.setSize(20,20);
paneMenu.setSize(200,200);
paneMenu.add(button, new Integer(1));
paneMenu.setBackground(Color.BLUE);
paneMenu.setOpaque(false); //True for the first example
pane.add(p, new Integer(1)); // canvas for the first two examples
pane.add(paneMenu, new Integer(2));
pane.setOpaque(false);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
}
}
A little update:
I initially suspected that because Swing elements delegate all their drawing to the underlying heavyweight element (In my case JFrame), then what happens is that the frame generates a single frameBuffer for itself and then displays on top of Canvas. Canvas itself is not handled in this generation and thus the frame "covers" over the canvas.
That doesn't seem to be the case. I tried making the JFrame undecorated, all panels non-opaque, and display the picture. The result - canvas is still "cut", and through the hole you can see the underlying IDE menu.
This makes me think that somewhere during Drawing, the Canvas itself detects that it is obscured by another element, and that it doesn't need to draw that area. So it "optimizes" itself and doesn't update these pixels.
Maybe I'm wrong. But here's another screenshot. This is the same example as before, but I took out 3d rendering and simply trying to display Canvas with background set to Red.

Once again, going to reply to my own question.
It turned out that I need to do
setComponentMixingCutoutShape(paneMenu, new Rectangle());
for the menu pane that lies underneath the button. That essentially tells java not to cut out the element from the heavyweight underlying component.

Related

Is there a different way to draw on a JPanel?

So, I have been having trouble drawing multiple objects on a JFrame, and I know I need to use Layout managers, so I decided to test it with one object before I do multiple, however when I run this code:
fps = 30;
panel = new JPanel();
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.addKeyListener(new key());
running = true;
update = true;
ball = new Ball(0,0,1);
//panel.setBackground(Color.BLACK);
panel.add(ball);
panel.setVisible(true);
frame.add(panel, BorderLayout.CENTER);
frame.requestFocus();
frame.setVisible(true);
startTime = System.currentTimeMillis();
nothing draws (There is more to the code, i just didnt want to inlcude all of it). However, when I remove the comment and set the background to black, the JFrame turns black. So why is it that it wont draw my Ball object (which i know works) but will change the background? Is there a specific way you need to draw on a JPanel?
panel = new JPanel();
By default a JPanel uses a FlowLayout which respects the size of any component you add to the panel.
ball = new Ball(0,0,1);
I have no idea what the code in your Ball class looks like, but I would guess the preferred size is (0, 0).
You need to override the getPreferredSize() method of your Ball class to return the size of the Ball so that your layout manager can do its job.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
I know I need to use Layout managers,
The problem with a layout manager in this case is that the layout manager will control the position of the Ball which may or may not be what you want. If you want balls in random positions, then you will need to use a null layout. Then you will need to use the setSize() and setLocation() methods to control each Ball component.
Another option is to do custom painting of all your Balls. In this case you would add an object that you want to paint to an ArrayList. Then the custom painting code would iterate through the object in the list and paint them individually. See Playing With Shapes for ideas on this approach.

Putting a JLabel on top of a JLabel in Java

I would like to know how to put a JLabel on top of another JLabel at a specific position, in a class that extends JPanel in Java. People have asked for help with this before but the solutions that I found do not satisfy me. I am using GridLayout, here is some of my code:
//imports
public class Game extends JPanel implements MouseListener {
Icon background = new ImageIcon(getClass().getResource("/background.jpg"));
Icon foreground = new ImageIcon(getClass().getResource("/foreground.jpg"));
JLabel backgr = new JLabel(background);
JLabel foregr = new JLabel(foreground);
JFrame frame = new JFrame("Game");
public Game() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
backgr.addMouseListener(this);
frame.setLayout(new FlowLayout());
frame.setPreferredSize(new Dimension(200,200));
frame.pack();
frame.setVisible(true);
frame.addMouseListener(this);
backgr.setLayout(new BorderLayout());
frame.add(backgr);
backgr.add(foreground);
}
}
(I have a lot of other methods in this class, such as some mouse stuff, but I don't include them here since they are not relevant to this problem.)
Currently the code almost works as I want, the foreground picture is displayed on top of the background, right in the middle of it. But I would like the foreground picture to be displayed at specific coordinates on the background picture. I thought that if I wanted to display the foreground at coordinates (50, 50), I could say backgr.add(foreground, 50,50), this compiles but returns an error when I run the program. Any tips of what to do? It would be really appreciated.
Edit: I discovered that by saying backgr.setLayout(null), backgr.add(foregr) and then foregr.setLocation(50,50), we can do what I was looking for. But since not using a layout manager is discouraged, I am looking for better solutions, so that I can use the coordinates on the backgr Icon.
People have asked for help with this before but the solutions that I found do not satisfy with me since they require me to set the frame layout to null, but I am using GridBagLayout
The frame is using a GridBagLayout.
You are adding the foreground JLabel, so the background JLabel. The background label can use any layout (including null) that you wish.
the foreground picture is displayed on top of the background, right in the middle of it.
That is because you are using a BorderLayout and are adding the foreground to the CENTER, which is the default when you don't specify a constraint.

Draw graphics beneath components

So I am currently making a login screen that has a cool looking background effect made using the Graphics object and a 'game loop'. When I add in a JTextField though, it is seen underneath everything and not above. Is there anyway to to make the graphics draw underneath all components inside of the JFrame?
Here is an image of the graphics:
The text field is there, just underneath everything being drawn to the surface of the frame. I want to somehow reorder this so it draws underneath components.
Here is my current frame code:
frame = new JFrame("Login");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(450, 200));
frame.getContentPane().setBackground(Color.black);
frame.setBackground(Color.black);
frame.setLayout(new FlowLayout());
JTextField user = new JTextField(20);
user.setLocation(100, 200);
user.setVisible(true);
frame.add(user);
frame.pack();
frame.setVisible(true);
frame.createBufferStrategy(2);
buff = frame.getBufferStrategy();
Painter painter = new Painter();
frame.add(painter);
Any help please?
AnimationTest shows one approach. It overrides paintComponent() and invokes super.paintComponent() to ensure that components are rendered atop the background. Click anywhere to position a text field; resize to see how the default FlowLayout works. JPanel is double buffered by default using the existing buffer strategy.

JSplitPane and Canvas

As part of an application I'm writing I need to mix the old (heavyweight) Canvas with swing components - specifically nesting them inside a JSplitPane. However, when I do this the divider refuses to resize anywhere as though neither canvas will accept a reduction in size. The code demonstrating the issue is thus:
JFrame frame = new JFrame();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new Canvas(), new Canvas());
pane.setResizeWeight(0.5);
frame.add(pane);
frame.pack();
frame.setVisible(true);
I initially assumed this was a simple thing to solve, however after doing a fair bit of research the options seem to present themselves as:
Use a JPanel instead (not always an option as in my case where I'm using vlcj to render directly onto a canvas)
Insert x dodgy hack that might work in some cases
Is there a better way of doing this? Or is it really just a case of resorting to hacks? I've debated other options such as if an alternative SplitPane implementation might be available that works, but there's no heavyweight implementation and I'd be surprised if a lightweight one avoided the problem.
A JSplitPane uses the "minimum size" of the component to determine if the component can shrink when using the divider.
I've never used a Canvas before, but itt appears that the minimum size always defaults to the preferred size.
Override the getMinimumSize(...) size method of the Canvas to return a reasonable minimum.
For a quick text you can use:
Canvas canvas = new Canvas();
canvas.setMinimumSize( new Dimension(50, 50) );
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, canvas, new Canvas());
and you will be able to move the divider left, but never back to the right.

Using JSplitPane with an AWT component

I have an AWT canvas which I cannot convert to a Swing component (it comes from VTK). I wish to display a few of these canvases inside of a JSplitPane. I've read about mixing heavy and light weight components in Java and know that it's a pain in the butt, but I don't have a choice. If I wrap the AWT canvas inside of a JPanel and then put that on the split pane the split pane doesn't function at all. However, if I put the AWT canvas inside of a JPanel and then that inside of a JScrollPane and then those scroll panes on the JSplitPane the split pane does function, but the AWT canvas components don't resize properly. I'm lost about how to get the AWT canvas components to resize properly when the JSplitPane's divider is moved. I can catch the divider moving operation and operate on the AWT canvases at that time, but I don't know what to do. I've tried calling invalidate() then validate() then repaint(), but that didn't work.
Any ideas?
Here's a example of the problem
import javax.swing.*;
import java.awt.*;
public class SwingAWTError {
public static void main(String[] args) {
Canvas leftCanvas = new Canvas();
Canvas rightCanvas = new Canvas();
leftCanvas.setBackground(Color.RED);
rightCanvas.setBackground(Color.BLUE);
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new BorderLayout());
leftPanel.add(leftCanvas, BorderLayout.CENTER);
rightPanel.add(rightCanvas, BorderLayout.CENTER);
JScrollPane leftScroll = new JScrollPane();
JScrollPane rightScroll = new JScrollPane();
leftScroll.getViewport().add(leftPanel);
rightScroll.getViewport().add(rightPanel);
JSplitPane split = new JSplitPane();
split.setLeftComponent(leftScroll);
split.setRightComponent(rightScroll);
split.setDividerLocation(400);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(split, BorderLayout.CENTER);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
It is a dirty way but this will solve it:
When you call pack() without resizing the window, not much happens. So, when you first resize the window and then call pack(), your components are correcly drawn. This means you can put this dirty method in your divider moved listener method:
frame.setPreferredSize(frame.getSize()); // store the current size to restore it after packing.
frame.setSize(frame.getWidth() + 1, frame.getHeight()); // resize it!!
frame.pack();
I don't know what it is exactly but it is a strange behavour in Java...
Hope this helps until you've found a better solution...
You are kind of out of luck here. There's a pretty good article on this on the sun/oracle website:
http://java.sun.com/products/jfc/tsc/articles/mixing/
Essentially it boils down to this guideline (taken from that link, under the z-ordering heading):
Do not mix lightweight (Swing) and
heavyweight (AWT) components within a
container where the lightweight
component is expected to overlap the
heavyweight one.
Edit: I kept browsing that site and came across another link, and it would appear that the situation has improved slightly: http://java.sun.com/developer/technicalArticles/GUI/mixing_components/ But I think your case is one of those listed at the bottom in the limitations section:
Limitations
A few situations are not supported:
* Non-opaque lightweight components that have translucent
pixels (0 < alpha < 255) are not
supported. If a partially translucent
lightweight component overlaps a
heavyweight component, the heavyweight
component will not show through.
* Embedded heavyweight components must belong to the process that
created the frame or applet. The
heavyweight component must have a
valid peer within the main process of
the application (or applet).
* Advanced Swing key events, such as those events maintained in an
InputMap, might not work correctly
where lightweight and heavyweight
components are being mixed. There are
no known workarounds.

Categories

Resources