Java图形与文本(3)|图形变换

这篇文章主要针对在Java中绘制图形时如何对图形进行缩放、旋转、斜切、填充渐进色、平移坐标轴的图形变换进行总结,与之前的其他图形和文本操作类似,本文提及的方法也是通过重写JComponent类的paint()方法,调用Graphics2D类中的相应方法进行实现的,故在重写时要将传入的参数强制类型转化成Graphics2D:

public abstract void scale(double sx,double sy);//对图形进行缩放,包括对图形进行放大、缩小和还原等操作
//参数sx,sy分别是与原图形x,y坐标值相乘的量,如果sx,sy大于1.0,则在相应的坐标轴上放大原图形;如果sx,sy小于1.0,则在相应的坐标轴缩小原图形;如果等于1.0,则是以原图形的大小显示原图形
public abstract void rotate(double theta,double x,double y);//旋转图形
//theta是旋转的角度,以弧度为单位,当大于0时是顺时针,小于0时是逆时针
//x,y是旋转原点的坐标
public abstract void shear(double shx,double shy);//斜切图形
//shx,shy分别是在正x轴、正y轴方向移动坐标的乘数,可以作为相应y、x坐标的函数(注意是相反的)
public abstract void setPaint(Paint paint);//图形填充渐变色
//paint是封装了渐变颜色的Paint对象
public GradientPaint(float x1,float y1,Color color1,float x2,float y2,Color color2,boolean cyclic);//使用GradientPaint类创建封装渐变颜色的对象
//参数x1,y1是用户空间中第1个指定点的坐标,x2,y2是用户空间中第2个指定点的坐标
//color1是第1个指定点处的Color对象,color2是第2个指定点处的Color对象
//cyclic:如果渐变模式在两种颜色之间重复循环,则该值设置为true,否则设置为false
public abstract void translate(int x,int y);//平移坐标轴
//x,y是指定要平移到的坐标

可能直接看完这些方法的定义也不知道如何下手使用,下面以一个例子来演示使用这些方法

class ZoomShapePanel extends JPanel{    //缩放图形
	public void paint(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		Rectangle2D.Float rect = new Rectangle2D.Float(120,50,80,50);      //创建矩形对象
		BasicStroke stroke = new BasicStroke(10);                                         //创建宽度为10的笔画对象
		g2.setStroke(stroke);																			 //设置笔画对象
		g2.clearRect(0,0,338,220);																	 //清除原有内容
		if(flag == 0){			//绘制原矩形
			g2.draw(rect);
		}else if(flag == 1){       //放大1.3倍
			g2.scale(1.3,1.3);
			g2.draw(rect);
		}else if(flag == 3){		//缩小0.5倍
			g2.scale(0.5,0.5);
			g2.draw(rect);
		}
	}
}
class RotateShapePanel extends JPanel{    //旋转图形
	public void paint(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		Rectangle2D.Float rect = new Rectangle2D.Float(120,50,80,50);      //创建矩形对象
		BasicStroke stroke = new BasicStroke(10);                                         //创建宽度为10的笔画对象
		g2.setStroke(stroke);																			 //设置笔画对象
		g2.clearRect(0,0,338,220);																	 //清除原有内容
		if(flag == 0){			//绘制原矩形
			g2.draw(rect);
		}else if(flag == 1){       //顺时针旋转
			g2.rotate(rotateValue);
			g2.draw(rect);
		}else if(flag == 3){		//逆时针旋转
			g2.rotate(rotateValue);
			g2.draw(rect);
		}
	}
}
class FillGradientPanel extends JPanel{    //斜切图形
	public void paint(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		Rectangle2D.Float rect = new Rectangle2D.Float(20,20,280,140);      //创建矩形对象
		GradientPaint paint = new GradientPaint(20,20,Color.BLUE,100,80,Color.RED,true);	//创建循环渐变的GradientPaint对象
		g2.setPaint(paint);						//设置渐变
		g2.fill(rect);	                                //绘制矩形
	}
}
class ShearShapePanel extends JPanel{    //斜切图形
	public void paint(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		Rectangle2D.Float rect = new Rectangle2D.Float(120,50,80,50);      //创建矩形对象
		BasicStroke stroke = new BasicStroke(10);                                         //创建宽度为10的笔画对象
		g2.setStroke(stroke);																			 //设置笔画对象
		g2.clearRect(0,0,338,220);																	 //清除原有内容
		if(flag == 0){			//绘制原矩形
			g2.draw(rect);
		}else if(flag == 1){       //向下斜切
			g2.shear(0.2,0.2);
			g2.draw(rect);
		}else if(flag == 3){		//向上斜切
			g2.shear(-0.2,-0.2);
			g2.draw(rect);
		}
	}
}
class TranslationAxisPanel extends JPanel{    //平移坐标轴
	public void paint(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		Rectangle2D.Float rect = new Rectangle2D.Float(120,50,80,50);      //创建矩形对象
		BasicStroke stroke = new BasicStroke(10);                                         //创建宽度为10的笔画对象
		g2.setStroke(stroke);																			 //设置笔画对象
		g2.clearRect(0,0,338,220);																	 //清除原有内容
		if(flag == 0){			//平移坐标轴
			g2.translate(0,0);
			g2.draw(rect);
		}else if(flag == 1){       //平移坐标轴
			g2.translate(120,60);
			g2.draw(rect);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/marwi_study/article/details/88858329