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

📄 directionpainter.java

📁 用于串口通讯测试的工具软件。完全使用java语言编写。
💻 JAVA
字号:
package com.zcsoft.stock;

import java.awt.*;
import javax.swing.*;

/** 运动方向绘制类 */
public class DirectionPainter
{
	public static final String LEFTWARD = "left";
	public static final String RIGHTWARD = "right";
	public static final String UPWARD = "up";
	public static final String DOWNWARD = "down";

	private String direction;
	private java.awt.Point location = new Point();
	private java.awt.Dimension size;
	private java.awt.Color color;
	private java.awt.Image image;

	public DirectionPainter()
	{

	}

	public DirectionPainter(String direction)
	{
		this.setDirection(direction);
	}


	public void drawDirection(Graphics g, int x, int y, int times)
	{
		if (this.location != null)
		{
			x += location.x;
			y += location.y;
		}
		if (size == null)
		{
			if (image != null)
			{
				size = new Dimension(image.getWidth(null), image.getHeight(null));
			}
			else
			{
				size = new Dimension(18, 18);
			}
		}
		Color oldColor = g.getColor();
		g.setColor(this.color);
		if (times > 3)
		{
			if (image != null)
				g.drawImage(image, x, y, null);
			else
				drawDirection(g, x, y, size.width, size.height, this.direction);
			drawTimesNo(g, x, y, times);
		}
		else
		{
			for (int i = 0; i < times; i++)
			{
				if (image != null)
					g.drawImage(image, x, y, null);
				else
					drawDirection(g, x, y, size.width, size.height, this.direction);
				if (this.incrementX())
				{
					x += size.width;
				}
				else
				{
					y += size.height;
				}
			}
		}
		g.setColor(oldColor);
	}

	private void drawTimesNo(Graphics g, int x, int y, int times)
	{
		if (this.incrementX())
		{
			g.drawString(String.valueOf(times), x + size.width, y + size.height);
		}
		else
		{
			g.drawString(String.valueOf(times), x, y + size.height + g.getFontMetrics().getAscent());
		}
	}

	boolean incrementX()
	{
		return LEFTWARD.equals(this.direction) || RIGHTWARD.equals(this.direction);
	}

	static void drawDirection(Graphics g, int x, int y, int w, int h, String direction)
	{
		if (LEFTWARD.equals(direction))
		{
			drawLeftDirection(g, x, y, w, h);
		}
		else if (RIGHTWARD.equals(direction))
		{
			drawRightDirection(g, x, y, w, h);
		}
		else if (UPWARD.equals(direction))
		{
			drawUpDirection(g, x, y, w, h);
		}
		else if (DOWNWARD.equals(direction))
		{
			drawDownDirection(g, x, y, w, h);
		}
	}

	public static void drawLeftDirection(Graphics g, int x, int y, int w, int h)
	{
		g.fillPolygon(new int[]{x + (w >> 1), x, x + (w >> 1), x + (w >> 1)
										, x + w, x + w, x + (w >> 1)}
						  , new int[]{y, y + (h >> 1), y + h, y + (h << 1)/ 3, y + (h << 1)/ 3
										, y + h / 3, y + h / 3}
						  , 7);
	}

	public static void drawRightDirection(Graphics g, int x, int y, int w, int h)
	{
		g.fillPolygon(new int[]{x + (w >> 1), x + (w >> 1), x, x
										, x + (w >> 1), x + (w >> 1), x + w}
						  , new int[]{y, y + h / 3, y + h / 3, y + (h << 1)/ 3, y + (h << 1)/ 3
										, y + h, y + (h >> 1)}
						  , 7);
	}

	public static void drawUpDirection(Graphics g, int x, int y, int w, int h)
	{
		g.fillPolygon(new int[]{x + (w >> 1), x, x + w / 3, x + w / 3
										, x + (w << 1)/ 3, x + (w << 1)/ 3, x + w}
						  , new int[]{y, y + (h >> 1), y + (h >> 1), y + h, y + h
										, y + (h >> 1), y + (h >> 1)}
						  , 7);
	}

	public static void drawDownDirection(Graphics g, int x, int y, int w, int h)
	{
		g.fillPolygon(new int[]{x + w / 3, x + w / 3, x, x + (w >> 1)
										, x + w, x + (w << 1)/ 3, x + (w << 1)/ 3}
						  , new int[]{y, y + (h >> 1), y + (h >> 1), y + h
										, y + (h >> 1), y + (h >> 1), y}
						  , 7);
	}

	public void setDirection(String direction)
	{
		this.direction = direction.intern();
	}

//
//	public String getDirection()
//	{
//		return direction;
//	}


	public void setLocation(java.awt.Point location)
	{
		if (location != null)
		{
			this.location.setLocation(location.x, location.y);
		}
	}


	public java.awt.Point getLocation()
	{
		return location;
	}

	public void setSize(java.awt.Dimension size)
	{
		this.size = size;
	}

	public java.awt.Dimension getSize()
	{
		return size;
	}

	public void setColor(java.awt.Color color)
	{
		this.color = color;
	}


	public void setImage(java.awt.Image loadedImage)
	{
		this.image = loadedImage;
	}


//	public java.awt.Color getColor()
//	{
//		return color;
//	}

//	public static void main(String[] args)
//	{
//		JFrame f =  new JFrame();
//		java.awt.Canvas c = new java.awt.Canvas(f.getGraphicsConfiguration()){
//
//			DirectionPainter left = new DirectionPainter(DirectionPainter.LEFTWARD);
//			DirectionPainter right = new DirectionPainter(DirectionPainter.RIGHTWARD);
//			DirectionPainter up = new DirectionPainter(DirectionPainter.UPWARD);
//			DirectionPainter down = new DirectionPainter(DirectionPainter.DOWNWARD);
//
//			public void paint(Graphics g)
//			{
//				left.drawDirection(g, 0, 0, 10);
//				right.drawDirection(g, 200, 0, 2);
//				up.drawDirection(g, 0, 200, 300);
//				down.setColor(Color.red);
//				down.drawDirection(g, 200, 200, 3);
//			}
//		};
//		f.getContentPane().add(c);
//		f.setSize(400, 400);
//		f.show();
//
//	}
}

⌨️ 快捷键说明

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