JPanel transparency problem - java

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)
Swing does not support transparent backgrounds.
Swing expects a component to be either:
opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.
The setOpaque(...) method is used to control the opaque property of a component.
In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.

This related example also makes the JPanel translucent.

try this, maybe it will solve your problem:
In actionPeroformed..
public void actionPerformed(ActionEvent e) {
final JLabel tmpLabel = new JLabel(value[++i]); //change text
label.setFont(new Font("Times New Roman", 1, 36));
label.setForeground(new Color(255, 255, 255));
label.setBackground(new Color(0, 0, 0, .5f));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setOpaque(true);
label.setBounds(10, 10, 270, 70);
label = tmpLabel; //replace the entire label with a new label
}

Related

When hovering over element, a copy of the parent JFrame appears inside it [duplicate]

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?
I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)
Swing does not support transparent backgrounds.
Swing expects a component to be either:
opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.
The setOpaque(...) method is used to control the opaque property of a component.
In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.
This related example also makes the JPanel translucent.
try this, maybe it will solve your problem:
In actionPeroformed..
public void actionPerformed(ActionEvent e) {
final JLabel tmpLabel = new JLabel(value[++i]); //change text
label.setFont(new Font("Times New Roman", 1, 36));
label.setForeground(new Color(255, 255, 255));
label.setBackground(new Color(0, 0, 0, .5f));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setOpaque(true);
label.setBounds(10, 10, 270, 70);
label = tmpLabel; //replace the entire label with a new label
}

Jpanel is not repainting properly

I'm making a pacman game using java swing.
in my code i use 2 jpanels in the component panel
the first is for the map and the second is for the pacman.
now i am trying to move pacman to other cell when a button is clicked.it is moved but the old picture is not deleted.image before clicking, image after clicking
as you can see the new pacman appears but the old didn't disappear. and some trash also appeared.
this is the code of creating the jpanel for the pacman
JLabel pacman = new JLabel("", new ImageIcon("pacman.png"), JLabel.CENTER);
player = new JPanel(new BorderLayout());
player.setBounds(n*1, n*1, n, n);
//pacman.setOpaque(true);
pacman.setBackground(new Color(0, 0, 0, 0));
player.add(pacman);
//player.setOpaque(true);
player.setBackground(new Color(0, 0, 0, 0));
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1000, 1000);
panel.setLayout(null);
panel.add(player);
panel.setBackground(new Color(0, 0, 0,0));
contentPane.add(panel);
the code inside the button actionPreformed method is :
panel.remove(player);
player.setLocation(new Point(n*1, n*2));
panel.add(player);
panel.revalidate();
panel.repaint();
how can i make the old pacman disappears ?
player.setBackground(new Color(0, 0, 0, 0));
Don't use transparent colors. Swing does not handle transparency properly.
For full transparency there is a simple solution. Just make the component transparent:
player.setOpaque( false );
If you ever need partial transparency then check out Backgrounds With Transparency for a solution.

How to make JTextField semi-transparent

I am trying to create a JTextField with semi-transparent background (i.e. black background with alpha value of 120). My current code is:
public static void designTextField(final JTextField tf) {
tf.setBorder(null);
tf.setFont(new Font("Comfortaa", Font.PLAIN, 30));
tf.setBackground(new Color(0, 0, 0, 120));
tf.setForeground(new Color(200, 200, 200, 200));
}
However, this doesn't seem to be working. Here are the pictures this code results in (there are two text fields):
No text entered:
Text entered:
As you see there're several weird drawing bugs, and both text fields seem to be fully non-transparent. How can I fix this?
Swing perfectly works with transparency. You just need to add
tf.setOpaque(false);
https://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html
section "Problem: Visual artifacts appear in my GUI" explains.

How do you set the bounds of a JLabel in a JFrame?

I use a JLabel to view an image in a JFrame. I load it from a file with an ImageIcon.
JFrame frame = new JFrame(String);
frame.setLocationByPlatform(true);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel cpu = new JLabel(new ImageIcon(String));
cpu.setLocation(20, 20);
cpu.setSize(20, 460);
frame.add(cpu);
frame.setVisible(true);
I can't set location and size of the JLabel because it is done automatically.
I have to manually set these values because I want to truncate the image (vertical progress bar).
One way is to just paint the image:
final ImageIcon icon = new ImageIcon(path);
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
};
frame.add(panel);
The getWidth() and getHeight() in drawImage will force the image to stretch the size of the panel. Also the getPreferredSize() will give a size to the panel.
If want the panel to stay that size, then make sure it's parent container has a layout manager that will respect preferred sizes, like FlowLayout or GridBagLayout. If you want the panel to be stretched, the make sure it's parent container has a layout manager that disregards the preferred size, like BorderLayout or GridLayout
See Performing Custom Painting for more info on painting.
See Laying Out Components Within a Container to learn more about layout managers (which you should be using). Also see Why is it frowned upon to use a null layout in SWING? and What's wrong with the Null Layout in Java?
The size of your original is (58 x 510). If you want to display the image at a fixed size of 20 x 420, then you should scale your image to that size to you don't truncate any of the image. One way to do that is to use the Image.getScaledImage(...) method. Then you just add the scaled image to the label.
If you want to position your label (20, 20) from the top left of the panel, then you can add an EmptyBorder to the panel or the label.
Use the features of Swing.
Edit:
I want to truncate the image
Read your Image into a BufferedImage. Then you can use the getSubImage(...) method to get an image any size you want. Then you can use the sub image to create your ImageIcon and add it to a label.
The LayoutManager is auto-sizing your components, not allowing you to resize manually.
If you want to get away from this, you can turn off the LayoutManager.
frame.setLayout(null);
Please note that you should not use the null layout.

AWT TextArea inside JTabbedPane visual bug

JTabbedPane main_tabbedPane = new JTabbedPane( JTabbedPane.TOP );
main_tabbedPane.setBorder( new EmptyBorder( 0, 0, 0, 0 ) );
main_tabbedPane.setBounds( 10, 76, 665, 473 );
main_tabbedPane.setVisible(false);
main_content.add( main_tabbedPane ); // main_content is a jpanel
I then call a class constructor which extends JPanel
alphaStarter_tab = new AlphaStarterPnl();
which among other things has a TextArea (from Java AWT not JTextArea)
public class AlphaStarterPnl extends JPanel {
private TextArea outputTxtA;
public AlphaStarterPnl(){
outputTxtA = new TextArea("",4,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
outputTxtA.setFont(new Font("Tahoma", Font.PLAIN, 13));
outputTxtA.setEditable(false);
outputTxtA.setBackground(new Color(179,190,201));
outputTxtA.setForeground(new Color(34,64,132));
outputTxtA.setBounds(15, 133, 630, 300);
add(outputTxtA);
}
}
I then add this panel (which has alot more to it than the pasted code, but that doesn't matter here) to the tabbed pane
main_tabbedPane.addTab( "Copy Files", null, alphaStarter_tab, null );
When I do this, despite main_tabbedPane having been set to setvisible false, the TextArea pops up and not only that but it also appears in three places. (Perhaps appearing once at the 0,0 coordinates, then at the set x,0 coordinates then at the set x,y coordinates. When I continue on in the program and this "flash of ugly content" goes away when a second tab is added.
Any thoughts?
The problem here can be that you are mixing lightweight components (i.e. Swing components like JTabbedPane, those with 'J' prefix) and heavyweight components (i.e. AWT components such as TextArea). Using these two types of components together can cause rendering issues.

Categories

Resources