2011-12-03 49 views
3

JFrameに半透明の背景を持つJLabelがありますが、文字の周りにアーティファクトがあります。透明な背景のアーティファクト

Screenshot of the Artifacts

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.io.IOException; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Main { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     JLabel label = new JLabel("Hello World!"); 
     frame.setPreferredSize(new Dimension(200, 200)); 
     frame.setUndecorated(true); 
     frame.setBackground(new Color(128, 128, 128, 128)); 
     //label.setOpaque(false); 
     //label.setBackground(new Color(0, 0, 0, 0)); 
     //((JPanel) frame.getContentPane()).setOpaque(false); 
     //((JPanel) frame.getContentPane()).setBackground(new Color(0, 0, 0, 0)); 
     frame.add(label); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

私はすでに運なしに、これらのコンポーネントへの不透明度を適用してみました。私はすべてのコンポーネントを完全に不透明にしたいので、JFrameのためのjava7 per-pixel transparencyが唯一の解決策であるようです。

答えて

6

透過性を持つ色を背景として使用することはできません。説明と潜在的な解決策については、Background With Transparencyを参照してください。

+0

このポストは背景色が再適用され、ますます透明になりますが、文字周辺のアーティファクトは処理しません。 – NCode

+0

@NCode転記は、コンポーネントのバックグラウンドが完全に塗りつぶされていることを保証するために、不透明なプロパティを持つコンポーネントの責任を扱います。不透明なコンポーネント上に透明な色を使用すると、ペイントの問題が発生する可能性があります。転記は、起こりうることの「例」を与える。絵画の問題が何であるかは制限されません。 – camickr

2

私はあなたの問題を再現することができません、私はバッテリーが切れているかもしれませんが、あなたのGPUに問題はありませんか?

enter image description hereenter image description here

私が間違って何もチュートリアルHow to Create Translucent and Shaped Windows

からのコードに基づいて

enter image description here

enter image description here

が起こっていない、@camickrによって提案を試してみました

import java.awt.*; 
import javax.swing.*; 

public class TranslucentWindow extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public TranslucentWindow() { 
     super("Test translucent window"); 
     this.setLayout(new FlowLayout()); 
     this.add(new JButton("test")); 
     this.add(new JCheckBox("test")); 
     this.add(new JRadioButton("test")); 
     this.add(new JProgressBar(0, 100)); 
     JPanel panel = new JPanel() { 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 300); 
      } 
      private static final long serialVersionUID = 1L; 

      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.setColor(Color.red); 
       g.fillRect(0, 0, getWidth(), getHeight()); 
      } 
     }; 
     panel.add(new JLabel("Very long textxxxxxxxxxxxxxxxxxxxxx ")); 
     this.add(panel); 
     this.setSize(new Dimension(400, 300)); 
     this.setLocationRelativeTo(null); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       Window w = new TranslucentWindow(); 
       w.setVisible(true); 
       com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.7f); 
      } 
     }); 
    } 
} 
+0

2つの問題があります:1. Java 7で使用できなくなった古いAWTUtilitiesを使用しているため、Window.setOpacity(...)を使用する必要がありますが、両方の関数がグローバルな透過性を設定しています。透明になるためには、コンポーネントは完全に不透明でなければなりません。 – NCode