?? drawer.java
字號:
/*
* Drawer.java
*
* Created on June 8, 2007, 11:52 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dyno.swing.beans;
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.JComponent;
/**
* 該類是個輔助類,目的是實現動畫狀態時透明圖像及組件的移動效果
* 像是一個抽屜,放應用程序的組件
*
* @author William Chen
*/
class Drawer extends JComponent {
//目前抽屜抽開比例,同時是透明度值
private double ratio = 1.0;
//目前是否處于動畫狀態
private boolean animating;
//應用程序的組件
private JComponent content;
//保存有應用程序組件的虛屏圖像
private Image offImage;
/** Creates a new instance of Drawer */
Drawer(double r, JComponent comp) {
this.ratio = r;
this.content = comp;
add(comp);
setLayout(null);
}
int getContentHeight() {
return content.getHeight();
}
int getContentWidth() {
return content.getWidth();
}
void setContentWidth(int w){
Rectangle bounds=content.getBounds();
bounds.width=w;
content.setBounds(bounds);
}
void setRatio(double ratio) {
this.ratio = ratio;
repaint();
}
double getRatio() {
return this.ratio;
}
//覆蓋父類的paintChildren
protected void paintChildren(Graphics g) {
if (animating) {
//動畫狀態畫出當前透明的應用程序組件以及抽拉狀態
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, (float) ratio));
g2d.drawImage(getOffImage(), 0, getHeight() - content.getHeight(),
this);
} else {
//普通狀態簡單使用容器缺省的渲染
super.paintChildren(g);
}
}
void clearOffImage() {
offImage = null;
}
void setAnimating(boolean animating) {
this.animating = animating;
}
//創建應用程序組件的虛屏圖像
private Image getOffImage() {
if (offImage == null) {
int contentWidth = content.getWidth();
int contentHeight = content.getHeight();
if (offImage == null) {
offImage = createImage(contentWidth, contentHeight);
Graphics g = offImage.getGraphics();
//使用Renderer機制
content.paint(g);
}
}
return offImage;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -