java2d常用api练习

package java2d;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * java 2d 常用api 练习
 * @author tiger
 */
public class GraphicsTest extends JPanel{

	
	public GraphicsTest() {
		this.setSize(500, 300);
		this.setPreferredSize(this.getSize());
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		
		/**
		 * rotate
		 * 
		 * 1, rotate(double theta)
		 * 将当前的 Graphics2D Transform 与旋转转换连接。后续呈现相对于前一原点旋转指定弧度。
		 * 这等同于调用 transform(R),其中 R 为以下矩阵表示的 AffineTransform : 
         *        [   cos(theta)    -sin(theta)    0   ]
         *        [   sin(theta)     cos(theta)    0   ]
         *        [       0              0         1   ]
 		 * 使用正角度 theta 进行旋转,可将正 x 轴上的点转向正 y 轴。 
 		 * 参数:theta - 旋转的角度,以弧度为单位
		 * 
		 * 
		 * 2, rotate(double theta, double x,double y)
		 * 将当前的 Graphics2D Transform 与平移后的旋转转换连接。
		 * 后续呈现的变换如下:平移到指定位置,旋转指定弧度,然后向回平移相同的距离。
		 * 这等同于以下调用序列: 
         *        translate(x, y);
         *        rotate(theta);
         *        translate(-x, -y);
 		 *	使用正角度 theta 进行旋转,可将正 x 轴上的点转向正 y 轴。 
		 * 
		 */
//		Graphics2D g2d = (Graphics2D) g.create();
//		
//		g2d.setColor(Color.GRAY.brighter());
//		g2d.fillRect(50, 50, 50, 50);
//		
//		g2d.rotate(Math.toRadians(45));
//		g2d.setColor(Color.GRAY.darker());
//		g2d.fillRect(50, 50, 50, 50);
//		
//		g2d = (Graphics2D) g.create();
//		g2d.rotate(Math.toRadians(45), 75, 75); //平移到指定位置,旋转指定弧度,然后向回平移相同的距离。
//		g2d.fillRect(50, 50, 50, 50);
//		
//		g2d.dispose();
		
		
		
		Graphics2D g2 = (Graphics2D) g;
//		for (int i = 1; i <= 10; i++) {
//			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, i * 0.1f));
//			g2.fillRect(40 * (i - 1), 20, 40, 40);
//		}
		
		
		/**
		 * 半透明
		 * 
		 * AlphaComposite 类实现一些基本的 alpha 合成规则,将源色与目标色组合,在图形和图像中实现混合和透明效果。
		 */
//		Rectangle2D r0 = new Rectangle2D.Double(130, 80, 40, 140);
//		g2.setPaint(Color.yellow);
//		g2.fill(r0);
//
//		Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F);
//		g2.setComposite(c);
//
//		Rectangle2D r1 = new Rectangle2D.Double(100, 100, 100, 100);
//		g2.setPaint(Color.red);
//		g2.fill(r1);
//
//		c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3F);
//		g2.setComposite(c);
//		g2.setColor(Color.blue);
//		g2.drawString("seeing through it with an AlphaComposite", 25, 150);
		
		
		//画椭圆和正方形
//		Color c0 = Color.orange, c1 = Color.cyan;
//
//		Ellipse2D e = new Ellipse2D.Double(80, 100, 180, 80);
//		g2.setPaint(c1);
//		g2.fill(e);
//
//		Rectangle2D r = new Rectangle2D.Double(50, 50, 100, 200);
//		g2.setPaint(c0);
//		g2.setStroke(new BasicStroke(10));
//		g2.draw(r);

		 /**
		 * ----纹理
		 * TexturePaint 类提供一种用被指定为 BufferedImage 的纹理填充 Shape 的方式。 因为 BufferedImage 数据由
		 * TexturePaint 对象复制,所以 BufferedImage 对象的大小应该小一些。 在构造时,纹理定位在用户空间中指定的 Rectangle2D
		 * 的左上角。
		 * 
		 * 在理论上,计算纹理在设备空间中的位置的方式是, 在用户空间中的所有方向上无限制地复制指定 Rectangle2D,然后将 BufferedImage
		 * 映射到各个复制的 Rectangle2D。 
		 * 
		 */
//		int imW=10, imH=10;
//		BufferedImage im = new BufferedImage(imW, imH, BufferedImage.TYPE_INT_RGB);
//		Graphics2D g2im = im.createGraphics();
//
//		g2im.setColor(new Color(192, 192, 192));
//		g2im.fillRect(0, 0, imW, imH);
//		g2im.setColor(Color.blue);
//		g2im.fillOval(imW / 8, imW / 8, 3 * imW / 4, 3 * imH / 4);
//		g2im.setColor(Color.red);
//		g2im.setStroke(new BasicStroke(2));
//		g2im.drawLine(0, 0, imW, imH);
//		g2im.drawLine(0, imH, imW, 0);
//		
////		g2im.setColor(Color.black);
////		g2im.fillRoundRect(0, 0, 10, 10, 45, 45);
////		g2im.setColor(Color.white);
////		g2im.drawLine(0, 5, 10, 5);
////		g2im.drawLine(5, 0, 5, 10);
//		
//		g2.drawImage(im, 2, 2, null);
//
//		TexturePaint tp = new TexturePaint(im, new Rectangle(imW, imH));
//		g2.setPaint(tp);
//		g2.fill(new RoundRectangle2D.Float(75, 20, 150, 200, 30, 30));
//
//		g2.setFont(new Font("Verdana", Font.BOLD, 24));
//		g2.drawString("Here's the text", 10, 260);

		
		
		/**
		 * clip
		 */
//		int w=300, h=300; // size of canvas
//
//		g2.setColor(Color.BLACK);
//		g2.fillRect(0, 0, w, h);
//
//		g2.clip(new Ellipse2D.Float(w / 4, h / 4, w / 2, h / 2));
//		g2.setPaint(new GradientPaint(0, 0, Color.cyan, 0, h / 4, Color.blue, true));
//		g2.fillRect(0, 0, w, h);
//
//		g2.clip(new Ellipse2D.Float((w / 2) - 10, (h / 2) - 10, 20, 20));
//		g2.setPaint(Color.red);
//		g2.fillRect(0, 0, w, h);
		
		
		/**
		 * GeneralPath -- 画五角星
		 */
//		int[] x = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
//	    int[] y = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};
//	
//	    GeneralPath star = new GeneralPath();
//	    star.moveTo(x[0],y[0]);
//	    for (int i=1; i<x.length; ++i) 
//	    {	    	
//	    	star.lineTo(x[i], y[i]);
//	    }
//	    star.closePath(); // 作用类似于:star.lineTo(x[0], y[0]);   ---- 通过绘制一条向后延伸到最后一个 moveTo 的坐标的直线,封闭当前子路径。
//	
//	    g2.setColor(Color.pink);
//	    g2.fill(star);
//	    g2.setColor(Color.blue);
//	    g2.draw(star);
		
		/**
	     * 渐变
	     * GradientPaint 类提供了使用线性颜色渐变模式填充 Shape 的方法。
	     * 
	     * 如果在用户空间指定了 Point P1 的 Color 为 C1,Point P2 的 Color 为 C2,则 P1、P2 连接线上的 Color 是逐渐地从 C1 变化到 C2 的。
	     * 任何不在 P1、P2 延长线上的点 P 都具有点 P' 的颜色,P' 是 P1、P2 延长线上点 P 的垂直投影。
	     * 
	     * P1、P2 段以外的延长线上的点可以按以下两种方式之一进行着色。
	     * 如果渐变是周期性的,那么 P1、P2 延长线上的点在颜色 C1 和 C2 之间来回循环。 
	     * 如果渐变是非周期性的,那么 P1 一边的点有固定的 Color C1,而 P2 一边的点有固定的 Color C2。 
	     * 
	     */
//		Rectangle2D r0 = new Rectangle2D.Double(50,50,50,50);
//	    Rectangle2D r1 = new Rectangle2D.Double(200,50,50,50);
//	    Rectangle2D r2 = new Rectangle2D.Double(50,150,200,90);
//
//	    Color c0 = Color.yellow, c1 = Color.blue;
//
//	    GradientPaint gp = new GradientPaint(0, 0, c0, 5, 5, c1, true);
//	    g2.setPaint(gp);
//	    g2.fill(r0);
//	    g2.setStroke(new BasicStroke(10));
//	    g2.draw(r1);
//
//	    gp = new GradientPaint(50, 200, c1, 250, 200, c0, false);
//	    g2.setPaint(gp);
//	    g2.fill(r2);
//
//	    Rectangle2D r3 = new Rectangle2D.Double(300,150,50,50);
//	    gp = new GradientPaint(300, 150, c1, 310, 160, c0, true);
//	    g2.setPaint(gp);
//	    g2.fill(r3);
		
		/**
		 * BasicStroke --- 笔画
		 */
//		BasicStroke bs = new BasicStroke(10);
//		Rectangle2D r = new Rectangle2D.Double(10, 50, 280, 90);
//		g2.setStroke(bs);
//		g2.draw(r);
//
//		bs = new BasicStroke(20, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
//		r = new Rectangle2D.Double(30, 75, 60, 40);
//		g2.setStroke(bs);
//		g2.draw(r);
//
//		bs = new BasicStroke(20, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
//		r = new Rectangle2D.Double(120, 75, 60, 40);
//		g2.setStroke(bs);
//		g2.draw(r);
//
//		bs = new BasicStroke(20, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
//		r = new Rectangle2D.Double(210, 75, 60, 40);
//		g2.setStroke(bs);
//		g2.draw(r);

	    
	}
	
	
	public static void main(String[] args) {
		
		GraphicsTest testPanel = new GraphicsTest();
		JFrame frame = new JFrame("tiger");
		frame.getContentPane().add(testPanel);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null); //居中
		frame.setVisible(true);
	}
		
	
}
原文地址:https://www.cnblogs.com/chaohi/p/1970802.html