Java ——数字图像处理(Java Graphics及其API简介)

1、创建一个Graphics对象
BufferedImage bi = new BufferedImage(120,120, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
2、控制图形边缘反锯齿
Graphics2D .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
3、线段 / 折线

//两点绘制线段
g2d.drawLine(50,50,200,50);
//多点绘制折线 点(50, 100), 点(100, 130), 点(150, 70), 点(200, 100)
int[] xpoints = new int[]{50,100,150,200};
int[] ypoints = new int[]{100,130,70,100};
int npoints=4;
g2d.drawPolyline(xpoints,ypoints,npoints);
g2d.dispose();
//两点绘制线段(设置线宽为5px): 点(50, 150), 点(200, 150)
BasicStroke bs1 = new BasicStroke(5);
g2d.setStroke(bs1);
g2d.drawLine(50,150,200,150);
//绘制虚线: 将虚线分为若干段( 实线段 和 空白段 都认为是一段), 实线段 和 空白段 交替绘制,
// 绘制的每一段(包括 实线段 和 空白段)的 长度 从 dash 虚线模式数组中取值(从首
// 元素开始循环取值), 下面数组即表示每段长度分别为: 5px, 10px, 5px, 10px, ...
float[] dash = new float[]{5,10};
BasicStroke bs2=new BasicStroke(1,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER,10.0f,dash,0.0f);
g2d.setStroke(bs2);
g2d.drawLine(50,200,200,200);

g2d.dispose();

  4、矩形

// 1. 绘制一个矩形: 起点(30, 20), 宽80, 高100
g2d.drawRect(30,20,80,100);

// 2. 填充一个矩形
g2d.fillRect(30,20,80,100);

// 3. 绘制一个圆角矩形: 起点(30, 150), 宽80, 高100, 圆角宽30, 圆角高30
g2d.drawRoundRect(30,150,80,100,30,30);

// 4. 绘制一个多边形(收尾相连): 点(140, 150), 点(180, 250), 点(220, 200)
int[] xpoints = new int[]{140,180,220};
int[] ypoints = new int[]{150,250,200};
int npoints = 3;
g2d.drawPolygon(xpoints,ypoints,npoints);

g2d.dispose();

  5、圆弧、扇形

// 1. 绘制一条圆弧: 椭圆的外切矩形 左上角坐标为(0, 0), 宽100, 高100,
// 弧的开始角度为0度, 需要绘制的角度数为-90度,
// 椭圆右边水平线为0度, 逆时针为正角度, 顺时针为负角度
g2d.drawArc(0,0,100,100,0,-90);
// 2. 绘制一个圆: 圆的外切矩形 左上角坐标为(120, 20), 宽高为100
g2d.drawArc(120,20,100,100,0,360);
// 3. 填充一个扇形
g2d.fillArc(80, 150, 100, 100, 90, 270);

g2d.dispose();

  6、椭圆

// 1. 绘制一个圆: 圆的外切矩形 左上角坐标为(0, 0), 宽高为100
g2d.drawOval(0,0,100,100);
// 2. 填充一个椭圆
g2d.fillOval(120, 100, 100, 150);
g2d.dispose();

  7、图片

String filepath = "C:/Users/LENOVO/Desktop/demo.jpg";
Image image = Toolkit.getDefaultToolkit().getImage(filepath);
g2d.drawImage(image,50,50,image.getWidth(this),image.getHeight(this),this);

g2d.dispose();

  8、文本

// 设置字体样式, null 表示使用默认字体, Font.PLAIN 为普通样式, 大小为 25px
g2d.setFont(new Font(null,Font.PLAIN,25));

// 绘制文本, 其中坐标参数指的是文本绘制后的 左下角 的位置
// 首次绘制需要初始化字体, 可能需要较耗时
g2d.drawString("Hello world!",20,60);
g2d.drawString("你好,世界!",20,120);

 

  9、绘制太极图形

private void paintTAiJi(Graphics g){
mf.setTitle("太极");
Graphics2D g2d = (Graphics2D) g.create();

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

Shape lefthalfCircle = new Ellipse2D.Double(10,10,300,300);
Shape righthalfCircle = new Ellipse2D.Double(10,10,300,300);
Shape innerCircle1 = new Ellipse2D.Double(85,10,150,150);
Shape innerCircle2 = new Ellipse2D.Double(85,160,150,150);

Shape rectangel1 = new Rectangle2D.Double(160,10,150,300);
Shape rectangle2 = new Rectangle2D.Double(10,10,150,300);

Area left = new Area(lefthalfCircle);
Area right = new Area(righthalfCircle);

Area area1 = new Area(rectangel1);
Area area2 = new Area(rectangle2);
//左半圆白色,右半圆黑色
left.subtract(area1);
right.subtract(area2);

Area inner1 = new Area(innerCircle1);
Area inner2 = new Area(innerCircle2);

left.add(inner1);
right.add(inner2);

right.subtract(inner1);
//小白圆
Shape minorWhiteCircle = new Ellipse2D.Double(150,70,20,20);
//小黑圆
Shape minorBalckCircle = new Ellipse2D.Double(150,230,20,20);

//
g2d.setPaint(Color.WHITE);
g2d.fill(left);
g2d.setPaint(Color.black);
g2d.fill(right);

g2d.fill(minorWhiteCircle);
g2d.setPaint(Color.WHITE);
g2d.fill(minorBalckCircle);


}

10、单一颜色填充

private void singleColor(Graphics g){
mf.setTitle("8.画笔颜色设置");
Graphics2D g2d = (Graphics2D) g.create();

g2d.setPaint(Color.blue);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}

11、设置渐变色

private void setGradient(Graphics g){
mf.setTitle("9.渐变色填充");
Graphics2D g2d = (Graphics2D) g.create();

Color secondColor = new Color(99,153,255);
//水平方向填充
//GradientPaint hLinePaint = new GradientPaint(0,0,Color.BLACK,this.getWidth(),0,secondColor);
//竖直方向填充
GradientPaint hLinePaint = new GradientPaint(0,0,Color.BLACK,0,this.getHeight(),secondColor);
g2d.setPaint(hLinePaint);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());

}

12、圆周径向渐变颜色填充

private void radialGradientPaint(Graphics g){
mf.setTitle("10.圆周径向渐变颜色填充");
Graphics2D g2d = (Graphics2D) g.create();

float cx = this.getWidth()/2;
float cy = this.getHeight()/2;//(cx,cy为圆心坐标)
float radius = Math.min(cx,cy);//圆半径
float[] fractions = new float[]{0.1f,0.5f,1.0f};//色彩渐变关键帧位置
Color[] colors = new Color[]{Color.red,Color.GREEN,Color.black};//颜色数组
RadialGradientPaint rgp = new RadialGradientPaint(cx,cy,radius,fractions,colors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
g2d.setPaint(rgp);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}

 

 

 



posted @ 2018-01-29 21:20  爽朗的sunmeng  阅读(1121)  评论(0编辑  收藏  举报