太极阴阳图

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一:基本原理

利用Java 2D的Area对象对绘制形状几何操作的支持,完成太极图案的绘制,使用Paint 来完成对不同颜色的填充。Java 2D图形API Area对Shape支持四种几何操作:

- Add (加)- 保留两个几何形状及其重叠部分

- Subtract (减) –从第一个几何形状上减去和第二个重叠的部分,保留减去之后的第一个几何形状

- Intersect (与) –只保留两个几何形状重叠的部分。

- ExclusiveOr(或) –保留他们相互不重叠的部分。

参看下图:

二:程序运行效果

程序基于JDK6 API完成

程序源代码如下:

[java]view plaincopy

1.package com.gloomyfish.swing;

2.

3.import java.awt.BorderLayout;

4.import java.awt.Color;

5.import java.awt.Dimension;

6.import java.awt.Graphics;

7.import java.awt.Graphics2D;

8.import java.awt.RenderingHints;

9.import java.awt.Shape;

10.import java.awt.geom.Area;

11.import java.awt.geom.Ellipse2D;

12.import java.awt.geom.Rectangle2D;

13.import java.awt.image.BufferedImage;

14.import java.io.File;

15.

16.import javax.imageio.ImageIO;

17.import javax.swing.JComponent;

18.import javax.swing.JFrame;

19.

20.public class YingYangGraphics extends JComponent {

21.

22./**

23. *

24. */

25.private static final long serialVersionUID = 8812325148970066491L;

26.private BufferedImage image = null;

27.public YingYangGraphics() {

28.super();

29.this.setOpaque(false);

30. }

31.protected void paintComponent(Graphics g) {

32. Graphics2D g2 = (Graphics2D)g;

33. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.

VALUE_ANTIALIAS_ON);

34. g2.drawImage(getImage(), 0, 0, image.getWidth(), image.getHeight(),

null);

35. Shape lefthalfCirle = new Ellipse2D.Double(10,10, 300,300); // R = 1

50

36. Shape righthalfCircle = new Ellipse2D.Double(10,10, 300,300); // R =

150

37. Shape innerCircle1 = new Ellipse2D.Double(85,10, 150,150); // R/2 =

75

38. Shape innerCircle2 = new Ellipse2D.Double(85,160, 150,150); // R = 1

50

39.

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

41. Shape rectangel2 = new Rectangle2D.Double(10, 10, 150, 300);

42.

43. Area left = new Area(lefthalfCirle);

44. Area right = new Area(righthalfCircle);

45.

46. Area area11 = new Area(rectangel1);

47. Area area22 = new Area(rectangel2);

48.

49. left.subtract(area11);

50. right.subtract(area22);

51.

52. Area inner1 = new Area(innerCircle1);

53. Area inner2 = new Area(innerCircle2);

54.

55. left.add(inner1);

56.//left.subtract(inner2);

57. right.add(inner2);

58. right.subtract(inner1); // trick is here !!!

59.

60.

61.// create minor circle here!!!

62. Shape minorWhiteCircle = new Ellipse2D.Double(150,70, 20,20); // ++

60

63. Shape innerBlackCircle = new Ellipse2D.Double(150,230, 20,20); // R

= 150

64.

65.// draw two big frame shape here...

66. g2.setPaint(Color.WHITE);

67. g2.fill(left);

68. g2.setPaint(Color.BLACK);

69. g2.fill(right);

70.

71.// draw minor circle here!!!

72. g2.fill(minorWhiteCircle);

73. g2.setPaint(Color.WHITE);

74. g2.fill(innerBlackCircle);

75. }

76.

77.private BufferedImage getImage() {

78.if(image != null) {

79.return image;

80. }

81.try {

82. File file = new File("D:\\android\\blue_flower.jpg");

83. image = ImageIO.read(file);

84. } catch (Exception e) {

85. e.printStackTrace();

86. }

87.return image;

88. }

89.

90.public static void main(String[] args) {

91. JFrame frame = new JFrame("Test Panel");

92. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

93. frame.getContentPane().setLayout(new BorderLayout());

94.

95.// Display the window.

96. frame.getContentPane().add(new YingYangGraphics(), BorderLayout.CENT

ER);

相关文档
最新文档