日期:2014-05-20  浏览次数:20656 次

求解一道关于图形的问题,谢谢!
import java.awt.*;
public class TaiJi extends Frame{
  public static void main(String args[]){
  new TaiJi();  
  }
  public TaiJi(){
  super("太极");
  setSize(500,500);
  setVisible(true);
  }
  public void paint(Graphics g){
  g.drawOval(150,150,200,200);
  g.drawOval(175,225,50,50);
  g.fillOval(275,225,50,50);
  }  
}

我想做一个太极的图片,我已经做了一些图形,但是还有一个圆弧不知道怎么画,我知道是用g.drawArc,但是那些弧度怎么表示我却不知道,请问哪位高手知道?谢谢!

------解决方案--------------------
有个fillPolygon 方法,可以根据自定义坐标来描绘图形的。
------解决方案--------------------
更本不需要这么搞

利用Java 2D的图形几何运算功能就可以了,然后对得出的每个子对象进行fill操作就可以

------解决方案--------------------
好了,我花点时间帮你写好了,主要是利用Java 2D对图像的几何操作!希望对你有帮助!
Java code
package com.gloomyfish.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class YingYangGraphics extends JComponent {
    
    /**
     * 
     */
    private static final long serialVersionUID = 8812325148970066491L;
    public YingYangGraphics() {
        super();
    }
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Shape lefthalfCirle = new Ellipse2D.Double(10,10, 300,300); // R = 150
        Shape righthalfCircle = new Ellipse2D.Double(10,10, 300,300); // R = 150
        Shape innerCircle1 = new Ellipse2D.Double(85,10, 150,150); // R/2 = 75
        Shape innerCircle2 = new Ellipse2D.Double(85,160, 150,150); // R = 150
    
        Shape rectangel1 = new Rectangle2D.Double(160, 10, 150, 300);
        Shape rectangel2 = new Rectangle2D.Double(10, 10, 150, 300);
        
        Area left = new Area(lefthalfCirle);
        Area right = new Area(righthalfCircle);
        
        Area area11 = new Area(rectangel1);
        Area area22 = new Area(rectangel2);
        
        left.subtract(area11);
        right.subtract(area22);
        
        Area inner1 = new Area(innerCircle1);
        Area inner2 = new Area(innerCircle2);
        
        left.add(inner1);
        //left.subtract(inner2);
        right.add(inner2);
        right.subtract(inner1); // trick is here !!!
        
        
        // create minor circle here!!!
        Shape minorWhiteCircle = new Ellipse2D.Double(150,70, 10,10); // ++ 60
        Shape innerBlackCircle = new Ellipse2D.Double(150,230, 10,10); // R = 150
        
        // draw two big frame shape here...
        g2.setPaint(Color.BLACK);
        g2.fill(left);
        g2.setPaint(Color.WHITE);
        g2.fill(right);
        
        // draw minor circle here!!!
        g2.fill(minorWhiteCircle);
        g2.setPaint(Color.BLACK);
        g2.fill(innerBlackCircle);
        
    }
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("Test Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        
        // Display the window.
        frame.getContentPane().add(new YingYangGraphics(), BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(330,350));
        frame.pack();
        frame.setVisible(true);
    }
}

------解决方案--------------------
一个π代表半个圆