2

现在我正在使用以下代码使我的 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(边框和所有)透明。

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

在此处输入图像描述

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

4

1 回答 1

4

您可以通过更改 JInternalFrame 的绘制方法使用的 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 演示

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

于 2012-11-23T02:21:54.003 回答