Reputation: 8268
What I want to make the content Pane transparent and that blue strip of normal dark bluecolour.But in the process of making contentPane transparent ,I am also making that strip as dull-coloured (coz that black colour is being painted over it) accidentally.
How can I correct it?
(Comment out the paint method and notice the change in strip.This is what I want as final result)
Here is the code:
class Home extends JFrame
{
int width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
int height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
public Home()
{
super("WiND");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setSize(width,height);
setBackground(new Color(0,0,0,0));
setUndecorated(true);
setVisible(true);
setLayout(new FlowLayout());
JPanel p=new JPanel();
p.setBackground(new Color(0x0D70E8));
p.setPreferredSize(new Dimension(width,height/10));
add(p);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
g2.setPaint(p);
g2.fillRect(0, 0, width,height);
}
}
(I did that the same thing a year back,but now after a year I forgot how I did that)
EDIT
I made the changes to the paint() method only according to @Sage.I get the following output Correct Blue Strip but now the grey translucent background has gone.
Upvotes: 0
Views: 91
Reputation: 15408
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
g2.setPaint(p);
g2.fillRect(0, 0, width,height);
}
When you are painting with paint()
function you are painting with graphics instance g
after your child component panel
is painted. Go to the source of the super.paint(g)
function and you will see that three subsequent function is being called:
protected void paintComponent(Graphics g)
: this one paint your component, e,g.:backgroundprotected void paintBorder(Graphics g)
: this one paints the border of the componentprotected void paintChildren(Graphics g)
: This one paints the children of the component in itSo after this super.paint(g)
call, anything you draw will appear above all the painting made with these three above function: hence above the children components, for your context the panel
with blue background.
Now, a solution would be:
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g.create();
// note, we are creating a graphics object here for safe painting
LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
g2.setPaint(p);
g2.fillRect(0, 0, width,height);
g2.dispose(); // disposing the object which we created
super.paint(g);
}
However rather than doing this, you use a class such as MyCanvas extends JComponent
and override it's paintComponent(Graphics)
function and draw inside of it. Then you can set an instance of MyCanvas
as the content pane of JFrame
using it's setContentPane(component)
function.
Check out the A Closer Look at the Paint Mechanism
Edit A small demo implementation of your use case:
class AMyContainer extends JComponent
{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g.create();
// note, we are creating a graphics object here for safe painting
LinearGradientPaint p=new LinearGradientPaint(0, 0, 0, getHeight(),new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
g2.setPaint(p);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose(); // disposing the object which we created
}
}
class Home extends JFrame
{
int width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
int height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
public Home()
{
super("WiND");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setSize(width,height);
setBackground(new Color(0,0,0,0));
setUndecorated(true);
JComponent container = new AMyContainer();
container.setLayout(new FlowLayout());
add(container);
JPanel p=new JPanel();
p.setBackground(new Color(0x0D70E8));
p.setPreferredSize(new Dimension(width,height/10));
container.add(p);
}
public static void main(String[] args)
{
new Home().setVisible(true);
}
}
Upvotes: 2