📄 progressbar.java
字号:
/*
$Author: $
$Date: $
$Revision: $
$NoKeywords: $
*/
package jp.co.ntl.awt;
import java.awt.Component;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.FontMetrics;
import jp.co.ntl.Util;
public class ProgressBar extends Component {
/**
*
*/
private static final long serialVersionUID = 1L;
private float ratio;
private Color progressColor;
private String text;
private Image imageBuffer = null;
private Graphics graphicsImage = null;
public ProgressBar() {
setSize(100, 20);
progressColor = Color.red;
setBackground(Color.blue);
setForeground(Color.white);
}
public void setRatio(float ratio) {
this.ratio = ratio;
repaint();
}
public void setText(String text) {
this.text = text;
repaint();
}
public void setProgressColor(Color progressColor) {
this.progressColor = progressColor;
repaint();
}
public void update(Graphics g) {
paint(g);
}
public void invalidate() {
super.invalidate();
imageBuffer = null;
if (graphicsImage != null) {
graphicsImage.dispose();
}
}
public void paint(Graphics g) {
Dimension d = getSize();
int width = d.width;
int height = d.height;
if (imageBuffer == null) {
imageBuffer = createImage(width, height);
graphicsImage = imageBuffer.getGraphics();
}
super.paint(graphicsImage);
int sx = (int)(width * ratio);
graphicsImage.setColor(progressColor);
graphicsImage.fillRect(0, 0, sx, height);
graphicsImage.setColor(getBackground());
graphicsImage.fillRect(sx, 0, width - sx, height);
if (text != null) {
FontMetrics fm = g.getFontMetrics();
int bx = Util.basePointX(fm, text, width);
int by = Util.basePointY(fm, text, height);
graphicsImage.setColor(getForeground());
graphicsImage.drawString(text, bx, by);
}
g.drawImage(imageBuffer, 0, 0, this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -