使整个jinternalframe透明

时间:2012-11-23 01:10:55

标签: java swing transparency jinternalframe

现在我正在使用此代码使我的jinternal框架透明:

double rgbConversionBackpack = Double.parseDouble(MyClient.configFile.getProperty("BACKPACK_FRAME_ALPHA"));
double tmp = (rgbConversionBackpack / 100.0) * 255.0;
this.getContentPane().setBackground(new Color(140, 0, 0, (int)tmp));
this.setOpaque(false);

我在滑块上设置代码以设置完全正常工作的alpha并将其保存到属性文件yada,yada,yada。问题是如何使整个JInternal Frame透明。

现在我只能设置内容窗格,并且jinternal框架中的任何其他面板(等)都是透明的,但我想让整个JinternalFrame(边框和所有)透明。

下面的屏幕截图显示背包上的红色是如何部分透明并且看起来不错,但仍然希望边框也是透明的。

enter image description here

有没有办法覆盖我的每个类的draw super方法,扩展JInternalFrame让它绘制半透明(显然取决于值)?

1 个答案:

答案 0 :(得分:4)

您可以通过更改JInternalFrame的paint方法使用的AlphaComposite来完成此操作。您必须小心,但要在透明组件的位置重新绘制包含顶级窗口,以免产生有趣的副作用。例如:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class TransparentInternalFrame extends JDesktopPane {
   private static final Color COLOR_1 = Color.red;
   private static final Color COLOR_2 = Color.blue;
   private static final float PT_2 = 30f;
   private static final int PREF_W = 800;
   private static final int PREF_H = 500;

   public TransparentInternalFrame() {
      add(new MyInternalFrame("Foo", 50, 50, 300, 300, 0.2f));
      add(new MyInternalFrame("Foo", 400, 100, 300, 300, 0.4f));
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(new GradientPaint(0, 0, COLOR_1, PT_2, PT_2, COLOR_2, true));
      g2.fillRect(0, 0, getWidth(), getHeight());
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      TransparentInternalFrame mainPanel = new TransparentInternalFrame();

      JFrame frame = new JFrame("TransparentInternalFrame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class MyInternalFrame extends JInternalFrame {
   private AlphaComposite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);

   public MyInternalFrame(String title, int x, int y, int w, int h, final float alpha) {
      super(title);
      setClosable(true);
      setBounds(x, y, w, h);
      setVisible(true);

      int sliderValue = (int) (alpha * 100);
      comp = comp.derive(alpha);
      final JSlider slider = new JSlider(0, 100, sliderValue);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);
      slider.setBorder(BorderFactory.createTitledBorder("Alpha Value"));
      slider.addChangeListener(new ChangeListener() {

         @Override
         public void stateChanged(ChangeEvent cEvt) {
            float alpha = (float) slider.getValue() / 100f;
            setAlpha(alpha);
            MyInternalFrame.this.repaint();

            Window win = SwingUtilities.getWindowAncestor(MyInternalFrame.this);
            win.repaint();
         }
      });

      add(new JLabel("My Label", SwingConstants.CENTER));
      add(slider, BorderLayout.SOUTH);
   }

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
      g2.setComposite(comp);
      super.paint(g);
   }

   public void setAlpha(float alpha) {
      comp = comp.derive(alpha);
   }
}

AlphaComposite Demo

但请注意,此程序尚未完全修复。如果您将一个JInternalFrame拖到另一个上,您仍会看到像素错误。我仍然需要解决这些问题......