+ 3 different types: Windows, PQ1, PQ2
x Bugfix with border
This commit is contained in:
@ -5,24 +5,31 @@ import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
|
||||
class MyProgBar extends Canvas {
|
||||
final static int WINDOWS = 1;
|
||||
final static int PQ1 = 2;
|
||||
final static int PQ2 = 3;
|
||||
int minVal = 0;
|
||||
int maxVal = 100;
|
||||
int curVal = 50;
|
||||
Color fgColor = new Color(0,0,128);
|
||||
Color bgColor = Color.lightGray;
|
||||
Color txColor = Color.white;
|
||||
private int mode = WINDOWS;
|
||||
|
||||
public MyProgBar(int min, int max, int pos) {
|
||||
public MyProgBar(int min, int max, int pos, int mode) {
|
||||
super();
|
||||
this.minVal = min;
|
||||
this.maxVal = max;
|
||||
this.curVal = pos;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public MyProgBar(int min, int max, int pos) {
|
||||
this(min, max, pos, MyProgBar.WINDOWS);
|
||||
}
|
||||
|
||||
public MyProgBar(int min, int max) {
|
||||
super();
|
||||
this.minVal = min;
|
||||
this.maxVal = max;
|
||||
this(min, max, min);
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
@ -33,9 +40,28 @@ class MyProgBar extends Canvas {
|
||||
g.setColor(bgColor);
|
||||
g.fillRect(0,0,getSize().width-1,getSize().height-1);
|
||||
g.setColor(fgColor);
|
||||
g.fillRect(0,0,(int)((double)(getSize().width-1)/(maxVal-minVal)*(curVal-minVal)),getSize().height-1);
|
||||
int pright = (int)((double)(getSize().width-2)/(maxVal-minVal)*(curVal-minVal));
|
||||
g.fillRect(1,1,pright,getSize().height-2);
|
||||
g.setXORMode(txColor);
|
||||
g.drawString(percString, getSize().width/2-fm.stringWidth(percString)/2, getSize().height/2-fm.getHeight()/2+fm.getAscent());
|
||||
int tleft;
|
||||
switch (mode) {
|
||||
default:
|
||||
case MyProgBar.WINDOWS:
|
||||
g.drawString(percString, getSize().width/2-fm.stringWidth(percString)/2, getSize().height/2-fm.getHeight()/2+fm.getAscent());
|
||||
break;
|
||||
|
||||
case MyProgBar.PQ1:
|
||||
tleft = pright+2;
|
||||
if (tleft+fm.stringWidth(percString)>=getSize().width-1) tleft = getSize().width-1-fm.stringWidth(percString);
|
||||
g.drawString(percString, tleft, getSize().height/2-fm.getHeight()/2+fm.getAscent());
|
||||
break;
|
||||
|
||||
case MyProgBar.PQ2:
|
||||
tleft = (pright-1)/2-fm.stringWidth(percString)/2+1;
|
||||
if (tleft <= 2) tleft = 2;
|
||||
g.drawString(percString, tleft, getSize().height/2-fm.getHeight()/2+fm.getAscent());
|
||||
break;
|
||||
}
|
||||
g.setPaintMode();
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(0,0,getSize().width-1,getSize().height-1);
|
||||
@ -48,9 +74,13 @@ class MyProgBar extends Canvas {
|
||||
public void setMaxValue(int v) {
|
||||
maxVal = v;
|
||||
}
|
||||
|
||||
public void setMode(int m) {
|
||||
this.mode = m;
|
||||
}
|
||||
|
||||
public void setPos(int p) {
|
||||
curVal = p;
|
||||
paint(getGraphics());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user