2011-07-20 2 views
1

java:グラデーションスタイルの透明な背景をJFrameに使用します。 透明度は100%にする必要がありますが、下に行くと下に行くと20%になるはずです。java:透明なグラデーションの背景をJFrameに追加するには

私は既にそのような効果がある画像を使用できますが、私はテーマを提供したいと思いますユーザが自分の好きな画像を使用することを可能にするが、実行時に透明性を可能にする。

+0

で半透明のグラデーションをJFrameの拡張クラスで働いていた、あなたはGradientPaint' 'について話していますか? – Moonbeam

+0

グラデーションスタイルの透明度を使用したい – Vivek

+0

+1これは興味深い質問です。私は 'RescaleOp'が関与すると思いますが、全体の"グラデーションの透明度 "がループのために私を投げています!私もこれを好きになるので、私は仕事の後に戻ってくることができます。 :) – Moonbeam

答えて

5

日がある6u10でJavaに半透明の背景のサポートを追加しましたが、それは正式にAPIで説明されていない: - paintComponents(Graphics)メソッドをオーバーライドします。 Java 7では、機能は、WindowクラスのsetBackground()、setOpacity()、およびsetShape()メソッドを介してAPIに正式に追加されました。

挙動は、コード例では、グラデーション効果のためにそこにある底部に向かってオラクルhere

によって記載されています。

この手法は、基礎となるOSウィンドウマネージャがサポートしている場合にのみ機能します。 X11(Linux)では、合成ウィンドウマネージャを正しくインストールして設定する必要があります。これは、Java 7リリースノートのknown issuesと、bugに記載されています。

+0

それは働いた。ありがとう。 – Vivek

+0

つまり、LInux envではサポートされていません...? @Aaron – gumuruh

+0

@gumuruh私はLinuxのサポートに関するいくつかの詳細を更新しました。これは、構成によってサポートされる場合があります。 – Aaron

1

Mac OSXのみがJavaで透明/半透明のフレームを提供します。

Macを所有している場合は、あなたが透明に背景色を設定することから始めなければならない:

frame.setBackground(new Color(0, 0, 0, 0)); 

その後 - 私は(私はまだ、マックを所有していない:P)を考える

public void paintComponents(Graphics g) 
{ 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setPaint(new GradientPaint(
    new Point(0, 0), 
    new Color(0.0f, 0.0f, 0.0f, 0.0f), 
    new Point(0, getHeight()), 
    new Color(0.0f, 0.0f, 0.0f, 0.8f))); 
    super.paintComponents(g); 
} 
+0

'JFrame'は' setOpaque(...) 'メソッドを持っていません。 – Moonbeam

+0

月光ビーム:おっと:D –

1

私は実際のJFrame

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GraphicsConfiguration; 
import java.awt.Rectangle; 
import javax.swing.JFrame; 
import javax.swing.JLayeredPane; 
import javax.swing.JPanel; 
import javax.swing.JRootPane; 

public class JFrameGradient extends JFrame 
{ 
private static final long serialVersionUID = 7565267933439533086L; 
private Color initialColor; 
private Color finalColor; 
public static final int VERTICAL = 0; 
public static final int HORIZONTAL = 1; 
private int orientation = VERTICAL; 

private int MAX_WIDTH = 1; 
private int MAX_HEIGHT = 1; 

private int xInitial = 0; 
private int xFinal = 0; 
private int yInitial = 0; 
private int yFinal = 0; 

private int initialRedComponent; 
private int initialGreenComponent; 
private int initialBlueComponent; 

private int finalRedComponent; 
private int finalGreenComponent; 
private int finalBlueComponent; 

private int delta = 1; 
private String title; 
private GraphicsConfiguration gc; 
private JFrame frame; 
private Graphics g; 
private double transparency; 
private JPanel gradientPane = new JPanel(true); 
private boolean isColorChanged = false; 



public JFrameGradient(Color initialColor, Color finalColor , double transparency) 
{ 
    super(); 
    this.frame = this; 
    setTransparency(transparency); 
    initClass(); 
    initColor(initialColor, finalColor); 
} 

public JFrameGradient(String title , Color initialColor, Color finalColor , double transparency) 
{ 
    super(title); 
    this.title = title; 
    setTransparency(transparency); 
    initClass(); 
    initColor(initialColor, finalColor); 
} 

public JFrameGradient(GraphicsConfiguration gc , Color initialColor, Color finalColor , double transparency) 
{ 
    super(gc); 
    this.setGc(gc); 
    setTransparency(transparency); 
    initClass(); 
    initColor(initialColor, finalColor); 
} 

public JFrameGradient(GraphicsConfiguration gc , String title , Color initialColor, Color finalColor , double transparency) 
{ 
    super(title , gc); 
    this.title = title; 
    this.setGc(gc); 
    setTransparency(transparency); 
    initClass(); 
    initColor(initialColor, finalColor); 
} 

public JFrameGradient( double transparency) 
{ 
    this(Color.black , Color.white , transparency); 
} 

public JFrameGradient(String title , double transparency) 
{ 
    this(title , Color.black , Color.white , transparency); 
} 

public JFrameGradient(GraphicsConfiguration gc , double transparency) 
{ 
    this(gc , Color.black , Color.white , transparency); 
} 

public JFrameGradient(GraphicsConfiguration gc , String title , double transparency) 
{ 
    this(gc , title , Color.black , Color.white , transparency); 
} 

public JFrameGradient() 
{ 
    this(Color.black , Color.white , 1); 
} 

public JFrameGradient(String title) 
{ 
    this(title , Color.black , Color.white , 1); 
} 

public JFrameGradient(GraphicsConfiguration gc) 
{ 
    this(gc , Color.black , Color.white , 1); 
} 

public JFrameGradient(GraphicsConfiguration gc , String title) 
{ 
    this(gc , title , Color.black , Color.white , 1); 
} 

public Color getInitialColor() 
{ 
    return initialColor; 
} 

public Color getFinalColor() 
{ 
    return finalColor; 
} 

public void setInitialColor(Color initialColor) 
{ 
    this.initialColor = initialColor; 
    isColorChanged = true; 
    if(isVisible()) 
    { 
     repaint(); 
    } 
} 

public void setFinalColor(Color finalColor) 
{ 
    this.finalColor = finalColor; 
    isColorChanged = true; 
    if(isVisible()) 
    { 
     repaint(); 
    } 
} 

public String getTitle() 
{ 
    return title; 
} 

public double getTransparency() 
{ 
    return transparency; 
} 

public void setTransparency(double transparency) 
{ 
    if((transparency >= 0.0) && (transparency <= 1.0)) 
    { 
     this.transparency = transparency; 
    } 
    else 
    { 
     this.transparency = 1.0; 
    } 
} 

/** 
* @return the gc 
*/ 
public GraphicsConfiguration getGc() 
{ 
    return gc; 
} 

/** 
* @param gc the gc to set 
*/ 
private void setGc(GraphicsConfiguration gc) 
{ 
    this.gc = gc; 
} 

public int getOrientation() 
{ 
    return orientation; 
} 

public void setOrientation(int orientation) 
{ 
    this.orientation = orientation; 
    if(isVisible()) 
    { 
     repaint(); 
    } 

} 


private void initColor(Color initialColor, Color finalColor) 
{ 
    this.initialColor = initialColor; 
    this.finalColor = finalColor; 

    this.initialRedComponent = initialColor.getRed(); 
    this.initialGreenComponent = initialColor.getGreen(); 
    this.initialBlueComponent = initialColor.getBlue(); 

    this.finalRedComponent = finalColor.getRed(); 
    this.finalGreenComponent = finalColor.getGreen(); 
    this.finalBlueComponent = finalColor.getBlue(); 

} 

private void initClass() 
{ 
    if(this != null) 
    { 
     frame = this; 
     frame.add(gradientPane); 
     frame.pack(); 
    } 
} 

@Override 
public void paint(Graphics g) 
{ 
    super.paint(g); 
    paintGradient(g); 
    Rectangle mask = null; 

    for(Component c : getComponents()) 
    { 
     if(c instanceof JRootPane) 
     { 
      JRootPane rootPane = (JRootPane) c; 
      rootPane.setDoubleBuffered(true); 
      for(Component cRootPane : rootPane.getComponents()) 
      { 
       if(cRootPane instanceof JLayeredPane) 
       { 
        JLayeredPane cLayerPanels = (JLayeredPane) cRootPane; 
        cLayerPanels.setDoubleBuffered(true); 
        for(Component cLayerPanel : cLayerPanels.getComponents()) 
        { 
         if(cLayerPanel instanceof JPanel) 
         { 
          JPanel cPanels = (JPanel) cLayerPanel; 
          cPanels.setDoubleBuffered(true); 

          for(Component cPanel : cPanels.getComponents()) 
          { 
           if(cPanel instanceof JPanel) 
           { 
            JPanel cPanels2 = (JPanel) cPanel; 
            cPanels2.setDoubleBuffered(true); 
            mask = frame.getBounds(); 
            for(Component cPanel2 : cPanels2.getComponents()) 
            { 
             mask.union(cPanel2.getBounds()); 
             cPanel2.paint(cPanel2.getGraphics()); 
            } 

           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
    g.setClip(mask); 
} 


private void paintGradient(Graphics g) 
{ 
    if(isColorChanged == true) 
    { 
     initColor(initialColor, finalColor); 
     isColorChanged = false; 
    } 

    if (orientation == VERTICAL) 
    { 
     if (this.getWidth() != 0) 
     { 
      MAX_WIDTH = this.getWidth(); 
     } 

     if (this.getHeight() != 0) 
     { 
      MAX_HEIGHT = this.getHeight(); 
      xFinal = MAX_WIDTH; 
      delta = MAX_HEIGHT; 
     } 

    } else if(orientation == HORIZONTAL) 
    { 
     if (this.getHeight() != 0) 
     { 
      MAX_HEIGHT = this.getHeight(); 
     } 

     if (this.getWidth() != 0) 
     { 
      MAX_WIDTH = this.getWidth(); 
      yFinal = MAX_HEIGHT; 
      delta = MAX_WIDTH; 
     } 

    } 

    if ((this.initialColor != null) && (this.finalColor != null)) 
    { 

     if (delta == 0) 
     { 
      delta = 1; 
     } 

     if(orientation == VERTICAL) 
     { 
      for(int n = 0 ; n < MAX_HEIGHT ; n++) 
      { 
       calculateColor(g, n); 
       g.drawLine(xInitial, n, xFinal, n); 
      } 
     } else if (orientation == HORIZONTAL) 
     { 
      for(int n = 0 ; n < MAX_WIDTH ; n++) 
      { 
       calculateColor(g, n); 
       g.drawLine(n , yInitial, n , yFinal); 
       delta = Math.max(MAX_HEIGHT , MAX_WIDTH); 
       } 
      } 



    } 

    } 

private void calculateColor(Graphics g, int n) 
{ 
    int redComponent = 0; 
    int greenComponent = 0; 
    int blueComponent = 0; 

    redComponent = interpolateComponent(n, initialRedComponent, finalRedComponent, delta); 
    greenComponent = interpolateComponent(n, initialGreenComponent, finalGreenComponent, delta); 
    blueComponent = interpolateComponent(n, initialBlueComponent, finalBlueComponent, delta); 

    if(redComponent > 255) 
    { 
     redComponent = 255; 
    } else if (redComponent < 0) 
    { 
     redComponent = 0; 
    } 

    if(greenComponent > 255) 
    { 
     greenComponent = 255; 
    } else if (greenComponent < 0) 
    { 
     greenComponent = 0; 
    } 

    if(redComponent > 255) 
    { 
     blueComponent = 255; 
    } else if (redComponent < 0) 
    { 
     blueComponent = 0; 
    } 

    g.setColor(new Color(redComponent , greenComponent , blueComponent , (int) (255 * transparency))); 
} 

private int interpolateComponent(int x , int xInit , int xFinal , int delta) 
{ 
    int returnValue = 0 ; 

    if (xInit < xFinal) { 
     double slope = (xFinal - xInit)/(double) delta ; 

     returnValue = (int) (xInit + slope * x); 
    } 
    else if(xFinal < xInit) { 
     double slope = (xInit - xFinal)/(double) delta ; 

     returnValue = (int) (xInit - slope * x); 
    } else { 
     returnValue = xInit; 
    } 


    return returnValue; 

} 

@Override 
public void setVisible(boolean aFlag) 
{ 
    if (aFlag == true) 
    { 
     g = gradientPane.getGraphics(); 
     paint(g); 
     frame.pack(); 
    } 

     super.setVisible(aFlag); 

} 
public void setPreferredSize(int width, int height) 
{ 
    setPreferredSize(new Dimension(width, height)); 
} 

}

+0

ありがとうございます。これは実際にうまく動作し、JFrameコンポーネントにグラデーションを適用できます。しかし、フレームに塗りつぶされたすべてのコンポーネントは、フレームが読み込まれると非表示/非表示になり、表示させるためにそれらの上にマウスを置く必要があります。なぜこれが事実かもしれないのか、あなたはおそらく理解できますか?私は、コンポーネントを追加した後で検証して再ペイントしましたが、あまり役に立ちませんでした。 – Rohan