2014-03-29 62 views
0

enter image description here試圖繪製橢圓時鐘在Java中

我希望這是12個橢圓所以它們代表全天候間隔5分鐘。但是,每次我更改路徑迭代器的linApproxLen時,它總是繪製太多的省略號或9個省略號。我怎樣才能讓這些橢圓代表5分鐘的標記?

private Ellipse2D ellipse = new Ellipse2D.Double(); 
public void setClockContour(int w, int h) 
{ 
size = (w > h) ? h/6f : w/6f; 
ellipse.setFrame(w/2-size*2-4.5f,h/2-size*2-4.5f,size*4,size*4); 
    double linApproxLen = 0.75 * size * 0.258819;    // sin(15 degree) 
    PathIterator pi = ellipse.getPathIterator(null, linApproxLen); 
    Point2D[] points = new Point2D[100]; 
    int num_pts = 0; 
    while (!pi.isDone()) 
    { 
     float[] pt = new float[6]; 
     switch (pi.currentSegment(pt)) { 
      case FlatteningPathIterator.SEG_MOVETO: 
      case FlatteningPathIterator.SEG_LINETO: 
       points[num_pts] = new Point2D.Float(pt[0], pt[1]); 
       num_pts++; 
     } 
     pi.next(); 
    } 
    pts = new Point2D[num_pts]; 
    System.arraycopy(points, 0, pts, 0, num_pts); 

} 

回答

1

我認爲這種方法不會工作,無論linApproxLen參數。此參數僅指定線條的「平直度」。雖然這肯定會影響到返回的線段的數量,實際數量將至少取決於你是畫橢圓的半徑。因此,即使設法在一種情況下精確返回12個點,更改半徑(即以wh給出的尺寸)也會影響返回點的數量。

計算以圓形形成的12點的位置是特別容易。你可以用相應角度的正弦和餘弦來計算圓上的位置。所得到的點然後可以被縮放或移動以便獲得期望的形狀。

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.RenderingHints; 
import java.awt.geom.Ellipse2D; 
import java.awt.geom.Point2D; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class ClockCirclesTest 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new GridLayout(1, 0)); 
     frame.getContentPane().add(new ClockCirclesPanel()); 
     frame.setSize(400,400); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

class ClockCirclesPanel extends JPanel 
{ 
    @Override 
    protected void paintComponent(Graphics gr) 
    { 
     super.paintComponent(gr); 
     Graphics2D g = (Graphics2D)gr; 
     g.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 

     int w = getWidth()/2; 
     int h = getHeight()/2; 
     Point2D points[] = computePoints(w, h, 12); 

     int cx = getWidth()/2; 
     int cy = getHeight()/2; 
     g.translate(cx, cy); 
     for (Point2D p : points) 
     { 
      g.draw(new Ellipse2D.Double(
       p.getX() - 5, p.getY() - 5, 
       10, 10)); 
     } 
    } 

    private Point2D[] computePoints(double w, double h, int n) 
    { 
     Point2D points[] = new Point2D[n]; 
     double angleDeltaRad = Math.PI * 2/n; 
     for (int i=0; i<n; i++) 
     { 
      double angleRad = i * angleDeltaRad; 
      double ca = Math.cos(angleRad); 
      double sa = Math.sin(angleRad); 
      double x = sa * w/2; 
      double y = ca * h/2; 
      points[i] = new Point2D.Double(x,y); 
     } 
     return points; 
    } 

}