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);
}
}


- 원본이미지






- 실행결과


원문 : Hacking Swing: Translucent Windows
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by 김형준


Trackback URL : http://www.jaso.co.kr/trackback/24

Leave a comment
« Previous : 1 : ... 370 : 371 : 372 : 373 : 374 : 375 : 376 : 377 : 378 : ... 388 : Next »