📄 gauge.java.svn-base
字号:
package gui;import java.awt.Graphics;import java.awt.Font;import java.awt.Color;import java.util.*;import javax.swing.*;public class Gauge extends JPanel{ private final int NUMBER_OF_TICKS = 10; private final int TEXT_OFFSET_X = -7; private final int TEXT_OFFSET_Y = 5; private final double TEXT_POSITION = .7; //how far in the gauge the text labels are as a percentage of the radius private String title; private int radius; private double minimum; private double maximum; private double radiansNotUsed; private double orientation; //where in radians is the bottom private double value; private int xOffset; private int yOffset; private int centerX; private int centerY; private double zero; //in radians public Gauge( String titleTemp, int radiusTemp, double minimumTemp, double maximumTemp, double radiansNotUsedTemp, double orientationTemp, int xOffsetTemp, int yOffsetTemp, double startValue ) { title = titleTemp; radius = radiusTemp; minimum = minimumTemp; maximum = maximumTemp; radiansNotUsed = radiansNotUsedTemp; orientation = orientationTemp; xOffset = xOffsetTemp; yOffset = yOffsetTemp; value = startValue; centerX = xOffset + radius; centerY = yOffset + radius; zero = orientation - radiansNotUsed / 2; setBackground( Color.BLACK ); } public void setValue( double valueTemp ) { value = valueTemp; repaint(); } protected void paintComponent( Graphics g ) { super.paintComponent( g ); g.setColor( Color.WHITE ); double radianValue = zero - ( ( 2.0 * Math.PI - radiansNotUsed ) * value / ( maximum - minimum ) ); g.fillOval( xOffset, yOffset, 2 * radius, 2 * radius ); g.setColor( Color.BLACK ); g.setFont( new Font( "Helvetica", Font.PLAIN, 10 ) ); for( int i = 0; i <= NUMBER_OF_TICKS; i++ ) { g.drawLine( (int) ( centerX + .9 * radius * Math.cos( zero - i * ( 2.0 * Math.PI - radiansNotUsed ) / NUMBER_OF_TICKS ) ), (int) ( centerY - .9 * radius * Math.sin( zero - i * ( 2.0 * Math.PI - radiansNotUsed ) / NUMBER_OF_TICKS ) ), (int) ( centerX + radius * Math.cos( zero - i * ( 2.0 * Math.PI - radiansNotUsed ) / NUMBER_OF_TICKS ) ), (int) ( centerY - radius * Math.sin( zero - i * ( 2.0 * Math.PI - radiansNotUsed ) / NUMBER_OF_TICKS ) ) ); g.drawString( Integer.toString( (int) ( minimum + ( ( maximum - minimum ) * i / NUMBER_OF_TICKS ) ) ), TEXT_OFFSET_X + (int) ( centerX + TEXT_POSITION * radius * Math.cos( zero - i * ( 2.0 * Math.PI - radiansNotUsed ) / NUMBER_OF_TICKS ) ), TEXT_OFFSET_Y + (int) ( centerY - TEXT_POSITION * radius * Math.sin( zero - i * ( 2.0 * Math.PI - radiansNotUsed ) / NUMBER_OF_TICKS ) ) ); } g.drawLine( centerX, centerY, (int) ( centerX + radius * Math.cos( radianValue ) ), (int) ( centerY - radius * Math.sin( radianValue ) ) ); g.drawString( title, centerX - 20, centerY - 5 ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -