Java箭头的绘制


	/**
	 * Method Name:drawAL
	 * Purpose:this class is copy from steven's parseLog file
	 */
	public static void drawAL(double sx, double sy, double ex, double ey,
			Graphics2D g2)
	{

		double H = 10; // 箭头高度
		double L = 4; // 底边的一半
		int x3 = 0;
		int y3 = 0;
		int x4 = 0;
		int y4 = 0;
		double awrad = Math.atan(L / H); // 箭头角度
		double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度
		double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len);
		double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len);
		double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点
		double y_3 = ey - arrXY_1[1];
		double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点
		double y_4 = ey - arrXY_2[1];

		java.lang.Double X3 = new java.lang.Double(x_3);
		x3 = X3.intValue();
		java.lang.Double Y3 = new java.lang.Double(y_3);
		y3 = Y3.intValue();
		java.lang.Double X4 = new java.lang.Double(x_4);
		x4 = X4.intValue();
		java.lang.Double Y4 = new java.lang.Double(y_4);
		y4 = Y4.intValue();
		// 画线
		g2.drawLine((int) sx, (int) sy, (int) ex, (int) ey);
		//
		GeneralPath triangle = new GeneralPath();
		triangle.moveTo(ex, ey);
		triangle.lineTo(x3, y3);
		triangle.lineTo(x4, y4);
		triangle.closePath();
		//实心箭头
		g2.fill(triangle);
		//非实心箭头
		//g2.draw(triangle);

	}

	// 计算
	public static double[] rotateVec(double e, double f, double ang,
			boolean isChLen, double newLen)
	{

		double mathstr[] = new double[2];
		// 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度
		double vx = e * Math.cos(ang) - f * Math.sin(ang);
		double vy = e * Math.sin(ang) + f * Math.cos(ang);
		if(isChLen)
		{
			double d = Math.sqrt(vx * vx + vy * vy);
			vx = vx / d * newLen;
			vy = vy / d * newLen;
			mathstr[0] = vx;
			mathstr[1] = vy;
		}
		return mathstr;
	}

你可能感兴趣的:(java)