📄 graphpanel.java
字号:
package com.power.util.graph2D;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import com.power.pipeengine.EngineUI.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Paraster,Inc.</p>
* @author Charoes Huang
* @version 1.0
*/
public class graphPanel extends JPanel {
/**
* the original point of the axis
*/
private Point2D original = null;
/**
* the original data
*/
private Vector pointVect = null;
/**
* converted vector according to the axis' scale
*/
private Vector convertedPointVect = null;
/**
* X axis
*/
private axis xAxis = null ;
/**
* Y axis
*/
private axis yAxis = null ;
/**
* the max Integer data
*/
private double xMax = Double.NEGATIVE_INFINITY,yMax = Double.NEGATIVE_INFINITY;
private double xMin = Double.POSITIVE_INFINITY, yMin = Double.POSITIVE_INFINITY;
private boolean minOrMaxChanged = false;
/**
* the size of the panel
*/
private Dimension dimension;
private Graphics myPen ;
private Line2D.Double tmpLine = new Line2D.Double();
/*************
* construtor
**********/
/**
* the basic constructor method
*/
public graphPanel(){
super();
this.xAxis = new axis() ;
this.yAxis = new axis(1) ;
this.pointVect = new Vector();
this.convertedPointVect = new Vector();
}
/**
* the constructor with two parameters
* @param original the origianl point of the axises
* @param d the Dimension the JPanel
*/
public graphPanel(Point2D original,Dimension d){
this.dimension = d;
this.original = original;
this.xAxis = new axis() ;
this.yAxis = new axis(1) ;
xAxis.setAxisLength(d.getWidth()-original.getX()-20);
yAxis.setAxisLength(original.getY()-40 );
xAxis.setOriginal(original);
yAxis.setOriginal(original);
this.setLocation( (int) original.getX(), (int) original.getY() );
this.setSize( d );
this.pointVect = new Vector();
}
/**
* the constructor with three parameters
* @param original the original point of the axises
* @param vect the data vector
* @param d the Dimension of the JPanel
*/
public graphPanel(Point2D original,Vector vect,Dimension d){
this.dimension= d;
this.pointVect= vect ;
this.original = original;
this.xAxis = new axis() ;
this.yAxis = new axis(1) ;
xAxis.setAxisLength(d.getWidth()-original.getX()-20);
yAxis.setAxisLength(original.getY()-20 );
xAxis.setOriginal(original);
yAxis.setOriginal(original);
}
public axis getXAxis() {
return this.xAxis;
}
public axis getYAxis() {
return this.yAxis;
}
/******
* methods
**********/
/**
* set the Data Vector.
* @param vect
*/
public void setPointVect(Vector vect){
this.pointVect = vect ;
}
/**
* get the data vector
* @return
*/
public Vector getPointVect(){
return this.pointVect ;
}
/**
* set the axis color
* @param c
*/
public void setAxisColor(Color c){
xAxis.setAxisColor(c);
yAxis.setAxisColor(c);
}
public void setOrigin( Point2D point ) {
this.original = point;
xAxis.setOriginal(original);
yAxis.setOriginal(original);
}
public void setDimension( Dimension size ) {
this.dimension = size;
setAxises();
}
private void setAxises() {
xAxis.setAxisLength(dimension.getWidth()-original.getX()-20);
yAxis.setAxisLength(original.getY()-20 );
}
private void setMinAndMax( Vector points ) {
Enumeration allPoints = points.elements();
this.xMax = 0.0;
this.yMax = 0.0;
this.xMin = Double.POSITIVE_INFINITY;
this.yMin = Double.POSITIVE_INFINITY;
while( allPoints.hasMoreElements() ) {
Point2D.Double aPoint = (Point2D.Double) allPoints.nextElement();
if( aPoint.getX() > this.xMax ) {
this.xMax = aPoint.getX();
}
if( aPoint.getY() > this.yMax ) {
this.yMax = aPoint.getY();
}
if( aPoint.getX() < this.xMin ) {
this.xMin = aPoint.getX();
}
if( aPoint.getY() < this.yMin ) {
this.yMin = aPoint.getY();
}
}
if( this.xMax < 10.0 ) {
this.xMax = 10.0;
this.xMin = 0.0;
}
if( this.yMax < 10.0 ) {
this.yMax = 10.0;
this.yMin = 0.0;
}
//Avoid the case where xMin = xMax and yMin = yMax.
if( this.xMin <= 1.01 * this.xMax &&
this.xMin >= 0.99 * this.xMax ) {
if( this.xMax > 0 ) {
this.xMin = 0.9 * this.xMax;
} else {
this.xMin = 1.1 * this.xMax;
}
}
if( this.yMin <= 1.01 * this.yMax &&
this.yMin >= 0.99 * this.yMax ) {
if( this.yMax > 0 ) {
this.yMin = 0.9 * this.yMax;
} else {
this.yMin = 1.1 * this.yMax;
}
}
}
/**
* the vector convert method
* @param vect
* @return vector
*/
protected void convert(Vector vect){
try{
double xRatio = ( this.xMax - this.xMin ) / xAxis.getAxisLength() ;
double yRatio = ( this.yMax - this.yMin ) / yAxis.getAxisLength() ;
Iterator pointIter = vect.iterator() ;
Iterator convertedPoints = convertedPointVect.iterator();
while(pointIter.hasNext()){
Point2D.Double point = (Point2D.Double)pointIter.next() ;
Point2D.Double convertedPoint = (Point2D.Double) convertedPoints.next() ;
double x = ( point.getX() - this.xMin ) / xRatio +original.getX();
double y = original.getY() - ( point.getY() - this.yMin ) /yRatio;
//newpoint = new Point2D.Double( ( point.getX() - this.xMin ) / xRatio +original.getX(),
// original.getY() - ( point.getY() - this.yMin ) /yRatio );
convertedPoint.setLocation ( x, y );
//convertedVect.add(newpoint);
}
}catch(Exception e){
System.out.println(e.toString());
}
//return convertedPointVect;
}
private void convert(Point2D inPoint, Point2D outPoint ){
double xRatio = ( this.xMax - this.xMin ) / xAxis.getAxisLength() ;
double yRatio = ( this.yMax - this.yMin ) / yAxis.getAxisLength() ;
outPoint.setLocation( ( inPoint.getX() - this.xMin ) / xRatio +original.getX(),
original.getY() - ( inPoint.getY() - this.yMin ) /yRatio );
}
private double getMaxInt(double d){
int i=0;
while(d>10){
d=d/10;
i++;
}
return ((int)d+1)*Math.pow(10,i);
}
/**
* draw the components to the container.
* @param g
*/
public void paintComponent(Graphics g){
myPen = g ;
super.paintComponent(g);
xAxis.drawAxis(g);
yAxis.drawAxis(g);
Graphics2D g2= (Graphics2D) g ;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g2.setFont(new Font("",Font.PLAIN ,9));
//g2.drawString(java.lang.String .valueOf(0) ,(float)original.getX() ,(float)original.getY()+10);
drawDataLine(g);
drawLabel(g);
}
/**
* draw the data line
* @param g
*/
private void drawDataLine(Graphics g){
try {
if( this.pointVect.size() == 0 ) return;
Graphics2D g2=(Graphics2D) g ;
Iterator pointIter = this.convertedPointVect.iterator() ;
//Iterator pointIter = convert(this.pointVect).iterator() ;
//Point2D point1= original;
Point2D point1 = (Point2D)pointIter.next();
g2.setColor(Color.blue);
while( pointIter.hasNext() ){
Point2D point2 = (Point2D)pointIter.next();
g2.drawRect((int)point2.getX(),(int)point2.getY(),2,2);
//g2.drawOval((int)point2.getX(),(int)point2.getY(),2,2);
//Line2D line = new Line2D.Double(point1,point2);
tmpLine.setLine( point1.getX(), point1.getY(), point2.getX(), point2.getY() );
point1 = point2 ;
g2.draw(tmpLine);
}
} catch ( Exception e ) {
//concurrent modification exception, does not matter here.
}
}
/**
* draw the label
* @param g
*/
private void drawLabel(Graphics g){
if( this.pointVect.size() == 0 ) return;
Graphics2D g2=(Graphics2D) g ;
int xlabel_count = xAxis.getLabelCount();
int ylabel_count = yAxis.getLabelCount();
double xlength = xAxis.getAxisLength();
double ylength = yAxis.getAxisLength();
double xlabel_step = xlength / xlabel_count ;
double ylabel_step = ylength / ylabel_count ;
double xValueStep = ( this.xMax - this.xMin ) / 10;
double yValueStep = ( this.yMax - this.yMin ) / 10;
int xStrLengthOffset = String.valueOf( (int) this.xMax ).length();
if( xStrLengthOffset < String.valueOf( (int) this.xMin ).length() ) {
xStrLengthOffset = String.valueOf( (int) Math.abs(this.xMin ) ).length();
}
xStrLengthOffset -= 3;
int yStrLengthOffset = String.valueOf( (int) this.yMax ).length();
if( yStrLengthOffset < String.valueOf( (int) this.yMin ).length() ) {
yStrLengthOffset = String.valueOf( (int) Math.abs(this.yMin ) ).length();
}
yStrLengthOffset -= 3;
int i=0,j=0;
while( i < xlabel_count ){
String str = String.valueOf( (int)( this.xMin + xValueStep * i ) );
if( str.length() > xStrLengthOffset && xStrLengthOffset > 0 ) {
str = str.substring( 0, str.length() - xStrLengthOffset );
}
g2.drawString( str,
(float)( original.getX()+xlabel_step*i-10 ),
(float)( original.getY()+10 ) );
i++;
}
while( j < ylabel_count ){
String str = String.valueOf( ( int)(this.yMin + yValueStep * j ) );
if( str.length() > yStrLengthOffset && yStrLengthOffset > 0 ) {
str = str.substring( 0, str.length() - yStrLengthOffset );
}
g2.drawString( str,
(float)original.getX()-23,
(float)(original.getY()-ylabel_step*j)+5);
j++;
}
//repaint();
}
/**
* add a point to the point vetor
* @param point
*/
public void addPoint(Point2D point){
//myPen.clearRect(0,0,(int)this.dimension.getHeight(),(int)this.dimension.getWidth());
this.pointVect.add(point);
this.convertedPointVect.add( new Point2D.Double() );
minOrMaxChanged = false;
if( this.pointVect.size() == 1 ) {
if( point.getX() > 0 ) {
this.xMax = 1.1 * point.getX();
this.xMin = 0.9 * point.getX();
} else {
this.xMax = 0.9 * point.getX();
this.xMin = 1.1 * point.getX();
}
if( point.getY() > 0 ) {
this.yMax = 1.1 * point.getY();
this.yMin = 0.9 * point.getY();
} else {
this.yMax = 0.9 * point.getY();
this.yMin = 1.1 * point.getY();
}
}
if( point.getX() > this.xMax ) {
if( this.xMax > 0 ) {
this.xMax = 1.1 * point.getX();
} else {
this.xMax = 0.9 * point.getX();
}
minOrMaxChanged = true;
}
if( point.getX() < this.xMin ) {
if( this.xMin > 0 ) {
this.xMin = 0.9 * point.getX();
} else {
this.xMin = 1.1 * point.getX();
}
minOrMaxChanged = true;
}
if( point.getY() > this.yMax ) {
if( this.yMax > 0 ) {
this.yMax = 1.1 * point.getY();
} else {
this.yMax = 0.9 * point.getY();
}
minOrMaxChanged = true;
}
if( point.getY() < this.yMin ) {
if( this.yMin > 0 ) {
this.yMin = 0.9 * point.getY();
} else {
this.yMin = 1.1 * point.getY();
}
minOrMaxChanged = true;
}
if( this.xMax < 10.0 ) {
this.xMax = 10.0;
//this.xMin = 0.0;
minOrMaxChanged = true;
}
if( this.yMax < 10.0 ) {
this.yMax = 10.0;
//this.yMin = 0.0;
minOrMaxChanged = true;
}
//Avoid the case where xMin = xMax and yMin = yMax.
if( this.xMin <= 1.01 * this.xMax &&
this.xMin >= 0.99 * this.xMax ) {
if( this.xMax > 0 ) {
this.xMin = 0.9 * this.xMax;
} else {
this.xMin = 1.1 * this.xMax;
}
minOrMaxChanged = true;
}
if( this.yMin <= 1.01 * this.yMax &&
this.yMin >= 0.99 * this.yMax ) {
if( this.yMax > 0 ) {
this.yMin = 0.9 * this.yMax;
} else {
this.yMin = 1.1 * this.yMax;
}
minOrMaxChanged = true;
}
if( !minOrMaxChanged && convertedPointVect.size() >= 2 ) {
convert( point, (Point2D) convertedPointVect.lastElement() );
//drawLineSegment( );
} else {
convert(this.pointVect );
}
repaint();
}
private void drawLineSegment( ) {
Point2D from = (Point2D) convertedPointVect.elementAt( convertedPointVect.size() - 2 );
Point2D to = (Point2D) convertedPointVect.lastElement();
tmpLine.setLine( from.getX(), from.getY(), to.getX(), to.getY() );
((Graphics2D) myPen).draw( tmpLine );
((Graphics2D) myPen).drawRect((int)to.getX(),(int)to.getY(),2,2);
}
public void clear() {
this.pointVect = new Vector();
this.convertedPointVect = new Vector();
repaint();
xMin = Double.POSITIVE_INFINITY;
yMin = Double.POSITIVE_INFINITY;
xMax = Double.NEGATIVE_INFINITY;
yMax = Double.NEGATIVE_INFINITY;
}
public void update()
{ repaint();}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -