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

📄 legend.java

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

class  Legend
{
	Point LeftTop;
	boolean Visible=true;
	Applet a;
	int Width;
	int	Height;

	Font TextFont = new Font("Courier",Font.PLAIN,12);
	Vector Text = new Vector();
	Vector ColorVector = new Vector();

	int Margin = 4;
	int LeftMargin = 5;
	int RightMargin = 5;
	int ColorTextMargin = 4;

	int ColorWidth = 6;
	int ColorHeight = 6;

	Color ShadowColor = Color.darkGray;
	Color BackgoundColor = Color.white;
	Color BorderColor = Color.black;
	Color TextColor = Color.black;
	Legend(Applet a)
	{
		this.a = a;
	}
	Legend(Applet a, Point x)
	{
		this.a = a;
		LeftTop = x;
	}
	Legend(Applet a, int x, int y)
	{
		this.a = a;
		LeftTop = new Point(x,y);
	}
	void setPoint(int x, int y)
	{
		LeftTop = new Point(x,y);
	}
	void clear()
	{
		Text.removeAllElements();
		ColorVector.removeAllElements();
	}
	void del(int i)
	{
		Text.removeElementAt(i);
		ColorVector.removeElementAt(i);
	}
	void setPoint(Point x)
	{
		LeftTop = x;
	}
	void addLegend(String s, Color c)
	{
		Text.addElement(s);
		ColorVector.addElement(c);
	}
	int getWidth()
	{
		if(Text.size()==0)
			return 0;
		int width = 0;
		int w;
		Graphics g = a.getGraphics();
		Font t = g.getFont();
		g.setFont(TextFont);
		for(int i=0;i<Text.size();i++)
		{
			w = g.getFontMetrics().stringWidth((String)Text.elementAt(i));
			width = width > w ? width : w ;
		}
		g.setFont(t);
		return width+LeftMargin+RightMargin+ColorTextMargin+ColorWidth;
	}

	int getHeight()
	{
		if(Text.size()==0)
			return 0;
		Graphics g = a.getGraphics();
		Font t = g.getFont();
		g.setFont(TextFont);
		int height = Text.size()*(g.getFont().getSize()+Margin)+Margin;
		g.setFont(t);
		return height;
	}

	void draw(Graphics g)
	{
		int i;
		int j;
		int x;
		int y;
		if(Text.size()==0)return ;
		// = a.getGraphics();
		Font t = g.getFont();
		g.setFont(TextFont);
		g.setColor(ShadowColor);
		g.fillRect(LeftTop.x+5, LeftTop.y+5, getWidth(),getHeight());
		g.setColor(BackgoundColor);
		g.fillRect(LeftTop.x, LeftTop.y, getWidth(),getHeight());
		g.setColor(BorderColor);
		g.drawRect(LeftTop.x, LeftTop.y, getWidth(),getHeight());
		x = LeftTop.x+LeftMargin;
		y = LeftTop.y+Margin;
		for(i=0;i<Text.size();i++)
		{
			g.setColor((Color)ColorVector.elementAt(i));
			g.fillRect(x, y+g.getFont().getSize()/2-1, ColorWidth,ColorHeight);
			g.setColor(TextColor);
			g.drawString((String)Text.elementAt(i), x+ColorWidth+ColorTextMargin, y+g.getFont().getSize());
			y += g.getFont().getSize() + Margin;
		}
		g.setFont(t);
	}
};

⌨️ 快捷键说明

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