/**
* @(#) KuGou7UI.java 2011-6-30
* @author Dai,Yiyuan
* Hunan University
* Blog:http://colorfuldiary.blog.163.com
* www.flyingwind.net
* Email:falwujn@163.com
*/
package net.flyingwind.kugou7ui;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
* @author FlyingWind
*
*/
public class KuGou7UI extends JFrame {
/**
* 2011-6-30 下午7:40:40
*/
private static final long serialVersionUID = 1L;
private Item main;
private Item left;
private Item top;
private Item right;
private Item bottom;
private Item leftBT;
private Item topBT;
private Item rightBT;
private Item bottomBT;
public KuGou7UI() {
setSize(202, 202);
setPreferredSize(new Dimension(202, 202));
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
if (!getGraphicsConfiguration().isTranslucencyCapable()) {
System.err.println("per-pixel translucency not in effect for "
+ "this graphics configuration");
System.exit(0);
}
try {
BufferedImage bi = null;
bi = ImageIO.read(KuGou7UI.class
.getResourceAsStream("/KugouSkin.png"));
main = new Item();
main.setBi(bi.getSubimage(0, 0, 106, 106));
main.setBi_over(bi.getSubimage(0, 110, 106, 106));
main.setBounds(48, 48, 106, 106);
getContentPane().add(main);
setIconImage(main.getBi());
leftBT = new Item();
leftBT.setBi(bi.getSubimage(113, 179, 24, 24));
leftBT.setBounds(30, 90, 24, 24);
leftBT.setVisible(false);
getContentPane().add(leftBT);
topBT = new Item();
topBT.setBi(bi.getSubimage(113, 154, 24, 24));
topBT.setBounds(91, 24, 24, 24);
topBT.setVisible(false);
getContentPane().add(topBT);
rightBT = new Item();
rightBT.setBi(bi.getSubimage(138, 179, 24, 24));
rightBT.setBounds(158, 90, 24, 24);
rightBT.setVisible(false);
getContentPane().add(rightBT);
bottomBT = new Item();
bottomBT.setBi(bi.getSubimage(138, 154, 24, 24));
bottomBT.setBounds(91, 157, 24, 24);
bottomBT.setVisible(false);
getContentPane().add(bottomBT);
BufferedImage tempBi = bi.getSubimage(110, 0, 98, 100);
left = new Item();
left.setBi(tempBi);
left.setBounds(0, 54, 98, 100);
left.setVisible(false);
getContentPane().add(left);
top = new Item();
top.setBi(tempBi);
top.setBounds(53, 0, 98, 100);
top.setVisible(false);
getContentPane().add(top);
right = new Item();
right.setBi(tempBi);
right.setBounds(106, 54, 98, 100);
right.setVisible(false);
getContentPane().add(right);
bottom = new Item();
bottom.setBi(tempBi);
bottom.setBounds(53, 109, 98, 100);
bottom.setVisible(false);
getContentPane().add(bottom);
addMouseMotionListener(left);
addMouseMotionListener(top);
addMouseMotionListener(right);
addMouseMotionListener(bottom);
addMouseMotionListener(main);
} catch (IOException e) {
e.printStackTrace();
return;
}
setBackground(new Color(0, 0, 0, 0)); // Achieve per-pixel translucency.
pack();
}
Point downPos;
Point locPos;
boolean isVisible = false;
private void addMouseMotionListener(Container c) {
c.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
downPos = e.getLocationOnScreen();
locPos = KuGou7UI.this.getLocation();
}
@Override
public void mouseEntered(MouseEvent arg0) {
main.changeImage(true);
if (!isVisible)
setItemsVisible(true);
}
@Override
public void mouseExited(MouseEvent e) {
if (!isInArea(e.getPoint(), e.getComponent().getLocation())) {
main.changeImage(false);
setItemsVisible(false);
}
}
private boolean isInArea(Point point, Point loc) {
int x = loc.x + point.x;
int y = loc.y + point.y;
if (x < 101) {
if (y < 101) {
if (x + y > 101)
return true;
} else {
if (x - y > -101)
return true;
}
} else {
if (y < 101) {
if (x - y < 101)
return true;
} else {
if (x + y < 303)
return true;
}
}
return false;
}
});
c.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int dx = e.getXOnScreen() - downPos.x;
int dy = e.getYOnScreen() - downPos.y;
KuGou7UI.this.setLocation(locPos.x + dx, locPos.y + dy);
}
});
}
private void setItemsVisible(final boolean b) {
Thread th = new Thread() {
@Override
public void run() {
try {
changeUniformTranslucency(b);
top.setVisible(b);
topBT.setVisible(b);
Thread.sleep(100);
left.setVisible(b);
leftBT.setVisible(b);
Thread.sleep(100);
bottom.setVisible(b);
bottomBT.setVisible(b);
Thread.sleep(100);
right.setVisible(b);
rightBT.setVisible(b);
isVisible = b;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
th.start();
}
/**
*
* @param b
* 2011-7-2 下午07:05:32
*/
private void changeUniformTranslucency(final boolean b) {
Thread th = new Thread() {
@Override
public void run() {
try {
if (b) {
float opacity = KuGou7UI.this.getOpacity();
while (opacity < 1.0f) {
opacity += 0.1f;
KuGou7UI.this.setOpacity(opacity);
Thread.sleep(100);
}
} else {
float opacity = KuGou7UI.this.getOpacity();
while (opacity > 0.5f) {
opacity -= 0.1f;
KuGou7UI.this.setOpacity(opacity);
Thread.sleep(100);
}
}
} catch (Exception e) {
}
}
};
th.start();
}
public static void main(String[] args) {
GraphicsEnvironment ge;
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
if (!ge.getDefaultScreenDevice().isWindowTranslucencySupported(
GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {
System.err.println("per-pixel transparency isn't supported");
return;
}
new KuGou7UI().setVisible(true);
}
}