Java 2D备忘

<!--[if !supportLists]-->1.   <!--[endif]-->渲染过程

<!--[if !vml]--><!--[endif]-->

Graphics Primitives:图形,文字,图片(原材料)。

Rendering Engine:对原材料进行变化,组合,渲染,剪切等。

Output Devices:输出变换后的产品。

 

 

<!--[if !supportLists]-->2.   <!--[endif]-->GeometryGraphics Primitives

<!--[if !supportLists]-->a)        <!--[endif]-->Point2D

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:Point2D,用于描述图像中的点。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->类图:

<!--[if !vml]--><!--[endif]-->

 

 

<!--[if !supportLists]-->b)        <!--[endif]-->Shape & PathIterator

<!--[if !supportLists]-->                      i.              <!--[endif]-->Shape

<!--[if !supportLists]-->1.        <!--[endif]-->说明:对于Graphics2D,她只知道如何去绘制一个Shape,所以,在Java2D当中,所有的图形都从Shape继承二来。我们可以通过draw()来绘制一个Shape的边框,通过fill()来绘制一个Shape的内部图形。

 

 

 

 

<!--[if !supportLists]-->2.        <!--[endif]-->类图:

<!--[if !vml]--><!--[endif]-->

<!--[if !supportLists]-->3.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D.Float r2 = new Rectangle2D.Float(10, 10, 100, 50);

     g2.setPaint(Color.RED);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->PathIterator

<!--[if !supportLists]-->1.        <!--[endif]-->说明:对于每一个Shape,他的边框都能够被分解为一些线的集合,这个集合在Java2D里面就是使用PathIterator来表示的,集合中的线包括了直线,弧线等。因此PathIterator定义了用于判断线段类型的常量SEG_MOVETO 等。PathIterator与我们平时所用的Iterator在操作上有相似但不完全等同。

 

 

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public static void main(String[] args) {

     Ellipse2D.Float e2 = new Ellipse2D.Float(0, 0, 100, 50);

     PathIterator pi = e2.getPathIterator(null);

     while (pi.isDone() == false) {

         double[] co = new double[6];

         int type = pi.currentSegment(co);

         switch (type) {

         case PathIterator.SEG_MOVETO:

         System.out.println("move to " + co[0] + ", " + co[1]);

              break;

         case PathIterator.SEG_LINETO:

              System.out.println("line to " + co[0] + ", " + co[1]);

              break;

         case PathIterator.SEG_QUADTO:

              System.out.println("quadratic to " + co[0] + ", " + co[1]

                       + ", " + co[2] + ", " + co[3]);

              break;

         case PathIterator.SEG_CUBICTO:

              System.out.println("cubic to " + co[0] + ", " + co[1] + ", "

                       + co[2] + ", " + co[3] + ", " + co[4] + ", " + co[5]);

              break;

         case PathIterator.SEG_CLOSE:

              System.out.println("close");

              break;

         }

          pi.next();

     }

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

move to 100.0, 25.0

cubic to 100.0, 38.80711874576983, 77.61423749153965, 50.0, 50.0, 50.0

cubic to 22.38576250846033, 50.0, 0.0, 38.80711874576983, 0.0, 25.0

cubic to 0.0, 11.192881254230166, 22.38576250846033, 0.0, 50.0, 0.0

cubic to 77.61423749153965, 0.0, 100.0, 11.192881254230166, 100.0, 25.0

close

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->GeneralPath

<!--[if !supportLists]-->1.        <!--[endif]-->说明:我们可以使用GeneralPath提供的方法,用手工的方式来指定一个Shape的边框。

 

 

 

 

 

 

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     GeneralPath gp = new GeneralPath();

     gp.moveTo(10, 10);

     gp.lineTo(80, 40);

     gp.quadTo(160, 100, 100, 60);

     gp.curveTo(200, 10, 20, 30, 70, 20);

     g2.setPaint(Color.RED);

     g2.draw(gp);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->c)        <!--[endif]-->图形库:

<!--[if !supportLists]-->                      i.              <!--[endif]-->Line2D

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于表示直线。

<!--[if !supportLists]-->2.        <!--[endif]-->类图:

<!--[if !vml]-->                  

<!--[if !supportLists]-->3.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Color[] mColors = { Color.red, Color.green, Color.blue };

     Graphics2D g2 = (Graphics2D) g;

     for (int i = 0; i < 25; i++) {

         double ratio = (double) i / (double) 25;

         Line2D line = new Line2D.Double(0, ratio * 200, ratio * 200, 200);

         g2.setPaint(mColors[i % mColors.length]);

         g2.draw(line);

     }

}

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->QuadCurve2D

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于表示二次曲线。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {     

     Graphics2D g2 = (Graphics2D) g;

     QuadCurve2D.Float q2 = new QuadCurve2D.Float(0, 0, 100, 200, 200, 0);

     g2.setPaint(Color.RED);

     g2.draw(q2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->CubicCurve2D

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于表示三次曲线。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     CubicCurve2D.Float c2 = new CubicCurve2D.Float(0, 50, 50, 100, 150, 0,

                   200, 50);

     g2.setPaint(Color.RED);

     g2.draw(c2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   iv.              <!--[endif]-->Rectangle2D

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于表示矩形。我们可以使用intersectsLine()outcode()来判断一条线或一个点与矩形之间的相对关系。

<!--[if !supportLists]-->2.        <!--[endif]-->类图:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->3.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D.Float r2 = new Rectangle2D.Float(0, 0, 100, 50);

     RoundRectangle2D.Float rr2 = new RoundRectangle2D.Float(150, 0, 100,

                   50, 5, 5);

     g2.setPaint(Color.RED);

     g2.draw(r2);

     g2.setPaint(Color.BLUE);

     g2.draw(rr2);

}

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->                      v.              <!--[endif]-->Ellipse2D

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于表示椭圆型。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Ellipse2D e2 = new Ellipse2D.Float(0, 0, 100, 50);

     g2.setPaint(Color.RED);

     g2.draw(e2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   vi.              <!--[endif]-->Arc2D

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于表示弧形或弓形。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Arc2D a21 = new Arc2D.Float(0, 0, 50, 50, -45, 90, Arc2D.OPEN);

     Arc2D a22 = new Arc2D.Float(100, 0, 50, 50, -45, 90, Arc2D.PIE);

Arc2D a23 = new Arc2D.Float(200, 0, 50, 50, -45, 90, Arc2D.CHORD);

     g2.setPaint(Color.RED);

     g2.draw(a21);

     g2.setPaint(Color.GREEN);

     g2.draw(a22);

     g2.setPaint(Color.BLUE);

     g2.draw(a23);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->d)        <!--[endif]-->Shapes Combining

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:Java2D提供了一个在图形之间进行求和,求差,异或等操作。这些操作都通过Area来提供,我们可以通过Area的构造函数Area(Shape)把一个Shape封装成一个Area,然后使用Area中已经实现好的这些操作。而由于Area也实现了Shape,所以他能被直接输出。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->效果图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->3.   <!--[endif]-->Painting & Stroking

<!--[if !supportLists]-->a)        <!--[endif]-->说明:Painting是指对Shape的内部进行操作,例如着色,渐变,纹理等。Stroking是指对Shape的边框进行操作,例如边框的宽度,风格,颜色等。

 

 

<!--[if !supportLists]-->b)        <!--[endif]-->Painting

<!--[if !supportLists]-->                      i.              <!--[endif]-->单色填充:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:Painting支持单色填充。由于Color扩展了Paint,所以他可以作为setPaint()的参数。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(50, 50, 100, 50);

     g2.setPaint(Color.RED);

g2.fill(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->渐变填充:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:Painting支持渐变填充。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r21 = new Rectangle2D.Float(50, 50, 100, 50);

GradientPaint gp1 = new GradientPaint(80, 80, Color.RED, 120, 70,

              Color.BLUE, false);

     Rectangle2D r22 = new Rectangle2D.Float(200, 50, 100, 50);

     GradientPaint gp2 = new GradientPaint(230, 80, Color.RED, 270, 70,

              Color.BLUE, true);

     g2.setPaint(gp1);

     g2.fill(r21);

     g2.setPaint(gp2);

     g2.fill(r22);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->纹理填充:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:Painting支持纹理填充,所谓纹理填充就是使用某一幅图片作为背景,把图像内部填充满。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     BufferedImage image = null;

     try {

         image = (BufferedImage) ImageIO.read(new File("libra.jpg"));

     } catch (Exception e) {}

     Rectangle2D r21 = new Rectangle2D.Float(50, 50, 100, 50);

     TexturePaint tp1 = new TexturePaint(image, new Rectangle2D.Float(0, 0,

                   10, 10));

     Rectangle2D r22 = new Rectangle2D.Float(200, 50, 100, 50);

     TexturePaint tp2 = new TexturePaint(image, new Rectangle2D.Float(0, 0,

                   20, 20));

     g2.setPaint(tp1);

     g2.fill(r21);

     g2.setPaint(tp2);

     g2.fill(r22);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->c)        <!--[endif]-->Stroking

<!--[if !supportLists]-->                      i.              <!--[endif]-->BasicStroke

<!--[if !supportLists]-->1.        <!--[endif]-->说明:这是Java2D提供的对于Stroke的一个基本实现,通过这个类,我们可以设置边框的宽度,风格等。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(10, 10, 100, 50);

     BasicStroke bs = new BasicStroke(5, BasicStroke.CAP_SQUARE,

              BasicStroke.JOIN_ROUND, 0, new float[] { 4, 4, 8 }, 0);

     g2.setStroke(bs);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->补充:

<!--[if !supportLists]-->a)        <!--[endif]-->END_STYLE

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->b)        <!--[endif]-->JOIN_STYLE

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->c)        <!--[endif]-->dash array

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->d)        <!--[endif]-->dash phase

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->Advance Stroke

<!--[if !supportLists]-->1.        <!--[endif]-->说明:当我们使用draw()而不是fill()时,则我们上述对Painting的设定将会应用在边框的绘制上。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     GradientPaint gp = new GradientPaint(0, 0, Color.RED, 100, 50,

              Color.BLUE, true);

     Rectangle2D r2 = new Rectangle2D.Float(0, 0, 100, 50);

     BasicStroke bs = new BasicStroke(5);

     g2.setStroke(bs);

     g2.setPaint(gp);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->4.   <!--[endif]-->Rendering

<!--[if !supportLists]-->a)        <!--[endif]-->Transforming

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:Java2D提供了对图像进行平移,旋转等功能。

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->Translation

<!--[if !supportLists]-->1.        <!--[endif]-->说明:对图像进行平移。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(0, 0, 50, 50);

     g2.setPaint(Color.RED);

     g2.draw(r2);

     g2.translate(60, 80);

     g2.setPaint(Color.BLUE);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]--> Rotation

<!--[if !supportLists]-->1.        <!--[endif]-->说明:对图形进行旋转。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(100, 100, 80, 50);

     g2.setPaint(Color.RED);

     g2.draw(r2);

AffineTransform at = AffineTransform.getRotateInstance(-1.57, 100,

100);

     g2.setTransform(at);

     g2.setPaint(Color.BLUE);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   iv.              <!--[endif]-->Scaling

<!--[if !supportLists]-->1.        <!--[endif]-->说明:对图形进行拉伸。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(10, 10, 80, 50);

     g2.setPaint(Color.RED);

     g2.draw(r2);

     g2.scale(1.5, 1.5);

     g2.setPaint(Color.BLUE);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                      v.              <!--[endif]-->Shearing

<!--[if !supportLists]-->1.        <!--[endif]-->说明:对图形进行错切。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(10, 10, 80, 50);

     g2.setPaint(Color.RED);

     g2.draw(r2);

     g2.shear(0.5, 0);

     g2.setPaint(Color.BLUE);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   vi.              <!--[endif]-->Compound Transformations

<!--[if !supportLists]-->1.        <!--[endif]-->说明:我们可以把多个变换进行组合,实现一种符合的变换,注意有些变换的顺序是不能交互的。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(10, 10, 80, 50);

     g2.setPaint(Color.RED);

     g2.draw(r2);

     g2.translate(50, 50);

     g2.scale(2, 1);

     g2.setPaint(Color.BLUE);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->b)        <!--[endif]-->Compositing

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:用于处理当两个图形发生叠加时,该如何处理。

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->AlphaCompositing

<!--[if !supportLists]-->1.        <!--[endif]-->说明:AlphaCompositing提供了用于进行图形合成时的一些策略。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r21 = new Rectangle2D.Float(10, 10, 80, 50);

     Rectangle2D r22 = new Rectangle2D.Float(10, 10, 50, 80);

     g2.setPaint(Color.RED);

     g2.fill(r21);

     Composite com = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,

                   0.8f);

     g2.setComposite(com);

     g2.setPaint(Color.BLUE);

     g2.fill(r22);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->补充:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->XOR

<!--[if !supportLists]-->1.        <!--[endif]-->说明:Java2D提供了XOR的绘图方法。Cd = Cs1 Cx Cs2

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     g2.setXORMode(Color.WHITE);

     Rectangle2D r21 = new Rectangle2D.Float(10, 10, 80, 50);

     Rectangle2D r22 = new Rectangle2D.Float(10, 10, 50, 80);

     g2.setPaint(Color.RED);

     g2.fill(r21);     

     g2.setPaint(Color.BLUE);

     g2.fill(r22);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->c)        <!--[endif]-->Clipping

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:Java2D提供了以任何的Shape来对图形进行剪切。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Rectangle2D r2 = new Rectangle2D.Float(10, 10, 80, 50);

     Ellipse2D e2 = new Ellipse2D.Float(20, 10, 30, 40);

     g2.setPaint(Color.RED);

     g2.setClip(e2);

     g2.fill(r2);

}

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->d)        <!--[endif]-->Rendering Hints

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:Rendering Hints提供了用于进行渲染时的一些选项。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     GeneralPath gp = new GeneralPath();

     gp.moveTo(0, 0);

     for (int i = 1; i < 20; i++) {

         gp.lineTo(i * 20, i % 2 * 50);

     }

     g2.setPaint(Color.RED);

     g2.draw(gp);

     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

         RenderingHints.VALUE_ANTIALIAS_ON);

     g2.translate(0, 80);

     g2.setPaint(Color.BLUE);

     g2.draw(gp);

}

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->                   iv.              <!--[endif]-->补充:

Key

Possible Values

KEY_ANTIALIASING

VALUE_ANTIALIAS_ON | VALUE_ANTIALIAS_OFF | VALUE_ANTIALIAS_DEFAULT

KEY_RENDERING

VALUE_RENDER_QUALITY | VALUE_RENDER_SPEED | VALUE_RENDER_DEFAULT

KEY_DITHERING

VALUE_DITHER_DISABLE | VALUE_DITHER_ENABLE | VALUE_DITHER_DEFAULT

KEY_COLOR_RENDERING

VALUE_COLOR_RENDER_QUALITY | VALUE_COLOR_RENDER_SPEED | VALUE_COLOR_RENDER_DEFAULT

KEY_FRACTIONALMETRICS

VALUE_FRACTIONALMETRICS_ON | VALUE_FRACTIONALMETRICS_OFF |

VALUE_FRACTIONALMETRICS_DEFAULT

KEY_TEXT_ANTIALIASING

VALUE_TEXT_ANTIALIAS_ON | VALUE_TEXT_ANTIALIAS_OFF |

VALUE_TEXT_ANTIALIAS_DEFAULT

KEY_INTERPOLATION

VALUE_INTERPOLATION_BICUBIC | VALUE_INTERPOLATION_BILINEAR |

VALUE_INTERPOLATION_NEAREST_NEIGHBOR

KEY_ALPHA_INTERPOLATION

VALUE_ALPHA_INTERPOLATION_QUALITY | VALUE_ALPHA_INTERPOLATION_SPEED

VALUE_ALPHA_INTERPOLATION_DEFAULT

 

 

<!--[if !supportLists]-->5.   <!--[endif]--> Text

<!--[if !supportLists]-->a)        <!--[endif]-->说明:我们有四种对文本进行渲染的方式,从简单到复杂是:使用Swing控件;使用Graphics2D提供的drawString()函数;使用TextLayout;使用Glyph Shape

<!--[if !supportLists]-->b)        <!--[endif]--> Drawing Text

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:通过Attribute String来定制字体的绘制。

 

 

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->补充: TextAttribute的键与值。

Key

Possible Values

BACKGROUND(文本背景)

a Paint

CHAR_REPLACEMENT(文本替换)

a GraphicAttribute

FAMILY(字体相关)

a String containing a font family name

FONT(字体相关)

a Font

FOREGROUND(文本前景)

a Paint

JUSTIFICATION(文本拉伸)

a Float from 0.0 to 1.0 | JUSTIFICATION_FULL | JUSTIFICATION_NONE

POSTURE(字体风格)

a Float | POSTURE_OBLIQUE | POSTURE_REGULAR

SIZE(字体相关)

a Float

STRIKETHROUGH(删除线)

a Boolean | STRIKETHROUGH_ON

SUPERSCRIPT(字体风格)

an Integer | SUPERSCRIPT_SUB | SUPERSCRIPT_SUPER

SWAP_COLORS(交换前,后景)

a Boolean | SWAP_COLORS_ON

TRANSFORM(文本变换)

a TransformAttribute

UNDERLINE(下划线)

an Integer | UNDERLINE_ON

WEIGHT(字体风格)

a Float | WEIGHT_EXTRA_LIGHT | WEIGHT_LIGHT | WEIGHT_DEMILIGHT |

WEIGHT_REGULAR | WEIGHT_SEMIBOLD | WEIGHT_MEDIUM | WEIGHT_DEMIBOLD | WEIGHT_BOLD | WEIGHT_HEAVY | WEIGHT_EXTRABOLD | WEIGHT_ULTRABOLD

WIDTH(字体风格)

a Float | WIDTH_CONDENSED | WIDTH_SEMI_CONDENSED | WIDTH_REGULAR | WIDTH_SEMI_EXTENDED | WIDTH_EXTENDED |

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->文本替换:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:通过TextAttributeCHAR_PRELACEMENT实现文本替换。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

          Graphics2D g2 = (Graphics2D) g;

          AttributedString as1 = new AttributedString("Hello World 1");

          as1.addAttribute(TextAttribute.FOREGROUND, Color.RED, 1, 4);

          as1.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 7, 10);

          g2.drawString(as1.getIterator(), 10, 10);

 

 

          AttributedString as2 = new AttributedString("Hello World 2");

          Image libra = null;

          try {

              libra = ImageIO.read(new File("libram.jpg"));

          } catch (Exception e) { }

          ImageGraphicAttribute iga = new ImageGraphicAttribute(libra,

                        GraphicAttribute.ROMAN_BASELINE);

          as2.addAttribute(TextAttribute.CHAR_REPLACEMENT, iga, 2, 3);

          g2.drawString(as2.getIterator(), 10, 40);

 

 

          AttributedString as3 = new AttributedString("Hello World 3");

          Ellipse2D e2 = new Ellipse2D.Float(0, 0, 50, 25);

          ShapeGraphicAttribute sga = new ShapeGraphicAttribute(e2,

               GraphicAttribute.ROMAN_BASELINE, ShapeGraphicAttribute.STROKE);

          as3.addAttribute(TextAttribute.CHAR_REPLACEMENT, sga, 4, 5);

          g2.drawString(as3.getIterator(), 10, 80);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   iv.              <!--[endif]-->文本变换

<!--[if !supportLists]-->1.        <!--[endif]-->说明:Rendering中的所有Transforming技术都可以在文本当中使用。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

          Graphics2D g2 = (Graphics2D) g;

          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

                   RenderingHints.VALUE_ANTIALIAS_ON);

          AttributedString as = new AttributedString("jade");

          int limit = 6;

          AffineTransform at = null;

          for (int i = 1; i <= limit; i++) {

              float ratio = (float) i / (float) limit;

              at = AffineTransform.getTranslateInstance(150, 150);

              at.rotate(Math.PI * (ratio - 1));

              float alpha = ((i == limit) ? 1.0f : ratio / 3);

              as.addAttribute(TextAttribute.TRANSFORM, at);

              as.addAttribute(TextAttribute.SIZE, new Float(64));

              g2.setComposite(AlphaComposite.getInstance(

AlphaComposite.SRC_OVER,alpha));

              g2.drawString(as.getIterator(), 0, 0);

          }

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                      v.              <!--[endif]-->下划线与删除线:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于设置文本的下划线与删除线。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     AttributedString as = new AttributedString("Hello World");

as.addAttribute(TextAttribute.UNDERLINE,

TextAttribute.UNDERLINE_ON, 0,5);

     as.addAttribute(TextAttribute.STRIKETHROUGH,

                   TextAttribute.STRIKETHROUGH_ON, 6, 11);

     as.addAttribute(TextAttribute.SIZE, 64.0f);

     g2.drawString(as.getIterator(), 10, 60);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->c)        <!--[endif]-->Fonts

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:Fonts有三个Name,一个是family name;一个是font name,唯一;一个是logical name,由操作系统定义,使用的时候将映射为实际的font

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->显示所有系统字体:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:通过GraphicsEnviroment可以获取系统中所有已注册的字体。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g){

     Graphics2D g2 = (Graphics2D)g;

     Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().

getAllFonts();

     for(Font f:fonts){

         System.out.printf("Family Name:%s./t/t/t

Font Name:%s/n",f.getFamily(),f.getFontName());

     }

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

Family Name:Arial.          Font Name:Arial

Family Name:Arial Black.             Font Name:Arial Black

Family Name:Arial.          Font Name:Arial Bold

Family Name:幼圆.           Font Name:幼圆

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->字体变换:

<!--[if !supportLists]-->1.        <!--[endif]-->说明:我们可以通过AffineTransform来对字体进行变换。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Font fo = new Font("新宋体", Font.PLAIN, 12);

     g2.setFont(fo);

     g2.drawString("Hello World", 40, 10);

     AffineTransform at = AffineTransform.getRotateInstance(3.14 / 6);

     Font fd = fo.deriveFont(at);

     g2.setFont(fd);

     g2.drawString("Hello World", 40, 40);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->                   iv.              <!--[endif]-->Font Render Context

<!--[if !supportLists]-->1.        <!--[endif]-->说明:当我们需要获取与字符串渲染有关的信息时,我们都需要有一个FontRenderContext实例。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     int middle = 50;

     FontRenderContext frc = g2.getFontRenderContext();

     Rectangle2D r2 = g2.getFont().getStringBounds("Hello World", frc);

     g2.drawString("Hello World", (float) (middle - r2.getWidth() / 2), 20);

     r2 = g2.getFont().getStringBounds("Nice To Meet You", frc);

     g2.drawString("Nice To Meet You", (float) (middle - r2.getWidth() / 2),

                   40);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                      v.              <!--[endif]-->Line Metrics

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于获取与文本相关的信息。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Font font = new Font("新宋体", Font.PLAIN, 64);

     g2.setFont(font);

     FontRenderContext frc = g2.getFontRenderContext();

     Rectangle2D r2 = g2.getFont().getStringBounds("Google", frc);

     int width = (int) r2.getWidth();

     int x = 20;

     int y = 80;

     LineMetrics lm = g2.getFont().getLineMetrics("Google", frc);

     g2.drawString("Google", x, y);

         Line2D baseLine = new Line2D.Float(x, y, x + width, y);

     Line2D ascentLine = new Line2D.Float(x, y - lm.getAscent(), x + width,

              y - lm.getAscent());

     Line2D descentLine = new Line2D.Float(x, y + lm.getDescent(),

              x + width, y + lm.getDescent());

     Line2D leadingLine = new Line2D.Float(x, y + lm.getDescent()

              + lm.getDescent(), x + width, y + lm.getDescent()

              + lm.getDescent());

     g2.setPaint(Color.GRAY);

     g2.draw(baseLine);

     g2.setPaint(Color.RED);

     g2.draw(ascentLine);

     g2.setPaint(Color.GREEN);

     g2.draw(descentLine);

     g2.setPaint(Color.BLUE);

     g2.draw(leadingLine);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->补充:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->d)        <!--[endif]-->TextLayout

<!--[if !supportLists]-->                      i.              <!--[endif]-->Base

<!--[if !supportLists]-->1.        <!--[endif]-->说明:使用TextLayout对文本进行渲染。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     FontRenderContext frc = g2.getFontRenderContext();

     TextLayout tl = new TextLayout("Hello World", g2.getFont(), frc);

     tl.draw(g2, 10, 20);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]--> Text Metrics

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于获取与文本相关的信息。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     Font font = new Font("新宋体", Font.PLAIN, 64);

     g2.setFont(font);

     FontRenderContext frc = g2.getFontRenderContext();

     TextLayout tl = new TextLayout("Google", g2.getFont(), frc);

     int width = (int) tl.getBounds().getWidth();

     int x = 20;

     int y = 80;

     tl.draw(g2, x, y);

     Line2D baseLine = new Line2D.Float(x, y, x + width, y);

     Line2D ascentLine = new Line2D.Float(x, y - tl.getAscent(), x + width,

              y - tl.getAscent());

     Line2D descentLine = new Line2D.Float(x, y + tl.getDescent(),

              x + width, y + tl.getDescent());

     Line2D leadingLine = new Line2D.Float(x, y + tl.getDescent()

              + tl.getDescent(), x + width, y + tl.getDescent()

              + tl.getDescent());

     g2.setPaint(Color.GRAY);

     g2.draw(baseLine);

     g2.setPaint(Color.RED);

     g2.draw(ascentLine);

     g2.setPaint(Color.GREEN);

     g2.draw(descentLine);

     g2.setPaint(Color.BLUE);

     g2.draw(leadingLine);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->Hit Testing

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于判断字符串中的哪个字符被鼠标点击了。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     g2.setFont(new Font("新宋体", Font.PLAIN, 24));

     FontRenderContext frc = g2.getFontRenderContext();

     final TextLayout tl = new TextLayout("Hello World", g2.getFont(), frc);

         tl.draw(g2, 10, 20);

     addMouseListener(new MouseAdapter() {

         public void mouseClicked(MouseEvent e) {

              TextHitInfo thi = tl.hitTestChar(e.getX(), e.getY());

              System.out.println("Hit " + thi.getCharIndex());

         }

     });

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

当点击在W上时的输出。

Hit 7

 

 

<!--[if !supportLists]-->                   iv.              <!--[endif]-->Caret Support

<!--[if !supportLists]-->1.        <!--[endif]-->说明:Caret就是我们平时所说的光标,他所在的位置,也就是我们下一个输入字符出现的位置,TextLayout提供了对Caret进行操作的方法。

 

 

<!--[if !supportLists]-->                      v.              <!--[endif]-->Highlighting Support

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于将文本中的部分进行高亮。当我们希望通过使用点击来高亮其中某一部分内容时,可以通过Hiting Test中得到的TextHitInfo来调用TextLayout的另一个getVisualHighlightShape()方法。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

     g2.setFont(new Font("新宋体", Font.PLAIN, 24));

     FontRenderContext frc = g2.getFontRenderContext();

     TextLayout tl = new TextLayout("Hello World", g2.getFont(), frc);

     Shape s = tl.getLogicalHighlightShape(1, 5);

     Shape ts = AffineTransform.getTranslateInstance(10, 20)

              .createTransformedShape(s);

     g2.setPaint(Color.GREEN);

     g2.fill(ts);

     g2.setPaint(Color.BLACK);

     tl.draw(g2, 10, 20);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->                   vi.              <!--[endif]--> Paragraph Layout on Multiple Lines

<!--[if !supportLists]-->1.        <!--[endif]-->说明:用于对一段长文本进行分行排版。需要类LineBreakMeasure的支持。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     String content = "This method is the same as the last method, but it

adds more constraints on the line breaking algorithm.The returned

line can contain only characters up to the supplied offsetLimit,

which is a logical character offset.";

     AttributedString as = new AttributedString(content);

     as.addAttribute(TextAttribute.SIZE, 24f);

     AttributedCharacterIterator iter = as.getIterator();

     FontRenderContext frc = g2.getFontRenderContext();

     LineBreakMeasurer lbm = new LineBreakMeasurer(iter, frc);

     int maxAdvance = 400;

     TextLayout tl = null;

     int x = 20;

     int y = 40;

     while (lbm.getPosition() < iter.getEndIndex()) {

         tl = lbm.nextLayout(maxAdvance);

         y += tl.getAscent();

         tl.draw(g2, x, y);

         y += tl.getDescent() + tl.getLeading();

     }

Rectangle2D r2 = new Rectangle2D.Float(20, 40, 400, y - 40);

g2.setPaint(Color.RED);

     g2.draw(r2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->e)        <!--[endif]-->Glyph Shape

<!--[if !supportLists]-->                      i.              <!--[endif]-->Base

<!--[if !supportLists]-->1.        <!--[endif]-->说明:使用Glyph Shape来输出文本。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     g2.setFont(new Font("新宋体", Font.PLAIN, 36));

     String content = "Hello World";

     FontRenderContext frc = g2.getFontRenderContext();

     GlyphVector gv = g2.getFont().createGlyphVector(frc, content);

     g2.drawGlyphVector(gv, 10, 30);

     Ellipse2D e2 = new Ellipse2D.Float(10, 50, 200, 50);

     g2.setPaint(Color.RED);

     g2.clip(gv.getOutline(10, 80));

     g2.fill(e2);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   ii.              <!--[endif]-->Retrieving Glyph Shape

<!--[if !supportLists]-->1.        <!--[endif]-->说明:通过GlyphVector,我们可以获取当中每个字符的Glyph Shape,然后进行进一步的操作。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

              RenderingHints.VALUE_ANTIALIAS_ON);

     String s = "G o o g l e";

     Font font = new Font("新宋体", Font.PLAIN, 24);

     FontRenderContext frc = g2.getFontRenderContext();

     g2.translate(20, 40);

     GlyphVector gv = font.createGlyphVector(frc, s);

     int length = gv.getNumGlyphs();

     for (int i = 0; i < length; i++) {

         Point2D p2 = gv.getGlyphPosition(i);

         double theta = i * Math.PI / 4;

         Shape glyph = gv.getGlyphOutline(i);

          Rectangle2D r = glyph.getBounds();

         AffineTransform at = AffineTransform.getRotateInstance(theta, r

                       .getCenterX(), r.getCenterY());

         Shape transformedGlyph = at.createTransformedShape(glyph);

         g2.fill(transformedGlyph);

     }

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->Modifying Glyph Vector

<!--[if !supportLists]-->1.        <!--[endif]-->说明:我们可以利用GlyphVector上的方法,来对他内部的GlyphShape进行修改。

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

                   RenderingHints.VALUE_ANTIALIAS_ON);

     String s = "G o o g l e";

     Font font = new Font("新宋体", Font.PLAIN, 24);

     FontRenderContext frc = g2.getFontRenderContext();

     GlyphVector gv = font.createGlyphVector(frc, s);

     int length = gv.getNumGlyphs();

     for (int i = 0; i < length; i++) {

         double theta = i * Math.PI / 6;

         AffineTransform at = AffineTransform.getRotateInstance(theta);

         gv.setGlyphTransform(i, at);

     }

     g2.drawGlyphVector(gv, 30, 40);

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->                   iv.              <!--[endif]-->Glyph Metrics

<!--[if !supportLists]-->1.        <!--[endif]-->说明:

<!--[if !supportLists]-->2.        <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     g2.setFont(new Font("新宋体", Font.PLAIN, 36));

     String content = "Google";

     FontRenderContext frc = g2.getFontRenderContext();

     GlyphVector gv = g2.getFont().createGlyphVector(frc, content);

     GlyphMetrics gm = null;

     for (int i = 0; i < gv.getNumGlyphs(); i++) {

         gm = gv.getGlyphMetrics(i);

         System.out.printf("advance:% 4.2f lsb:% 4.2f rsb:% 4.2f/n", gm

                   .getAdvance(), gm.getLSB(), gm.getRSB());

     }

}

 

 

<!--[if !supportLists]-->3.        <!--[endif]-->运行图:

advance:18.00 lsb:0.98 rsb:0.70

advance:18.00 lsb:2.25 rsb:2.11

 

 

<!--[if !supportLists]-->4.        <!--[endif]-->补充:

<!--[if !vml]--><!--[endif]-->  <!--[if !vml]--><!--[endif]-->

 

 

<!--[if !supportLists]-->6.   <!--[endif]-->Image

<!--[if !supportLists]-->a)        <!--[endif]-->base

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:我们可以使用系统提供的BufferedImageOp的子类来实现对图片的处理。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->流程:

<!--[if !vml]-->
<!--[endif]-->

<!--[if !supportLists]-->                iii.              <!--[endif]-->补充:

Class Name

Supporting Classes

Effect

ConvolveOp

Kernel

模糊,锐化,边界检查

AffineTransformOp

AffineTransform

几何变换

LookupOp

LookupTable, ByteLookupTable,ShortLookupTable

分色,反转

RescaleOp

 

 

增亮,变暗

ColorConvertOp

ColorSpace

变换ColorSpace

 

 

<!--[if !supportLists]-->b)        <!--[endif]-->ConvolveOp

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:通过提供不同的矩阵(Kernel),实现图形的模糊,锐化,边界检查等。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     BufferedImage image = null;

     try {

         FileInputStream fis = new FileInputStream("libra.jpg");

         JPEGImageDecoder docoder = JPEGCodec.createJPEGDecoder(fis);

         image = docoder.decodeAsBufferedImage();

     } catch (Exception e) {

         e.printStackTrace();

     }

     float[] blurs = new float[] { 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f,

         1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f };

ConvolveOp co = new ConvolveOp(new Kernel(3, 3, blurs));

     BufferedImage dest = co.filter(image, null);

     int width = image.getWidth();

     g2.drawImage(image, null, 10, 10);

     g2.drawImage(dest, null, 10 + width + 10, 10);

}

注:这里没有使用ImageIO进行图片的读取,因为通过ImageIO返回的BufferedImage不能进行ConvolveOp操作。另外blurs数组中的值必须是浮点值,否则也会出错。

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->c)        <!--[endif]-->AffineTransformOp

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:对图片进行几何变换。由于是对象素点进行变换,所以与之前的在Graphics2D上做的几何变换不等价。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     BufferedImage image = null;

     try {

         image = ImageIO.read(new File("libra.jpg"));

     } catch (Exception e) {

         e.printStackTrace();

     }

     AffineTransform at = AffineTransform.getRotateInstance(Math.PI / 6);

     AffineTransformOp ao = new AffineTransformOp(at, null);

     BufferedImage dest = ao.filter(image, null);

int width = image.getWidth();

     g2.drawImage(image, null, 10, 10);

     g2.drawImage(dest, null, 10 + width + 10, 10);

}

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->d)        <!--[endif]-->LookupOp

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:LookupOp通过LookupTable来进行颜色的映射。LookupTable相当于一个颜色的映射表,例如RGB(200,150,100)的象素点,她就会以200,150,100作为索引分别到LookupTable当中查找这个索引值所对应的值。所以,像下例当中,当我们把invert[i] 设置为 (short) (255 - i)时,则原来为200的值通过LookupTable后,将变成55,所以RGB(200,150,100)的值讲过查表操作后将变成(55,105,155),从而实现了颜色的反转。通过LookupTable提供的不同构造函数,我们还可以分别指定每一列所对应的short数组。

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     BufferedImage image = null;

     try {

         image = ImageIO.read(new File("libra.jpg"));

     } catch (Exception e) { }

     BufferedImage src = new BufferedImage(image.getWidth(), image

                   .getHeight(), BufferedImage.TYPE_INT_RGB);

     src.createGraphics().drawImage(image, 0, 0, null);

     short[] invert = new short[256];

     for (int i = 0; i < 256; i++) {

         invert[i] = (short) (255 - i);

     }

     LookupTable lt = new ShortLookupTable(0, invert);

     LookupOp lo = new LookupOp(lt, null);

     BufferedImage dest = lo.filter(src, null);

         int width = image.getWidth();

     g2.drawImage(src, null, 10, 10);

     g2.drawImage(dest, null, 10 + width + 10, 10);

}

注:这里中间增加了一个从imagesrc的转换,之所以要这样做是因为通过ImageIO读出来的BufferedImage使用了IndexColorModel,而这将导致LookupOp不能正常执行,所以需要进行一个额外的转换。

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->e)        <!--[endif]-->RescaleOp

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:用于增加或减少图片的光亮度。Cd = Co * scaleFactor + offset

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     BufferedImage image = null;

     try {

         image = ImageIO.read(new File("libra.jpg"));

     } catch (Exception e) {

         e.printStackTrace();

     }

     RescaleOp ro = new RescaleOp( 0.9f, 0, null);

     BufferedImage dest = ro.filter(image, null);

     int width = image.getWidth();

     g2.drawImage(image, null, 10, 10);

     g2.drawImage(dest, null, 10 + width + 10, 10);

}

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->f)        <!--[endif]--> ColorConvertOp

<!--[if !supportLists]-->                      i.              <!--[endif]-->说明:用于转换图像所使用的ColorSpace

<!--[if !supportLists]-->                   ii.              <!--[endif]-->代码:

public void paint(Graphics g) {

     Graphics2D g2 = (Graphics2D) g;

     BufferedImage image = null;

     try {

         image = ImageIO.read(new File("libra.jpg"));

     } catch (Exception e) {

         e.printStackTrace();

     }

     ColorConvertOp cco = new ColorConvertOp(ColorSpace

         .getInstance(ColorSpace.CS_GRAY), null);

     BufferedImage dest = cco.filter(image, null);

     int width = image.getWidth();

     g2.drawImage(image, null, 10, 10);

     g2.drawImage(dest, null, 10 + width + 10, 10);

}

 

 

<!--[if !supportLists]-->                iii.              <!--[endif]-->运行图:

<!--[if !vml]-->
<!--[endif]-->

 

 

<!--[if !supportLists]-->7.   <!--[endif]-->总结

Java  2D模型的核心是Shape,所有的文本,图形,图像最终都需要被转换为Shape,然后Java 2D引擎才能对他进行进一步的操作。对于一个Shape,他包含了边框和内容两步分,我们通过调用Graphics2D上的draw()或者fill()操作,就可以向Java 2D指明是操作边框还是内容。不管是边框还是内容我们都可以对他进行变换,合并,组合等操作。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值