⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 axis.java

📁 用于显示适时的曲线图
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.text.*;

class Axis extends Object
{
	private Vector  TextLabel = new Vector() ;
	private int TextType = 1;//0 :value, 1:format ,2 Label,3 Date(yyyymmddhhnnssmmm)
	private String Format = new String("n.0");//float format # a bit ,0 use full
	private String TimeFormat = new String("");
	boolean Visible = true;
	Applet a;

	String Title = new String("");
	Color TitleColor = Color.black;
	boolean TitleVisible = true;
	Font TitleFont  = new Font("Courier",Font.PLAIN,12);

	int TickLength = 6;//刻度长度,负值反方向,水平下为正向,垂直作为正向
	int LittleTickLength = 4;
	int TickInc = 5;
	boolean ShowInTick = true;
	float LittleTickSpace = 5;
	float TickSpace = 25;
	float TickValue = 4;

	private int Type = 0;// 0 left , 1 bottom , 2 right , 3 top ,
	private Point Start;
	private Point End;
	private boolean Horizontal = true;//是否水平
	boolean FlagTextOrValue = false;//显示文本值
	private float Max = 100;
	private float Min = 0;
//	private Time MaxTime;
//	private Time MinTime;
	private int Length;

	Color TickColor = Color.black;
	Color LineColor = Color.black;
	Color TextColor = Color.black;
	Font TextFont=new Font("Courier",Font.PLAIN,12);
	
	Axis(Applet a,int Type, Point s, Point e)
	{
		this.a = a;
		this.Type = Type;
		try
		{
			setPosition(s, e);
			setFormat("n.2");
		}
		catch(Exception ec)
		{
			System.out.println(ec);
		}
	}
	void setTextType(int t)
	{
		TextType = t;
	}
	int getTextType()
	{
		return TextType;
	}
	float getMap()
	{
		return (Max-Min)/Length;
	}
	String  getFormat()
	{
		return Format;
	}
	void setMaxMin(float max, float min)
	{
		Max = max>min?max:min; Min =  max<min?max:min; setPara();
	}
	float getMax()
	{
		return Max;
	}
	float getMin()
	{
		return Min;
	}
	float getTickValue(int i)
	{
		if(i>getTickCount() || i<0) return 0;
		return i*TickValue+Min;
	}
	private void setPara()
	{
		int i;
		float min_value = (Max - Min)/(Length*1.0f/((Horizontal?getLabelWidth():getLabelHeight())+(Horizontal?15:10)));
	if(min_value>1)
		{
			for(i=0;min_value>2;i++)
				min_value/=10;
			if(min_value<1)
			{
				if(min_value>0.35 && min_value<0.75) min_value = 0.5f;
				else	if(min_value<0.35 && min_value>0.15) min_value = 0.2f;
				else	if(min_value<0.15) min_value = 0.1f;
				else	min_value = 1.0f;
			}
			else
				min_value = Math.round(min_value);
			min_value *= Math.pow(10,i);
		}
		else
		{
			for(i=0;min_value<1;i++)
				min_value*=10;
			if(min_value<1.5) min_value = 1;
			else	if(min_value<2.5)  min_value = 2;
			else	if(min_value<3.5)  min_value = 3;
			else	if(min_value<7.5)  min_value = 5;
			else	min_value = 10;
			min_value /= Math.pow(10,i);
		}
		TickValue = min_value;
		TickSpace = TickValue/getMap();
	}
	void setTitle(String s)
	{
		Title = s;
	}
	int getTickWidth()
	{
		return Math.round(TickSpace);
	}
	int getLabelHeight()
	{
		Graphics g = a.getGraphics();
		Font t = g.getFont();
		g.setFont(TextFont);
		int height = g.getFont().getSize();
		g.setFont(t);
		return height;
	}
	int getLittleTickPosition(int i)
	{
		if(i>=getLittleTickCount()) return -1;
		return Math.round(Horizontal? i*TickSpace/TickInc + Start.x : Start.y - i*TickSpace/TickInc);
	}
	int getTickPosition(int i)
	{
		if(i>=getTickCount()) return -1;
		return Math.round(Horizontal? i*TickSpace + Start.x : Start.y - i*TickSpace);
		
	}
	int getLittleTickCount()
	{
		//return Math.round(Length/(TickSpace/TickInc)+1);//.intValue();
		return new Float(Length/(TickSpace/TickInc)+1).intValue();
	}
	int getTickCount()
	{
		return new Float(Length/TickSpace+1).intValue();
	}
	float getValue(int x)
	{
		return Min + (Horizontal? (x-Start.x)*getMap() : (Start.y-x)*getMap());
	}
	void setFormat(String format_s)// throws Exception//0 :value, 1:format ,2 Label,3 Date(yyyymmddhhnnssttt)
	{
		Format = format_s;	setPara();//	setLabelCharNum();
	}
	 int getCharNum()
	{
		int i;
		int max;
		int t;
		String s;
		String floats;
		String ints;
		int il;
		int fl;
		switch(TextType)//0 :value, 1:format ,2 Label,3 Date(yyyymmddhhnnssttt)y-m-d h:n:s.t 2003-8-9
		{
			case 0: for(i=0,max=0;i<getTickCount();i++)
						max = max>getLabel(i).length()?max:getLabel(i).length();
					return  max;
			case 1: il = (Format.substring(0,1).compareTo("n")==0||Format.substring(0,1).compareTo("N")==0)? 99 : Integer.parseInt(Format.substring(0,1));
					fl =(Format.substring(2).compareTo("n")==0||Format.substring(2).compareTo("N")==0)? 99 : Integer.parseInt(Format.substring(2));
					s = new Float(Max).toString();
					t = s.indexOf('.');
					if(t == -1) 	return il > s.length() ? s.length() : il;
					else return (il>t?t:il)+(fl==0?1:0)+(fl>s.length()-1-t?s.length()-1-t:fl);
			case 2: for(i=0,max=0;i<TextLabel.size();i++) 
						max = max>((String)TextLabel.elementAt(i)).length()?max:((String)TextLabel.elementAt(i)).length();
					return max;
			case 3: return TimeFormat.length();
			case 4: //float v = getTickValue()
					
					return 5;
		}
		return 8;
	}
	int getLabelWidth()
	{
		String str = new String("");
		Graphics g = a.getGraphics();
		if(!Visible)return 0;
		for(int i=0;i<getCharNum();i++) str +='A';
		//System.out.println(getCharNum());
		return g.getFontMetrics().stringWidth(str);
	}

	void setPosition(Point s, Point e)throws Exception
	{
		if(s.x!=e.x && s.y!=e.y) throw new Exception("Axis轴应是水平或垂直!");
		Horizontal =  s.x == e.x ? false : true;
		if(Horizontal)
		{
			Start = s.x<e.x ? s : e; 			End = s.x>e.x ? s : e; 
		}
		else
		{
			Start = s.y>e.y ? s : e; 			End = s.y<e.y ? s : e; 
		}
		Length = Math.abs(judgeHorizontal() ? Start.x - End.x : Start.y - End.y) ;
		if(Length<=1)throw new Exception("Axis轴的长度应大于1.");
		setPara();
	}
	void setTextColor(Color c)
	{
		TextColor = c;
	}
	int getLength()
	{
		return Length; 
	}
	boolean judgeHorizontal()
	{
		Horizontal =  Start.x == End.x ? false : true;
		return Horizontal;
	}
	void draw(Graphics g)
	{
		int i;		int j;   int t_x = 0;	 int t_y =0;
		int text_i;
		int line_length;
		int pi ;
		setPara();
		// = a.getGraphics();
		String str = new String("");
		judgeHorizontal();
		g.setColor(LineColor);
		g.setFont(TextFont);
		g.drawLine(Start.x, Start.y, End.x, End.y);
		if(Visible)
		{
			int s = 0; 
			int e = 0;
			switch(Type)
			{
				case 0:s = Start.y > End.y ? Start.y : End.y;
					   e = Start.y < End.y ? Start.y : End.y;
					   g.drawLine(Start.x-1, s+1, End.x-1, e);break;
				case 1:s = Start.x > End.x ? Start.x : End.x;
					   e = Start.x < End.x ? Start.x : End.x;
					   g.drawLine(s-1, Start.y+1, e, End.y+1);break;
				case 2:s = Start.y > End.y ? Start.y : End.y;
					   e = Start.y < End.y ? Start.y : End.y;
					   g.drawLine(Start.x+1, s+1, End.x+1, e);break;
				case 3:s = Start.x > End.x ? Start.x : End.x;
					   e = Start.x < End.x ? Start.x : End.x;
					   g.drawLine(s-1, Start.y-1, e, End.y-1);break;
			}
		if(Horizontal)
		{
			if(ShowInTick)
			{
				for(i=0;i<getLittleTickCount();i++)
				{
					pi = getLittleTickPosition(i);
					g.drawLine(pi, Start.y, pi, Start.y+LittleTickLength);
				}
			}
			for(i=0;i<getTickCount();i++)
			{
				pi = getTickPosition(i);
				g.setColor(TickColor);
				g.drawLine(pi, Start.y, pi, Start.y+TickLength);
				g.setColor(TextColor);
				str = getLabel(i);
				if(str!=null)
				{
					t_x = pi - g.getFontMetrics().stringWidth(str)/2;
					t_y = TickLength<=0 ? Start.y +3 : Start.y+TickLength+3;
					t_y += g.getFont().getSize();
					g.drawString(str,t_x,t_y);
				}
			}
			if(TitleVisible)
			{
				g.setColor(TitleColor);
				g.setFont(TitleFont);
				g.drawString(Title,t_x+g.getFontMetrics().stringWidth(str)+15,t_y);
			}
		}
		else
		{
			if(ShowInTick)
			{
				g.setColor(TickColor);
				for(i=0;i<getLittleTickCount();i++)
				{
					pi = getLittleTickPosition(i);
					g.drawLine(Start.x, pi, Start.x-LittleTickLength, pi );
				}
			}
			for(i=0;i<getTickCount();i++)
			{
				pi = getTickPosition(i);
				g.setColor(TickColor);
				g.drawLine(Start.x, pi, Start.x-TickLength, pi);
				g.setColor(TextColor);
				str = getLabel(i);
				if(str!=null)
				{
					t_y = pi + g.getFont().getSize()/2 ;
					t_x = TickLength<=0 ?  Start.x -3 : Start.x-TickLength-3;
					t_x -= g.getFontMetrics().stringWidth(str);
					g.drawString(str,t_x,t_y);
				}
			}
			if(TitleVisible)
			{
				g.setColor(TitleColor);
				g.setFont(TitleFont);
				g.drawString(Title,End.x-g.getFontMetrics().stringWidth(Title)-5,End.y-10);
			}
			
		}
		}
		
	}
	String getLabel(int i)//0 :value, 1:format ,2 Label,3 Date(yyyymmddhhnnssttt)y-m-d h:n:s.t 2003-8-9
	{
		int il;
		int fl;
		String s = new String("");
		if(i>=getTickCount()) return null;
		switch(TextType)
		{
			case 0: return Float.toString(getTickValue(i));
			case 1: il = (Format.substring(0,1).compareTo("n")==0||Format.substring(0,1).compareTo("N")==0)? 20 : Integer.parseInt(Format.substring(0,1));
					fl =(Format.substring(2).compareTo("n")==0||Format.substring(2).compareTo("N")==0)? 20 : Integer.parseInt(Format.substring(2));
					if(fl!=0)
						return new Float(Math.round(getTickValue(i)*Math.pow(10,fl))*1.0/Math.pow(10,fl)).toString();
					else
						return Integer.toString(Math.round(getTickValue(i)));
			case 2: return i<TextLabel.size() ? (String)TextLabel.elementAt(i) : null;
			case 3: return null;
			case 4: il = Math.round(getTickValue(i));
					return Integer.toString(il/60)+'\''+Integer.toString(il%60);
		}
		return null;
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -