Lightweight on top of heavyweight - java

I have a JFrame that has a JLayeredPane. The JLayeredPane contains a heavyweight component (Ardor3d AWT canvas). I am trying to display a JPanel on top of the heavyweight component. This works perfectly until I set the size of the frame to the size of my monitor. The heavyweight seems to draw over the Jpanel that I want on top.
Any ideas?

This might help you: http://java.sun.com/developer/technicalArticles/GUI/mixing_components/

Yes, this is why you should not mix heavyweight and lightweight components. Extend JComponent and override paintComponent() instead of using a canvas.
You mention Ardor3d, I assume that is some sort of 3rd party component. If there is not a Swing version I would suggest finding something else to meet your need.

Related

How to use Postprocessing on an awt or swing window

Is it possible to use Post Processing effects like gaussian blur or greyscale effects on an entire JFrame independent from its contents?
I would guess you are looking at the JLayer class. See How to Decorate Components with the JLayer Class for more information and examples.
Another option might be to use a Glass Pane. See the glass pane demo from How to Use Root Panes.

Is it possible to open JFileChooser over Canvas?

Is there any alternative way to open a JFileChooser in which a JFrame only contains a Canvas?
I don't think so.
Taking a look at the documentation of Canvas, seem the only component one can add is a PopMenu
not possible without dirty hacks, I'd suggest to use JPanel instead of Canvas,
lightweight Swing JComponents are behind AWT Components,
for painting to Swing JComponents to use paintComponent instead of paint
I'd suggest don't mixing AWT Components (Canvas) with Swing JComponents (JFrame or JFileChooser)

Is it appropriate to extend JPanel to create a drawing surface for a 2D game?

I'm converting a 2D game from an Applet to a JFrame. I know I should add a JPanel to a JFrame and use the JPanel as my drawing surface but I'm not sure exactly what I should be subclassing. I was trying it with JFrame but I've read that it's usually unnecessary and a bad idea to extend JFrame (I wasn't adding any extra functionality anyway, so that makes sense). I need to override the paintComponent() method of JPanel so right now I'm leaning towards extending JPanel. Is there a better way of creating a 2D game surface or is extending JPanel and adding that to a JFrame an appropriate way to do it?
No, there is no better way, that's a very good option.
If you would extend a JFrame (the other option), your animation would blink on most windows family OS.
JPanel is a pure swing component and reacts better in java than a peered component like a JFrame.
Your choice is good, go ahead and have fun programming.
update
My knowledge in this area has grown since I originally answered the question and I agree with #Snicolas that you should use a JPanel.
JPanel (because it's a swing component) has builtin support for double buffering (AWT does not) and has better performance (so I hear, but I have no first hand experience with this).
You may find this article useful: http://www.oracle.com/technetwork/java/painting-140037.html#swing
original answer:
I recommend you subclass Canvas or Component or JComponent. My preference is Canvas. You should read the 2d graphics tutorial.
update
Use Canvas because it receives all the user input events and doesn't have the baggage of being a container. read more. Panels don't receive all mouse input events, for instance.

Painting a JComponent without adding it to a container

I've implemented a custom JPanel, whose paint method I've extended to do a lot of manual rendering in full screen mode. Now I would like to integrate another JComponent to this (in my case a JPanel that contains a JScrollpane with a JTextPane as its viewport) that should appear on top of my first panel, but because my custom rendering pipeline is complex, adding the JComponent to my panel and having it painted the traditional way through the AWT system is not an option (I tried and it's quirky at best, not functional at worst), so my question is: is it possible to manually order the JComponent to be painted at one point in my program by calling its regular paint method without tying it to a JContainer and if yes, how do I do this?
Thanks in advance for your answers.
See the LabelRenderTest.java source on this thread. The label is eventually drawn to screen, but it is painted to BufferedImage before ever being displayed.
The important line of the source is..
textLabel.setSize(textLabel.getPreferredSize());
You can take a look at CellRendererPane and see how for example BasicTableUI paints component images with it.
Yes, just call the normal paint method on the object and pass the Graphics you want it to paint on. However, this is just going to paint it and it sounds like you want it to possibly scroll which means you will need to add it to your custom JPanel. In that case just add the panel and you a layout manager that will place the component where you need it.
You should set size for the component. Then to position it use your Graphics' translate(x,y) to position the component in desired Point.
if there is any container higher level in the hierarchy you can use
validate(); repaint();
pair to do that.
if not you can change it's size or bounds ( like +1 , -1 ) at the end to make it repaint itself.

Swing: Is there a simple way to make 1 component ignore the layout manager?

I have a JPanel with one component that I want to place in an absolute sense, whereas the rest of the components are placed according to a layout manager.
Is there a simple way to do this?
Are you saying you want a component painted over top of all the other components? If so then you would need to use a JLayeredPane.
Why don't you post a SSCCE that demonstrates what you want to do?
You can add components to a frame as you would do normally and make the frame visible. Then you can add this random component and use setBounds on the component. As long as you don't revalidate() the panel or resize the frame we will be able to see how you intend to position this component relative to all the other components.
You might also want to look at OverlayLayout, seen here. For some reason it's excluded from the conventional gallery, but it may be of interest.
You can do this with only needing one JPanel using MigLayout

Categories