Sun Jun 17, 2007 4:40 am
package lgfRoundGauge;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class RoundGaugePanel extends JPanel implements Runnable {
int targetValue = 0;
int MAX_VALUE = 100; // default to 100 for now
int value = 0;
Thread repaintThread;
int degreesPerSecond = 1;
Dimension Size;
double gaugeWidth;
double gaugeHeight;
int centerX = (int)(gaugeWidth/2.0);
int centerY = (int)(gaugeHeight/2.0);
double zeroAngle = 225.0;
double maxAngle = -45;
double range = zeroAngle - maxAngle;
RoundGaugePanel(Dimension size) {
Size = size;
gaugeWidth = Size.width * 0.75;
gaugeHeight = Size.height * 0.75;
centerX = (int)(gaugeWidth/2.0);
centerY = (int)(gaugeHeight/2.0);
setSize(Size);
setMaximumSize(Size);
setPreferredSize(Size);
repaintThread = new Thread( this );
repaintThread.setDaemon(true);
repaintThread.start();
}
public void updateValue( int i ){ targetValue = i; }
public void setMaxValue( int i) { MAX_VALUE = i; }
private void paintTheBackground( Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, Size.width, Size.height);
g.setColor(Color.white);
g.fillOval(0, 0, (int)gaugeWidth, (int)gaugeHeight);
// now the lines and the arcs on the gauge
g.setColor( Color.lightGray);
g.drawLine(centerX, centerY, (int) gaugeWidth -25, (int)gaugeHeight-25);
g.drawLine(centerX, centerY, 23, (int)gaugeHeight-25);
g.setColor( Color.blue);
g.drawArc( 10, 10, (int)gaugeWidth-20, (int)gaugeHeight-20, -45, 270);
// yellow doesn't show up really well on a white background
//g.setColor( Color.yellow);
//g.drawArc( 10, 10, (int)gaugeWidth-20, (int)gaugeHeight-20, 0, 45);
// this red line doesn't add much to the gauge so....
//g.setColor( Color.red);
//g.drawArc( 10, 10, (int)gaugeWidth-20, (int)gaugeHeight-20, -45, 45 );
g.setColor( Color.black);
g.drawString("0%", centerX - 45, centerY + 55);
g.drawString("50%", centerX - 5, centerY - 50);
g.drawString("100%", centerX + 15, centerY + 55);
}
public void paintComponent(Graphics g){
Color oldColor;
oldColor = g.getColor();
paintTheBackground( g);
g.setColor(Color.red);
int x1 = centerX,
x2 = x1,
y1 = centerY,
y2 = y1;
double angleToUse = zeroAngle - 1.0 * range *( value * 1.0 / MAX_VALUE * 1.0);
x2 += (int)( Math.cos(Math.toRadians(angleToUse))*centerX);
y2 -= (int)( Math.sin(Math.toRadians(angleToUse))*centerY);
g.drawLine(x1, y1, x2, y2 );
g.setColor(Color.black);
g.drawString(""+ value, centerX - 10, centerY + 30);
g.setColor(oldColor);
}
public void run() {
int oldValue = 32768;
while ( true ) {
try {
Thread.sleep(100);
} catch( InterruptedException ie) { /**/ }
if ( targetValue != value ) {
if ( degreesPerSecond value ) value += degreesPerSecond;
}
if( oldValue != value) {
repaint();
oldValue = value;
}
} // close while()
} // close run()
} // close class RoundGaugePanel
package lgfRoundGauge;
import java.awt.Dimension;
import javax.swing.JFrame;
/*
* this code started 15JUN2007 17:10 pm
* finished 15JUN2007 23:07 pm
*/
public class RoundGauge {
RoundGaugePanel rp;
Dimension size = new Dimension( 200, 200);
public RoundGauge(){
rp = new RoundGaugePanel( size );
JFrame frame = new JFrame("RoundGaugePanel Test" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(size);
frame.setMaximumSize(size);
frame.setPreferredSize(size);
frame.add(rp);
frame.pack();
frame.setVisible(true);
}
public void updateValue( int i) { rp.updateValue( i ); }
public void setMaxValue( int i) { rp.setMaxValue(i); }
public static void main(String[] args) {
RoundGauge r = new RoundGauge();
r.setMaxValue(10000);
// I'm not messing with the event dispatch thread here 'cause
// nothing touches the swing components at all....one of them
// reads an interger variable I'm modifying but that's okay
for( int i = 0; i < 99; i++) {
try {
r.updateValue(i * 100 );
Thread.sleep(100);
} catch(InterruptedException ie) {/**/}
}
} // close main
} // close class RoundGauge
Sun Jun 17, 2007 12:06 pm
Sun Jun 17, 2007 11:01 pm
Sun Feb 08, 2009 7:08 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.