自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Java 2D繪制黑白太極圖案

開發(fā) 后端
本文講解了利用Java 2D的Area對象對繪制形狀幾何的操作,完成太極圖案的繪制,使用Paint來完成對不同顏色的填充。下文附加全部代碼。

一:基本原理

利用Java 2D的Area對象對繪制形狀幾何操作的支持,完成太極圖案的繪制,使用Paint來

完成對不同顏色的填充。Java 2D圖形API Area對Shape支持四種幾何操作:

◆  Add (加)- 保留兩個幾何形狀及其重疊部分

◆ Subtract (減) – 從第一個幾何形狀上減去和第二個重疊的部分,保留減去之后的第一個幾何形狀

◆ Intersect (與) – 只保留兩個幾何形狀重疊的部分。

◆ ExclusiveOr(或) – 保留他們相互不重疊的部分。

參看下圖:

 

二:程序運行效果

程序基于JDK6 API完成。

背景圖片我是在網(wǎng)上找來的,可以自己替換的。

程序源代碼如下:

  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(), 00, image.getWidth(), image.getHeight(), null);  
  35.         Shape lefthalfCirle = new Ellipse2D.Double(10,10300,300); // R = 150  
  36.         Shape righthalfCircle = new Ellipse2D.Double(10,10300,300); // R = 150  
  37.         Shape innerCircle1 = new Ellipse2D.Double(85,10150,150); // R/2 = 75  
  38.         Shape innerCircle2 = new Ellipse2D.Double(85,160150,150); // R = 150  
  39.       
  40.         Shape rectangel1 = new Rectangle2D.Double(16010150300);  
  41.         Shape rectangel2 = new Rectangle2D.Double(1010150300);  
  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,7020,20); // ++ 60  
  63.         Shape innerBlackCircle = new Ellipse2D.Double(150,23020,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.CENTER);  
  97.         frame.setPreferredSize(new Dimension(330,350));  
  98.         frame.pack();  
  99.         frame.setVisible(true);  
  100.     }  

原文鏈接:http://blog.csdn.net/jia20003/article/details/7105487

【編輯推薦】

  1. Java并發(fā)編程:守護(hù)線程
  2. 深入Java虛擬機(jī)之內(nèi)存優(yōu)化
  3. 深入Java虛擬機(jī)之虛擬機(jī)體系結(jié)構(gòu)
  4. Java與XML:采用DOM操作XML文件
  5. 從Java的角度理解Ext的extend
責(zé)任編輯:林師授 來源: jia20003的博客
相關(guān)推薦

2012-02-22 15:41:50

HTML 5

2012-05-07 14:25:16

HTML5

2011-04-25 14:36:24

Ubuntu Unit

2022-06-16 10:33:14

代碼AI

2011-08-11 18:07:55

iPhoneQuratz 2D

2023-05-03 09:01:41

CanvasWebGL

2013-01-08 11:00:20

IBMdW

2020-10-26 13:40:00

CascadingSt

2012-05-07 14:13:59

HTML5

2022-05-23 10:26:10

人工智能機(jī)器學(xué)習(xí)機(jī)器視覺

2012-12-24 09:11:58

iOSUnity3D

2012-05-07 15:08:00

HTML5

2012-05-08 10:20:36

HTML5

2012-05-08 09:53:56

HTML 5

2012-05-07 16:14:21

HTML5

2013-01-30 16:15:40

adobeHTML5css3

2022-07-13 10:20:14

自動駕駛3D算法

2023-09-27 10:13:09

3D模型

2011-08-17 14:07:43

IOS開發(fā)Quartz 2D

2012-11-07 09:43:58

IBMdw
點贊
收藏

51CTO技術(shù)棧公眾號