Transarent background over heavyweight components - java

I've got a problem. In LayeredPane I have 2 components: heavyweight coponent and JComponent(or any other what I could draw text in) that is placed above the first one. I'd like JComponent to have transparent background, but it has black background. Heavyweight coponent is a component that renders video and JComponent is component what I draw text in. This text should be printed with transparent background.
I tried to use GlassPane but glass content of glassPane was displayed above lightweight components and below heavyweight component.
Is there possibility to draw text above heavyweight coponent (Canvas)?
ps. I can't overload paint method because I got object of class HeavyComponent that is subclass of Canvas.

A heavyweight component will generally paint over lightweight components.
JMF is quite old. Investigate some of the newer alternatives. See the section labeled "Alternatives" at http://en.wikipedia.org/wiki/Java_Media_Framework

Related

Java Swing: how to shade a game gui after the match has finished

In a board game we are developping in Java we would like the gui to be overshadowed when the game is finished. We have a Jframe in which there is JPanel with the board on which there are some colored pawns and boxes (JButtons) and we would like that everything becomes a sort of black and white and grey. Is there an authomatic method in Java to do this in Java Components?
There are several different kinds of panes to look at that could achieve this, or something similar, if you are using Swing (which I assume from the tag, that you are).
You could use a Glass Pane. http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
glass panes could be thought of as like a CSS overlay. They dis-allow interaction with components behind them.
Also, take a look at JXLayers and JLayers, they allow you to modify the way components are actually painted.
Good Luck!
Override paintComponent() (Or if you have some components added to the container it's better to override paintComponents() method).
Call
super.paintComponent(g);
Color semiColor=new Color(0,0,0,128);//the last param represents alpha
g.fillRect(semiColor);

JScrollPane and printAll()

I am working on a JPanel extension that is only for rendering its contents to a BufferedImage, which I then use as a texture for rendering in OpenGL. For this, I create my special JPanel extension with a layout manager etc., like I would if I was building a normal Swing GUI.
In order to render it to an image, I make sure to call doLayout first and then I use the printAll(java.awt.Graphics) method to print it and the added components:
Graphics2D g = image.createGraphics();
printAll(g);
Swing itself (EDT, RepaintManager etc) is never involved, I just create the components (outside any window or frame) and directly use printAll.
This is working perfectly well for most "atomic" components (such as buttons, labels, lists), but I hit on a problem with the JScrollPane. Whatever I try, the JScrollPane will never paint its viewports (the main viewport and the scroll bars) to the given Graphics but only its border, and even manually calling printAll on the viewports yields no result.
I attached two screenshots which show a "before and after" of a simple GUI consisting of a JList and a JButton. On the first image, I use the raw JList, in the second image, I wrapped it into a JScrollPane using new JScrollPane(list).
I failed to find any magic inside the JScrollPane code related to painting its viewports. Maybe something needs to be initialized that Swing would do, but I forgot. Is there any way I can get those viewports to show up?
See Why does the JTable header not appear in the image? for some general tips about rendering components to images. Try calling combinations of validate(), addNotify() & doLayout() as mentioned in the code of that thread.
not sure from your question
1) if there is about JScrollPane or JViewport, becasue JViewport is only (from JScrollPane) about visible Rectangle on the screen,
2) every JComponent returns Graphics2D
3) please why do you call doLayout() / addNotify(), in the case that all events for Swing GUI are done on EDT, or are there some events based on Swing Timer
4) better would be edit your question with SSCCE that generated un_wanted output to the GUI
5) for JViewport you have to create and override your own RepaintManager
6) maybe stupid question, please ensure us that there isn't any animations of JComponent, meaning moving accross JScrollPane and freezed on EDT by Thread.sleep(int)
EDIT (could be most important)
7) for OpenGL you have to implements Components based on Java AWT

Transparent AWT component, possible?

I'm in the need of a translucent AWT component/container to be placed on top of other components to receive the mouse events.
The problem here is to create a translucent component. My investigation so far showed that Swing has the possiblity to do setOpaque(false) on e.g. a JPanel. Is there something similiar for AWT?
The idea is to make a ScrollPane scrollable by simply clicking somewhere and moving the mouse around. Thats why I need a transparent component to place it above all other components.
Long story short, I need a translucent component, no matter which one, to place on top of other elements to receive the mouse events.
Thanks in advance
AFAIK not possible correctly for prehistoric AWT Container and Components you can to try that with changing this code from Swing to the AWT, Translucency is supported for Swing JComponents only
The JNA project provides some utilities for making AWT components translucent. There's a WebStart demo of the functionality. The window transparency utilities are in platform.jar, which augments the base jna.jar.
However, if all you want to do is trap mouse events, you should look into the different layers that are already built into a JFrame. The component already has a concept of layers via JLayeredPane, and you can fairly easily use a JPanel as a glass pane which sits above all other components to filter events.

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.

How to make a Java Canvas look raised from its container

I have a simple GUI component written in Java. The class draws an analog clock in a java.awt.canvas.
This canvas is then contained in a JFrame - What I want to do is give the canvas a 3d "raised" effect - almost like adding a drop shadow to a photo.
Is there a simple way to do this?
If you are using a JFrame, then you have two options:
Add your own component to a JPanel first and then add this to the JFrame.
Instead of inheriting from java.awt.Canvas, you can inherit from JComponent. Then you would have to do all your painting in the paintComponent() Method instead of just paint() (you can just rename your current paint method).
In both cases you can now set a border with the setBorder() Method (on the JPanel or your component) you can get from BorderFactory.
See also: How to Use Borders
If you were using a Swing element, you'd use the createRaisedBevelBorder() method of the BorderFactory and set the canvas' border to the resulting border. Canvas is an AWT component, so you'll need to wrap it in a Swing component to which you can set the border.

Categories

Resources