Step By Step(Java 2D图形篇<四>) (三)

2014-11-24 03:21:34 · 作者: · 浏览: 4
7 // 9. 调整阴影的绘制坐标,使他比原始图形的BASELINE下降一点儿来模拟投影的效果。

38 g2.translate(15, 20);

39 // 10.基于原始字符图形J和变换对象创建阴影图形对象,并绘制到Graphics中

40 g2.fill(shadowTransform.createTransformedShape(jshape));

41 // 11. 调整回原始图形J的BASELINE原点坐标

42 g2.translate(-15, -20); // Undo the translation above

43 // 12. 和绘制普通图形一样绘制基于字符生成的Shape对象,既可以填充也可以只是绘制边框。

44 g2.setPaint(Color.blue);

45 g2.fill(jshape);

46 g2.setPaint(Color.black);

47 g2.draw(jshape);

48 // 13. 绘制原始字符图形A和他的阴影,处理方式和上面J的完全一致。

49 g2.translate(75, 0);

50 g2.setPaint(shadowPaint);

51 g2.fill(shadowTransform.createTransformedShape(ashape));

52 g2.setPaint(new Color(0, 255, 0, 125));

53 g2.fill(ashape);

54 g2.setPaint(Color.black);

55 g2.draw(ashape);

56 // 14. 绘制字符V的阴影。 www.2cto.com

57 g2.translate(75, 0);

58 g2.setPaint(shadowPaint);

59 g2.fill(shadowTransform.createTransformedShape(vshape));

60 // 15. 通过纹理绘制字符V的原图形。

61 BufferedImage tile = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);

62 Graphics2D tg = tile.createGraphics();

63 // 15.1 纹理的背景为pink

64 tg.setColor(Color.pink);

65 tg.fillRect(0, 0, 50, 50);

66 // 15.2 纹理上绘制渐变的椭圆

67 tg.setPaint(new GradientPaint(40, 0, Color.green, 0, 40, Color.gray));

68 tg.fillOval(5, 5, 40, 40);

69 tg.dispose();

70 g2.setPaint(new TexturePaint(tile, new Rectangle(0, 0, 50, 50)));

71 g2.fill(vshape);

72 g2.setPaint(Color.black);

73 g2.draw(vshape);

74 }

75 public static void main(String[] args) {

76 JFrame frame = new JFrame();

77 frame.setTitle("A Good Example");

78 frame.addWindowListener(new WindowAdapter() {

79 public void windowClosing(WindowEvent e) {

80 System.exit(0);

81 }

82 });

83 frame.setContentPane(new MyTest());

84 frame.setSize(WIDTH, HEIGHT);

85 frame.setVisible(true);

86 }

87 }


作者 Stephen_Liu