Total members 11894 |It is currently Thu Nov 21, 2024 11:06 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Here's another widget I built using Swing. This one is a round gauge like an ammeter. I've set it up to function as a sort of progress meter but you can modify it to do whatever you want. There's two files to this eclipse project.



This is the first file:

java code
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


Here's the second of two files:

java code
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


All this code is in the public domain so you can do with it whatever you'd like




Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time

Thanks , Very Cool one , :D

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Thanks for this good topic.

_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

Thx!!!! Cool


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 4 posts ] 

  Related Posts  to : an ammeter ( round gauge ) sortof widget built using Swing
 a moving graph sortof widget build using Swing     -  
 old NASA style counter built from Swing components     -  
 draw Round gradient     -  
 Draw Oval,Arc,Polygon,string,Line,Round and 3D Rectangle     -  
 Implementation of FCFS, SJFS, Round Robin and priority algo     -  
 What is Swing?!!     -  
 Swing: repaint and setVisible     -  
 JLabel Class Example Swing Package     -  
 JUnit Testing of GUIs in Swing     -  
 How to create a log in form using javax.swing     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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