상세 컨텐츠

본문 제목

[Java] 투명한 윈도우, 모양이 있는 윈도우 만들기 관련

JAVA/JAVA UI

by AlrepondTech 2018. 8. 20. 11:37

본문

반응형

 

 

 

 

=================================

=================================

=================================

 

 

 

출처: http://techalpine.com/how-to-create-translucent-and-shaped-windows-in-java/

 

How to create Translucent and Shaped Windows in Java?

Overview: In this article we will discuss about the transparency and different shaped windows in Java. In java 7 swing supports this feature and making swing UI components more flexible and user friendly.

Introduction: In some application, transparent window is a requirement to support its functionality. Now in Java, transparency can be implemented. A translucent window is created by altering its opacity by implementing a method called setOpacity on a JFrame component. But we must understand that a translucent window is only possible if the underlying operating system supports it. And we also need to make sure that the window is not decorated. To make a window undecorated, you need to call setUndecorated (true) method. Some time it is also required to change the shape of a window UI. To implement it, we need to call setShape method within the componentResized method. It will recalculate the shape when the window is resized.

Back ground: In java UI, support for translucency and shaped window was a long time demand for Swing/AWT components. For native development access to these properties are available from long time back. But it was not accessible to core java components. In Java6 onward, support for translucency and shaped window is available. Even per pixel level translucency is also supports in Java7.

Type of support for translucency and transparency: Java 7 supports the following three type of support.

TRANSLUCENT: In this feature the window can have different colors but with the same level of opacity. So the alpha value is same for all the pixels. This effect is suitable for fade off a window and then gradually decreases and increases the alpha value.

PERPIXEL_TRANSLUCENT: This property supports different alpha values within the window itself. This is used to fade selected region of the window.

PERPIXEL_TRANSPARENT: This property supports different shapes of windows rather than traditional rectangle. If per pixel transparency is supported then different shapes like circle, triangle can be created.

Before we use any of the above properties we need to test the support of under lying operating system. The test can be done by using method isWindowTranslucencySupported belonging to the java.awt.GraphicsDevice class. It takes the type of transparency as input and returns true/false to indicate the support.

Let us check a sample code to implement the translucency feature. In this example we have created a rectangular translucent window on a text area. First we have disabled the decoration mode to get the effect of transparency. Then we have checked if the underlying operating system supports it or not. If supported the effect is visible on the frame window.

Listing1: Sample showing Translucency of a JFrame window

import java.awt.Color;

import java.awt.GraphicsDevice;

import java.awt.GraphicsEnvironment;

import java.awt.GridBagLayout;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.SwingUtilities;

public class TransRecFrame extends JFrame {

/**

* Create a transparent rectangular frame with 85% transparency

*/

public TransRecFrame() {

super(“Translucent Rectangular Frame”);

//Set layout

setLayout(new GridBagLayout());

//Create a text area

final JTextArea txtArea = new JTextArea(5, 50);

txtArea.setBackground(Color.CYAN);

add(txtArea);

//Call to disable decoration

setUndecorated(true);

 

//Call setShape to resize the shape when widnow is resized

addComponentListener(new ComponentAdapter() {

@Override

public void componentResized(ComponentEvent e) {

setShape(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));

}

});

//Make the window 85% transparent

setOpacity(0.85f);

//Set other parameters

setLocationRelativeTo(null);

setSize(200, 300);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) {

//Create graphics environment

GraphicsEnvironment genv = GraphicsEnvironment

.getLocalGraphicsEnvironment();

//Check if OS supports Translucency

if (genv.getDefaultScreenDevice().isWindowTranslucencySupported(

GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {

System.out.println(“OS supports translucency”);

new TransRecFrame();

}

}

}

Features to support translucency and shape: In Java7 there are mainly three features available to support translucency and shape of a window.

  • Full window translucency: In this approach full window is translucent.
  • Per pixel translucency: In this approach a part of the window is translucent
  • Shaped windows: Make different shaped windows like ovale, circle, rectangular etc.

Per pixel translucency:

We have already seen how to make a complete window translucent in the previous example. Now we will discuss the second part of making a set of pixels translucent by using their background color. There are some limitations to implement this scenario. The window should not be full screen and the system must support pixel level translucency. The rest of the procedure is similar to the above example.

In the following example we will see how pixel level translucency is set in a frame.

Listing2: Sample showing pixel level translucency support.

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class PixelTranslucency extends JFrame {

public PixelTranslucency() {

super(“Set pixel level translucency”);

//Set size of the JFrame

setSize(250, 250);

//Set lay out

getContentPane().setLayout(new GridLayout(6, 6));

//Call for pixel setting

for(int i = 0; i &lt16; i++){

add(new PixelPanel(255 – i * 12));

}

//Set background and other properties

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

setLocationRelativeTo(null);

setVisible(true);

}

public static void main(String[] args) {

GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();

//Check if window supports translucency

if(genv.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {

System.out.println(“OS supports translucency”);

new PixelTranslucency();

}

}     private class PixelPanel extends JPanel {

private PixelPanel(int pixl) {

super();

setBackground(new Color(0, 0, 255, pixl));

}

}

}

Shaped Windows:

Now we will discuss about another important feature supported by Java7.The shaped window supports all types of shapes whatever be the requirement of the user. This feature helps you to create any shape like circle, triangle, polygon or any possible complex shape. The setShape method of the window class is available for setting the property. But again we have to remember that full screen mode should not be allowed and the operating system supports translucency.

The following example shows the usage of shaped window.

Listing3: The sample code showing the usage of shaped window.

import javax.swing.*;

import java.awt.*;

import java.awt.geom.Ellipse2D;

import java.awt.geom.GeneralPath;

public class JavaShapedWindow extends JFrame {

public JavaShapedWindow() {

super(“Set shaped Window”);

//Set undecorated OFF to get an effect

setUndecorated(true);

//Set size

setSize(new Dimension(250, 250));

//Set polygon properties

Polygon polygon = new Polygon();

polygon.addPoint(0, 100);

polygon.addPoint(50, 0);

polygon.addPoint(100, 100);

//Set the values of the shape

Ellipse2D.Double newCircle = new Ellipse2D.Double(0, 50, 1.0*100, 1.0*100);

//Set general path properties

GeneralPath gpath = new GeneralPath();

gpath.append(polygon, true);

gpath.append(newCircle, true);

//Set the path

setShape(gpath);

//Set box layout

getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

add(Box.createHorizontalGlue());

//Create label and set properties

JLabel newlabel = new JLabel(“Java Shaped window”);

newlabel.setForeground(Color.white);

add(newlabel);

add(Box.createHorizontalGlue());

//Set content pane background color

getContentPane().setBackground(Color.cyan);

//Set location

setLocationRelativeTo(null);

setVisible(true);

}

public static void main(String[] args) {

GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();

//Check OS support for this property

if(genv.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {

System.out.println(“OS supports translucency”);

new JavaShapedWindow();

}

}

}

We can also implement a combination of two features like translucency and shaped window. To implement this, call setOpacity method to your frame. The result window will display the combined effect. But we should remember the underlying operating system must support pixel level translucency and pixel level transparency.

Conclusion:

 

 

 

=================================

=================================

=================================

 

 

 

출처: https://m.blog.naver.com/PostView.nhn?blogId=sarah7_2000&logNo=220611615388&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F

 


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame implements MouseMotionListener, MouseListener
{
    MyPanel panel;
    Point ptFirst;

    JButton btn;
        
    public Test()
    {   
        setUndecorated(true); //이걸 해줘야 타이틀 바가 사라진다.
        setBackground(new Color(0, 0, 0, 0));//

        panel = new MyPanel("bogo.png");
        setContentPane(panel);
        //getContentPane().setBackground(Color.BLACK);
        
        //버튼 넣기
        btn = new JButton("button");
        btn.setBounds(200, 200, 100, 50);
        
        panel.setLayout(null);
        panel.add(btn);
        
        setSize(512, 512);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addMouseMotionListener(this);
        addMouseListener(this);
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        Point loc = e.getLocationOnScreen();
        loc.x -= ptFirst.x;
        loc.y -= ptFirst.y;
        
        setLocation(loc);//프레임창 움직이기
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {

    }

    @Override
    public void mouseEntered(MouseEvent e)
    {

    }

    @Override
    public void mouseExited(MouseEvent e)
    {

    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        ptFirst = e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {

    }

    public static void main(String args[])
    {
        new Test();
    }
}

class MyPanel extends JPanel
{
    Image image;

    MyPanel(String img)
    {
        image = Toolkit.getDefaultToolkit().createImage(img);

        setOpaque(false);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (image != null)
        {
            g.drawImage(image, 0, 0, this);
        }
        
        Graphics2D g2d = (Graphics2D) g.create();
        // 50% transparent Alpha
        //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        // 100% transparent Alpha
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f));
        
        g2d.setColor(getBackground());
        g2d.fill(getBounds());
        g2d.dispose();
    }
}

 

 

 

=================================

=================================

=================================

 

 

 

 

출처: http://www.jaso.co.kr/index.php/2005/11/untitled24/

 

 

Swing을 이용하여 투명한 윈도우나 사각형이 아닌 여러가지 모양의 윈도우를 만들수 있는 방법이 ONJava.com에 올라와 있네요…
그래서 저도 한번 해봤습니다.

 

public class TransparentBackground extends JComponent implements

ComponentListener, WindowFocusListener, Runnable

{

private JFrame frame;

 

 

private Image background;

 

private long lastupdate = 0;

 

public boolean refreshRequested = true;

 

public TransparentBackground(JFrame frame)

{

this.frame = frame;

updateBackground();

frame.addComponentListener(this);

frame.addWindowFocusListener(this);

new Thread(this).start();

}

 

public void updateBackground()

{

try

{

Robot rbt = new Robot();

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension dim = tk.getScreenSize();

background = rbt.createScreenCapture(new Rectangle(0, 0, (int) dim

.getWidth(), (int) dim.getHeight()));

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

 

 

 

public void paintComponent(Graphics g)

{

Point pos = this.getLocationOnScreen();

Point offset = new Point(-pos.x, -pos.y);

g.drawImage(background, offset.x, offset.y, null);

}

 

public void componentShown(ComponentEvent evt)

{

repaint();

}

 

public void componentResized(ComponentEvent evt)

{

repaint();

}

 

public void componentMoved(ComponentEvent evt)

{

repaint();

}

 

public void componentHidden(ComponentEvent evt)

{

}

 

public void windowGainedFocus(WindowEvent evt)

{

refresh();

}

 

public void windowLostFocus(WindowEvent evt)

{

refresh();

}

 

public void refresh()

{

if (frame.isVisible())

{

repaint();

refreshRequested = true;

lastupdate = new Date().getTime();

}

}

 

public void run()

{

try

{

while (true)

{

Thread.sleep(250);

long now = new Date().getTime();

if (refreshRequested && ((now – lastupdate) > 1000))

{

if (frame.isVisible())

{

Point location = frame.getLocation();

frame.setVisible(false);

updateBackground();

frame.setVisible(true);

frame.setLocation(location);

refresh();

}

lastupdate = now;

refreshRequested = false;

}

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

 

 

원리는 updateBackground() 가 호출될 때 현재 영역의 Screen을 캡쳐한 다음 paintComponent() 메소드가 호출될 때 캡쳐한 이미지를 보여 주는 것입니다.

 

 

public class TransparentWindowExample
{
public static void main(String[] args)
{
JFrame frame = new JFrame(“배경이 투명한 윈도우”);
TransparentBackground bg = new TransparentBackground(frame);
bg.setLayout(new BorderLayout());
JButton button = new JButton(“버튼 Component”);
bg.add(“North”, button);
JLabel label = new JLabel(“이것은 새로 생성한 윈도우에서 만든겁니다”);
bg.add(“South”, label);
frame.getContentPane().add(“Center”, bg);
frame.pack();
frame.setSize(150, 100);
frame.setVisible(true);
}

}

 

 

문제는 투명도 % 조정이 안된다는 것입니다. ^^

다음은 똑같은 내용으로 이미지를 이용하여 윈도우를 만든 것입니다.
이미지 만들때 배경이 투명하게 만들어야 합니다.

public class TransparentWindowExample
{
public static void main(String[] args)
{
JFrame frame = new JFrame(“Transparent Window”);
frame.setUndecorated(true);

 

TransparentBackground bg = new TransparentBackground(frame);
bg.setLayout(new BorderLayout());

JPanel panel = new JPanel() {
public void paintComponent(Graphics g)
{
g.setColor(Color.blue);
Image img = new
ImageIcon(“clock.png”).getImage();
g.drawImage(img, 0, 0, null);
}
};
panel.setOpaque(false);

bg.add(“Center”, panel);

frame.getContentPane().add(“Center”, bg);
frame.pack();
frame.setSize(200, 200);
frame.setLocation(200, 200);
frame.setVisible(true);
}

}

 

– 원본이미지

 

 

– 실행결과

 

 

 

 

 

 

 

 

=================================

=================================

=================================

 

 

 

출처: http://egloos.zum.com/tobby48/v/2923010

 

[Java] 프레임 이동, 투명, 모양 정의한 예제소스

 

 

급하게 해서 주석이 없는 .....

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.RoundRectangle2D;

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

public class MovePanel extends JFrame implements ComponentListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public JPanel jPanel1 = new JPanel(){
        /* (non-Javadoc)
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
         */
        protected void paintComponent(Graphics g) {
            if (g instanceof Graphics2D) {
                Paint p = new GradientPaint(getWidth(), getHeight(), new Color(221, 221, 221, 50), 0.0f, 0.0f,
                        new Color(221, 221, 221, 150), true);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setPaint(p);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            } else {
                super.paintComponent(g);
            }
        }
    };

    /* move by mouse pointer */
    Point mainFrameLocation = new Point(0, 0);
    Point mouseClickedLocation = new Point(0, 0);

    public MovePanel() {

//        jPanel1.setOpaque(true);
        addMouseListener(new FrameMove_mouseAdapter(this));
        addMouseMotionListener(new FrameMove_mouseMotionAdapter(this));

        /* root frame */
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(200, 200));
        setUndecorated(true);

        getContentPane().add(jPanel1, BorderLayout.CENTER);
        setLocationRelativeTo(null);
        
        addComponentListener(this);
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MovePanel mainFrame = new MovePanel();
                mainFrame.setVisible(true);
                com.sun.awt.AWTUtilities.setWindowOpacity(mainFrame, 80.0f / 100.0f);
            }
        });
    }

    public void componentHidden(ComponentEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    public void componentMoved(ComponentEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    public void componentResized(ComponentEvent arg0) {
        // TODO Auto-generated method stub
        Shape shape = new RoundRectangle2D.Float(0, 0, getWidth(),
                getHeight(), 20, 20);
        com.sun.awt.AWTUtilities.setWindowShape(this, shape);
    }

    public void componentShown(ComponentEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}

/**
 * @author tobby48 2009. 07. 22
 */
class FrameMove_mouseAdapter extends MouseAdapter {
    private MovePanel frame;

    FrameMove_mouseAdapter(MovePanel mainframe) {
        this.frame = mainframe;
    }

    public void mousePressed(MouseEvent e) {
        frame.mouseClickedLocation.x = e.getX();
        frame.mouseClickedLocation.y = e.getY();
    }

    public void mouseReleased(MouseEvent e) {
    }
}

/**
 * @author tobby48 2009. 07. 22
 */
class FrameMove_mouseMotionAdapter extends MouseMotionAdapter {
    private MovePanel frame;
    
    FrameMove_mouseMotionAdapter(MovePanel mainframe) {
        this.frame = mainframe;
    }

    public void mouseMove(MouseEvent e) {}

    public void mouseDragged(MouseEvent e) {
        frame.setLocation(e.getLocationOnScreen().x - frame.mouseClickedLocation.x,
                e.getLocationOnScreen().y - frame.mouseClickedLocation.y);
    }
}

 

 

 

 

 

 

출처: http://levin01.tistory.com/238

 

[펌] 자바 스윙으로 반투명창을 만들수 있을까?

 

질문:자바 스윙으로 반투명창을 만들수 있을까? being20c/ 2004-02-18 15:59
음.. 예전에 자바로 작성된 프로그램중에 반투명으로 표시되는..
그러니까 창이 뜨면 뒤에 배경까지 보이는 반투명으로된 프로그램을 본듯 한데..
어떻게 하는 것인가요?
JFrame이나 Frame의 LayerPane에 반투명을 설정하는 메소드가 있는 건가요?
Document 뒤져봐도 찾을수 가 없군요..

혹시 아시는분 답변좀..
답변:re: 자바 스윙으로 반투명창을 만들수 있을까? imsangchin/ 2004-02-17 22:04
 다음은, swing을 이용한, 투명 윈도우 만드는 소스 입니다.참고로, java에서는 JFrame, JWindow같은, 중량 컴포넌트에 대해서 투명도를 설정할수 있는 메소드를 제공하지는 않습니다.위소스는 편법으로, Robet클래스를 이용해서, 배경화면을 캡쳐해서, 현재 윈도우가 투명하게끔 보이게 만든 것입니다. 흠이라면, 약간 속도 처리가 느리다는거죠.일반적으로 반투명윈도우 는 주로 VC++로 만든 프로그램입니다. VC++에서는 윈도우를 반투명으로 만드는 API를 제공하거든요. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TransparentWnd extends JWindow implements FocusListener, MouseMotionListener,
    MouseListener
{
    Image img, tim;
    Graphics tig;
    Point mp;
    Robot r;
    public TransparentWnd()
    {
        setBounds(170, 170, 100, 100);
        try
        {
            r = new Robot();
        }
        catch (AWTException awe)
        {
            System.out.println("robot excepton occurred");
        }
        capture();
        addMouseMotionListener(this);
        addMouseListener(this);
        addFocusListener(this);
        setVisible(true);
    }
    public void mouseDragged(MouseEvent m)
    {
        if (mp == null)
        {
            return;
        }
        Point p = m.getPoint();
        int x = getX() + p.x - mp.x;
        int y = getY() + p.y - mp.y;
        setLocation(x, y);
        paintP(getGraphics());
    }
    public void mouseMoved(MouseEvent m)
    {}
    public void mouseClicked(MouseEvent m)
    {}
    public void mouseEntered(MouseEvent m)
    {}
    public void mouseExited(MouseEvent m)
    {}
    public void mouseReleased(MouseEvent m)
    {
        mp = null;
    }
    public void mousePressed(MouseEvent m)
    {
        mp = m.getPoint();
    }
    public void focusGained(FocusEvent fe)
    {
        setSize(0, 0);
        capture();
        setSize(100, 100);
    }
    public void focusLost
        (FocusEvent fe)
    {}
    public void capture()
    {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        img = r.createScreenCapture(new Rectangle(0, 0, d.width, d.height));
    }
    public void captureX()
    {
        Rectangle rect = getBounds();
        setVisible(false);
        Thread.yield();
        Image xmg = r.createScreenCapture(rect);
        img.getGraphics().drawImage(xmg, rect.x, rect.y, rect.width,
                                    rect.height, null);
        setVisible(true);
    }
    public void paint(Graphics g)
    {
        Rectangle rect = g.getClipBounds();
        if (tim == null)
        {
            tim = createImage(getWidth(), getHeight());
            tig = tim.getGraphics();
        }
        if (!rect.getSize().equals(getSize()))
        {
            captureX();
        }
        else
        {
            paintP(g);
        }
    }
    public void paintP(Graphics g)
    {
        tig.drawImage(img, 0, 0, getWidth(), getHeight(), getX(), getY(),
                      getX() + getWidth(), getY() + getHeight(), null);
        tig.setColor(Color.orange);
        tig.fillOval(10, 20, 70, 80);
        tig.setColor(Color.green);
        tig.fillOval(21, 16, 20, 10);
        tig.fillOval(40, 02, 11, 21);
        g.drawImage(tim, 0, 0, null);
    }
    public void update(Graphics g)
    {
        this.paint(g);
    }
    public
        static void main(String[] args)
    {
        new TransparentWnd();
    }
}

 

 

=================================

=================================

=================================

 

 

 

출처: https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

 

 


Determining a Platform's Capabilities

Not all platforms support all of these capabilities. An UnsupportedOperationException exception is thrown when code attempts to invoke the setShape or setOpacity methods on a platform that does not support these capabilities. Therefore, it is best practice to first check that the platform supports the capability that you want to implement. The GraphicsDevice class provides theisWindowTranslucencySupported(GraphicsDevice.WindowTranslucency) method that you can use for this purpose. You pass one of three enum values, defined in GraphicsDevice.WindowTranslucency, to this method:

  • TRANSLUCENT – The underlying platform supports windows with uniform translucency, where each pixel has the same alpha value.
  • PERPIXEL_TRANSLUCENT – The underlying platform supports windows with per-pixel translucency. This capability is required to implement windows that fade away.
  • PERPIXEL_TRANSPARENT – The underlying platform supports shaped windows.

The GraphicsConfiguration class also provides the isTranslucencyCapable method to determine if PERPIXEL_TRANSLUCENT translucency is supported by the given GraphicsConfiguration object.


Version note: The translucent and shaped window API was first added to the Java SE 6 Update 10 release as a private API. This functionality was moved to the public AWT package in the JDK 7 release. This tutorial describes the API that is available in the JDK 7 release. See Java SE 6 Update 10 API for a mapping of the private API in the Java SE 6 Update 10 release to the public API in the JDK 7 release.

The following code shows how to check for all three capabilities:

import static java.awt.GraphicsDevice.WindowTranslucency.*;  // Determine what the default GraphicsDevice can support. GraphicsEnvironment ge =     GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice();  boolean isUniformTranslucencySupported =     gd.isWindowTranslucencySupported(TRANSLUCENT); boolean isPerPixelTranslucencySupported =     gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); boolean isShapedWindowSupported =     gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT); 

Note: None of these capabilities work on windows in full-screen mode. Invoking any of the relevant methods while in full-screen mode causes an IllegalComponentStateException exception to be thrown.

How to Implement Uniform Translucency

You can create a window where each pixel has the same translucency by invoking the setOpacity(float) method in the Window class. The float argument passed to this method represents the translucency of the window and should be a value between 0 and 1, inclusive. The smaller the number, the more transparent the window. There is also a corresponding getOpacity method.

The TranslucentWindowDemo.java example creates a window that is 55 percent opaque (45 percent translucent). If the underlying platform does not support translucent windows, the example exits. The code relating to opacity is shown in bold.

import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*;  public class TranslucentWindowDemo extends JFrame {     public TranslucentWindowDemo() {         super("TranslucentWindow");         setLayout(new GridBagLayout());          setSize(300,200);         setLocationRelativeTo(null);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          //Add a sample button.         add(new JButton("I am a Button"));     }      public static void main(String[] args) {         // Determine if the GraphicsDevice supports translucency.         GraphicsEnvironment ge =              GraphicsEnvironment.getLocalGraphicsEnvironment();         GraphicsDevice gd = ge.getDefaultScreenDevice();          //If translucent windows aren't supported, exit.         if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {             System.err.println(                 "Translucency is not supported");                 System.exit(0);         }                  JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread         SwingUtilities.invokeLater(new Runnable() {             @Override             public void run() {                 TranslucentWindowDemo tw = new TranslucentWindowDemo();                  // Set the window to 55% opaque (45% translucent).                 tw.setOpacity(0.55f);                  // Display the window.                 tw.setVisible(true);             }         });     } } 

Note that the button is also affected by the uniform translucency. Setting the opacity affects the whole window, including any components that the window contains.

How to Implement Per-Pixel Translucency

Creating a window that uses per-pixel translucency involves defining alpha values over the rectangular area that the window occupies. When a pixel's alpha value is zero, that pixel is fully transparent. When a pixel's alpha value is 255, that pixel is fully opaque. When a pixel's alpha value is 128, that pixel is 50 percent translucent, and so on. An easy way to create a smooth interpolation between alpha values is to use the GradientPaint class. The included example uses this approach.

Invoking setBackground(new Color(0,0,0,0)) on the window causes the software to use the alpha values to render per-pixel translucency. In fact, invoking setBackground(new Color(0,0,0,alpha), where alpha is less than 255, installs per-pixel transparency. So, if you invoke setBackground(new Color(0,0,0,128)) and do nothing else, the window is rendered with 50 percent translucency for each background pixel. However, if you are creating your own range of alpha values, you most likely will want an alpha value of 0.

While not prohibited by the public API, you will generally want to enable per-pixel translucency on undecorated windows. In most cases, using per-pixel translucency on decorated windows does not make sense. Doing so can disable the decorations, or cause other platform-dependent side effects.

To determine if a window is using per-pixel translucency, you can use the isOpaque method.

An example follows. First, here are the steps required to implement the example:

  1. Invoke setBackground(new Color(0,0,0,0)) on the window.
  2. Create a JPanel instance that overrides the paintComponent method.
  3. In the paintComponent method, create a GradientPaint instance.
  4. In the example, the top of the rectangle has an alpha value of 0 (the most transparent) and the bottom has an alpha value of 255 (the most opaque). The GradientPaint class smoothly interpolates the alpha values from the top to the bottom of the rectangle.
  5. Set the GradientPaint instance as the panel's paint method.

Here is the code for the GradientTranslucentWindowDemo.java example. If the underlying platform does not support per-pixel translucency, this example exits. The code specifically relating to creating the gradient window is shown in bold.

import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*;  public class GradientTranslucentWindowDemo extends JFrame {     public GradientTranslucentWindowDemo() {         super("GradientTranslucentWindow");          setBackground(new Color(0,0,0,0));         setSize(new Dimension(300,200));         setLocationRelativeTo(null);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          JPanel panel = new JPanel() {             @Override             protected void paintComponent(Graphics g) {                 if (g instanceof Graphics2D) {                     final int R = 240;                     final int G = 240;                     final int B = 240;                      Paint p =                         new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),                             0.0f, getHeight(), new Color(R, G, B, 255), true);                     Graphics2D g2d = (Graphics2D)g;                     g2d.setPaint(p);                     g2d.fillRect(0, 0, getWidth(), getHeight());                 }             }         };         setContentPane(panel);         setLayout(new GridBagLayout());         add(new JButton("I am a Button"));     }      public static void main(String[] args) {         // Determine what the GraphicsDevice can support.         GraphicsEnvironment ge =              GraphicsEnvironment.getLocalGraphicsEnvironment();         GraphicsDevice gd = ge.getDefaultScreenDevice();         boolean isPerPixelTranslucencySupported =              gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);          //If translucent windows aren't supported, exit.         if (!isPerPixelTranslucencySupported) {             System.out.println(                 "Per-pixel translucency is not supported");                 System.exit(0);         }          JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread         SwingUtilities.invokeLater(new Runnable() {             @Override             public void run() {                 GradientTranslucentWindowDemo gtw = new                     GradientTranslucentWindowDemo();                  // Display the window.                 gtw.setVisible(true);             }         });     } } 

Note that the button is not affected by the per-pixel translucency. Setting the per-pixel translucency affects the background pixels only. If you want a window that has a uniformly translucent effect on the background pixels only, you can invoke setBackground(new Color(0,0,0,alpha)) where alpha specifies your desired translucency.

How to Implement a Shaped Window

You can create a shaped window by invoking the setShape(Shape) method in the Window class. The Shape argument that is passed to the method determines how the window is clipped. When a shape is set on a window, the window decorations are not re-formed to the new shape, so setting a shape works best on undecorated windows.

The best practice for setting the window's shape is to invoke setShape in the componentResized method of the component event listener. This practice will ensure that the shape is correctly calculated for the actual size of the window. The following example uses this approach.

The ShapedWindowDemo.java example creates an oval-shaped window with 70 percent opacity. If the underlying platform does not support shaped windows, the example exits. If the underlying platform does not support translucency, the example uses a standard opaque window. You could modify this example to create a shaped window that also uses per-pixel translucency.

The code relating to shaping the window is shown in bold.

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.Ellipse2D; import static java.awt.GraphicsDevice.WindowTranslucency.*;  public class ShapedWindowDemo extends JFrame {     public ShapedWindowDemo() {         super("ShapedWindow");         setLayout(new GridBagLayout());          // It is best practice to set the window's shape in         // the componentResized method.  Then, if the window         // changes size, the shape will be correctly recalculated.         addComponentListener(new ComponentAdapter() {             // Give the window an elliptical shape.             // If the window is resized, the shape is recalculated here.             @Override             public void componentResized(ComponentEvent e) {                 setShape(new Ellipse2D.Double(0,0,getWidth(),getHeight()));             }         });          setUndecorated(true);         setSize(300,200);         setLocationRelativeTo(null);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          add(new JButton("I am a Button"));     }      public static void main(String[] args) {         // Determine what the GraphicsDevice can support.         GraphicsEnvironment ge =              GraphicsEnvironment.getLocalGraphicsEnvironment();         GraphicsDevice gd = ge.getDefaultScreenDevice();         final boolean isTranslucencySupported =              gd.isWindowTranslucencySupported(TRANSLUCENT);          //If shaped windows aren't supported, exit.         if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) {             System.err.println("Shaped windows are not supported");             System.exit(0);         }          //If translucent windows aren't supported,          //create an opaque window.         if (!isTranslucencySupported) {             System.out.println(                 "Translucency is not supported, creating an opaque window");         }          // Create the GUI on the event-dispatching thread         SwingUtilities.invokeLater(new Runnable() {             @Override             public void run() {                 ShapedWindowDemo sw = new ShapedWindowDemo();                  // Set the window to 70% translucency, if supported.                 if (isTranslucencySupported) {                     sw.setOpacity(0.7f);                 }                  // Display the window.                 sw.setVisible(true);             }         });     } } 

 

 

 

=================================

=================================

=================================

 

 

 

 

반응형


관련글 더보기

댓글 영역