Transparent JFrame - java

I have a JFrame that holds a JPanel. I want to make the frame completely transparent and the panel slightly transparent.
I'm using the following code to do so:
frame.setBackground(new Color(0, 0, 0, 0));
panel.setBackground(new Color(51, 51, 51, 190));
The desired effect is achieved, however, when I make the JFrame transparent using this method it distorts all of the other components.
The following image demonstrates this better than I can explain.
On the left is an example with no transparency and the text appears clearly. On the right the panel and the frame are set using the code above, and the text is distorted.
What am I missing?

however, when I make the JFrame transparent using this method it distorts all of the other components.
The problem is Swing thinks the component is opaque so it doesn't paint the background first so you get painting artifacts.
Check out Background With Transparency. It has two simple solutions:
paint the background yourself
use a wrapper container to do the painting for you

Related

Adding Jlabel on top of canvas

Is there any way that i can add a jlabel on top of the canvas? In my code, the constructor of my frame adds the label first before adding the canvas but when i run it it does not show the label.
I am painting the background of my canvas.
Suggestions:
Don't use Canvas objects. You've got a Swing GUI and should use the Swing equivalent -- a JPanel.
Draw the background image in the JPanel's paintComponent method as the tutorials and hundreds of examples on this site will show you.
Add the JLabel to the JPanel not to the JFrame.
Then add the JPanel to the JFrame.
Layout managers and your understanding of them are critical. Understand that a JPanel uses FlowLayout by default, and if you add a single JLabel to it, it will be placed in the center top region of the JPanel. Requisite Layout Manager Tutorial Link

JLabel and JTextArea not working

I am making a window that has a large text area and a small text area under it. This is what i have so far:
This is the code I have for it:
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon ii=new ImageIcon("textEffect.png");
Image i=ii.getImage();
g.drawImage(i,0,0,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//main method
public static void main(String[] args){
new Window();
}
//Makes window and starts bot
public Window(){
super("Pollockoraptor");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setOpaque(false);
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setLineWrap(true);
input.addKeyListener(this);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.LEFT);
window.add(scroll);
window.add(label);
window.add(input);
window.setBackground(new Color(97,118,131));
add(window);
setVisible(true);
}'
How do I make the larger textarea partially transparent so I can see the background and how do I move "Say:" to be infront of the smaller textarea?
JPanel uses a FlowLayout by default. You'll want to change the layout manager, personally, I'd recommend something like GridBagLayout, but that's just me.
See Laying Out Components Within a Container for more details
To make the JTextArea see through, you're going to have to make the JScrollPane and it's JViewPort transparent as well.
Swing only knows how to paint fully opaque or fully transparent components. You can create a translucent effect by making the component transparent and overriding it's paintComponent method and using an AlphaComposite or painting a with a Color which has an alpha value set to something below 255
For example...
Can not draw image on JTextArea background when using Nimbus Look And Feel
JTextArea not selectable, but still showing a "ghost" cursor
Swing Graphics on JFrame
And general advice...
Key Bindings over KeyListener
Override getPreferredSize of your custom component to get better results when been laid out
Use JFrame#pack over setSize, this will calculate the window size based on the needs of the content and take into account the window frame decorations as well
How do I make the larger textarea partially transparent so I can see the background
Check out Backgrounds With Transparency for problems when using a transparent background and a general purpose solution you can use to you don't need to do custom painting all the time.

Paint Applet graphics onto a JPanel

I have been trying to paint the graphics of an Applet when Applet.setVisible(false); and painting it onto my JPanel when JPanel.visible(true);
I tried to use Image i = Applet.createImage(width,height); and then paint the image to my JPanel with g.drawImage(i, 0, 0, this);
All I am left with is a blank JPanel when JPanel.paintComponent(Graphics g)... is invoked.
Is there a better approach to this?
If I'm not mistaken, the Applet won't draw when it's visibility is set to false, which explains why your JPanel is coming up blank. Is it necessary for you to hide the Applet? A typical solution would be to add the Applet directly to the JPanel by calling:
myJPanel.add(myApplet);
Of course, this mixes Swing and AWT components, which some people will object to, but it still works. Note however that a single instance of an Applet cannot be drawn to two places at once, so you cannot add your Applet to the JPanel and another place at the same time.

Java Swing JPanel Sizes

I would like to be able to have a JPanel within my JFrame of a fixed size 400x400.
I would also like the to be a 20px wide border all around it.
The main problem is the following code doesnt stick it its size.` JScrollPane runningAni = new JScrollPane(new views.cRunningAnimation(
model));
runningAni.setMaximumSize(new Dimension(400,400));
this.setSize(new Dimension(600,600));
this.add(runningAni,BorderLayout.CENTER);`
When doing this the runningAni panel just strethces accross the whole frame.
public void paint(Graphics g) {
this.setBackground(new Color(0,255,0));
}
I know this because my full frame paints itself green rather than just the JPanel (The above paint code is for my panel not the frame)
How would i create the panel so it always stays the same size and so there is always a 20px colored border around it?
BorderLayout ignores the size. You need to set a LayoutManager that either allows you to set the size to a fixed size or one that cares for the sizes set. There are different layout managers that allow this (e.g. GrindBagLayout or no layout manager at all). Some are not that easy to use (e.g. GridBagLayout). What to use depends on the rest of the layout.
You could probably use a layout panel that contains your custom panel. The layout panel needs an appropriate layout manager and could be put into the center of the BorderLayout. This would mean nearly no modifications to existing layout code.
The whole point of BorderLayout is to fill the center with the center component.
Don't override the paint() method to set the color of the panel. Use:
panel.setBackground(...);
When you create the panel.
How would i be able to set a border around my Jpanel
See How to Use Borders.
Just set your layout to null, to what ever class your adding your JPanel.
Then use the setBounds() method to set your location and size!
For example:
public class Main extends JFrame{
YourPanelClass panel = new YourPanelClass();
public Main(){
// I didn't want to put all the, everyday JFrame methods...
setLayout(null);
/*
First two coordinates indicate the location of JPanel inside JFrame.
The seconds set of coordinates set the size of your JPanel.
(The first two coordinates, 0 and 0, tell the JPanel to start at the
top left of your JFrame.)
*/
panel.setBounds(0, 0, 100, 100);
add(panel);
}
}
And i would GREATLY recommend using the paintComponent() method.
For instance:
(Obviously you put this in your JPanel's class.)
public void paintComponent(Graphics g){
super.paintComponent(g); // don't forget this if you are going to use this method.
//Basically this makes your JPanel's background green(I did it this way because I like doing it this way better.)
g.setColor(new Color(0, 255, 0));
g.fillRect(0, 0, getWidth(), getHeight());
}
Please don't forget to thumbs up if this helped!
setPreferredSize()
setMinimumSize()
setMaximumSize()
should do the trick

Create a rounded JFrame / ContentPane

I'm creating a login window with rounded corners in java. Everything is OK, see pic, but i'm having challenges making the JFrame / ContentPane transparent. There are white areas at each corner (shown by the arrows) that i seem not to be able to remove since i can't set opague to false for the JFrame or ContentPane.
Any ideas of how i can remove these white areas
Since Java 1.3 there's a trick which allows to make partially transparent windows, or windows fading in (I usually use this for my splash screens), or special FX (such as shadows):
Before opening the window, programmatically take a screenshot of the region where your window is going to be (using java.awt.Robot.createScreenCapture())
Set the screenshot as the background of your root container (JPanel with custom paintComponent() routine)
Now you can add all kinds of transparent components, or paint another semitransparent image on top of the background.
Example which creates a window with a semitransparent shadow using this technique:
http://www.eclipsezone.com/eclipse/forums/t17720.html
Not much help to you but Java7 will support transparent and shaped windows: More info here. These are available already in Java 6u10 but not publicly, ie, you need to use an unsupported com.sun... class that might change in future and break your program.
try this. its work :)
yourframe.setBackground(new Color(0, 0, 0, 180));
yourframe.setUndecorated(true);
yourframe.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 80, 80));
}
});
JFrame can not be made transparent as it is a heavyweight component. Only lightweight components such as JWindow can be made transparent.

Categories

Resources