Witam,
Mam małą zagwostkę której nie mogę sam rozwiązać.
Otóż chcę stworzyć Panel nad Panelem, który sam w sobie będzie przeźroczysty (powiedzmy w 50%) natomiast komponenty na nim umieszczone już nie.
Efekt chcę uzyskać mniej więcej taki:
tylko żeby przyciski na Panelu nie były przeźroczyste. Próbowałem to uzyskać za pomocą bibliotek SwingX a także próbując nadać przeźroczystość kolorom na Panelu.
W tym pierwszym przypadku wszystkie komponenty są również przeźroczyste, natomiast w drugim nie widać tła spod Panelu.
zamieszczam przykładowe kody może ktoś się spotkał z czymś takim i będzie w stanie mi pomóc
dzięki z góry.
Okno z wykorzystaniem SwingX
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package przezroczystosc;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import org.jdesktop.swingx.JXPanel;
/**
*
* @author w.maciejewski
*/
public class Okno extends JFrame{
public Okno(){
JPodPanel jpod;
jpod = new JPodPanel(new ImageIcon( "H:\\APPS\\Przezroczystosc\\tlo.png"));
getContentPane().setFocusable(true);
jpod.setVisible(true);
jpod.setFocusable(true);
JXPanel jxp=new JXPanel(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
GradientPaint gradient = new GradientPaint(0, this.getHeight(), Color.RED, 0,0 ,Color.GRAY);
g2.setPaint(gradient);
g2.fillRect(0, 0, this.getWidth(),this.getHeight());
}
};
jxp.setAlpha(0.3f);
jxp.setBackground(Color.red);
jxp.setSize(200,200);
jxp.setLayout(new GridLayout(3,3,10,10));
for(int i=0;i<9;i++){
JButton jbut=new JButton("test"+(i+1));
jxp.add(jbut);
jbut.setOpaque(true);
}
jpod.setLayout(new BorderLayout());
jpod.add(jxp,BorderLayout.CENTER);
setSize(400,400);
setVisible(true);
getContentPane().add(jpod);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class JPodPanel extends JPanel{
private Image imga;
JPodPanel(ImageIcon img){
imga=img.getImage();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// paint the background image and scale it to fill the entire space
g.drawImage(imga, 0, 0, getWidth(), getHeight(), this);
}
}
Próby ze zwykłymi JPanelami
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package przezroczystosc;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
/**
*
* @author w.maciejewski
*/
public class Okno2 extends JFrame{
public Okno2(){
JPodPanel2 jpod;
jpod = new JPodPanel2(new ImageIcon( "H:\\APPS\\Przezroczystosc\\tlo.png"));
jpod.setVisible(true);
jpod.setFocusable(true);
JNPanel jxp=new JNPanel();
jxp.setLayout(new GridLayout(3,3,10,10));
for(int i=0;i<9;i++){
JButton jbut=new JButton("test"+(i+1));
jxp.add(jbut);
jbut.setOpaque(true);
}
jpod.setLayout(new BorderLayout());
jpod.add(jxp,BorderLayout.CENTER);
setSize(400,400);
setVisible(true);
getContentPane().add(jpod);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class JPodPanel2 extends JPanel{
private Image imga;
JPodPanel2(ImageIcon img){
imga=img.getImage();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// paint the background image and scale it to fill the entire space
g.drawImage(imga, 0, 0, getWidth(), getHeight(), this);
}
}
class JNPanel extends JPanel{
private Color kolor1=new Color (0,195,0,150);
private Color kolor2=new Color (229,0,0,150);
JNPanel(){
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.SrcOver.derive(0.5f));
GradientPaint gradient = new GradientPaint(0, this.getHeight(), kolor1, 0,0 ,kolor2);
g2.setPaint(gradient);
g2.fillRect(0, 0, this.getWidth(),this.getHeight());
}
}