2013-10-31 91 views
0

你好傢伙我需要畫粗線的曲線主要是那些起始N - S,如果可能的話得到350-10 340-20等等。我試過了QuadCurve2D和drawArc,但沒有一個做了這項工作。有沒有辦法避免使用drawPolyline(xPoints,yPoints,WIDTH),因爲它需要幾百對才能畫出一條線。繪製曲線,任何建議

enter image description here

這是代碼的一部分,以避免你的時間損耗測試自己:

 public class PaintMyQuad extends JPanel { 

    @Override 
    protected void paintComponent(Graphics g) { 

     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g.create(); 

     QuadCurve2D.Double curve = new QuadCurve2D.Double(200,0,200,100,200,200); 
     QuadCurve2D.Double curve1 = new QuadCurve2D.Double(200,0,180,100,200,200); 
     QuadCurve2D.Double curve2 = new QuadCurve2D.Double(200,0,160,100,200,200); 
     //etc 
     g2d.setColor(Color.RED); 
     g2d.draw(curve); 
     g2d.draw(curve1); 
     g2d.draw(curve2); 
     g2d.drawOval(100,100,200,200); 
     g2d.drawArc(100,100, 100, 200, 90, 180); 
     g2d.drawArc(100, 100, 100, 200, 180, 360); 
     g2d.drawArc(100, 100, 0, 200, 90, 180); 
     g2d.drawRect(100, 100, 200, 200); 

回答

1

林不知道這是否真的是一個答案 - 但我遇到了解決問題。我嘗試了抗鋸齒,但看起來更糟糕。 你可以這樣做的一種方法是在for循環中爲奇數和偶數添加if語句。對於這個問題的一些建議很開心,我想有一個更好的庫可以獲得更好的圈子。 也許我在做老派的方式?

import java.awt.*; 
import javax.swing.*; 

public class Stereonet{ 

    private static int centreX, centreY, radius; 
    private Color colour; 

    /**Stereonet template contructor takes 3 int parameters 
    * x, y for centre position and radius for stereonet radius*/ 
    public Stereonet(int x , int y, int radius, Color colour){ 

    centreX = x; 
    centreY = y; 
    this.radius = radius; 
    this.colour = colour; 

    } 

    public void draw(Graphics g){ 

     Graphics2D g2D = (Graphics2D) g;   
     g2D.setStroke(new BasicStroke(2F)); 
     g.setColor(colour); 
     g.drawOval(centreX - radius , centreY - radius, radius * 2 , radius * 2); 
     g2D.setStroke(new BasicStroke(1F)); 
     g.drawLine(centreX, centreY - radius, centreX, centreX + radius); 
     g.drawLine(centreX - radius, centreY, centreX + radius, centreY); 

     g2D.setStroke(new BasicStroke(1F)); 

     for(int degrees = 10; degrees <= 80; degrees += 10){ 


     double greatRadius = radius/(Math.cos(Math.toRadians(degrees))); // radius of great circle 
     int greatX1 = (int)Math.round(centreX + radius * (Math.tan(Math.toRadians(degrees))) 
             - greatRadius); // x coord of great circle left hemisphere 
     int greatX2 = (int)Math.round(centreX - (radius * (Math.tan(Math.toRadians(degrees)))) 
             - greatRadius); // x coord of great circle right hemisphere 
     int greatY = (int)Math.round(centreY - greatRadius); // y coord of great circle 

     double smallRadius = (radius/(Math.tan(Math.toRadians(degrees)))); 
     int smallY1 = (int)Math.round((centreY - (radius/(Math.sin(Math.toRadians(degrees)))) - smallRadius)); 
     int smallY2 = (int)Math.round((centreY + (radius/(Math.sin(Math.toRadians(degrees)))) - smallRadius)); 
     int smallX = (int)Math.round(centreX - smallRadius); 

     g.drawArc(greatX1, greatY, 2 * (int)Math.round(greatRadius), 2 * (int)Math.round(greatRadius), 
        90 + degrees, 180 - (2 * degrees)); 
     g.drawArc(greatX2, greatY, 2 * (int)Math.round(greatRadius), 2 * (int)Math.round(greatRadius), 
        270 + degrees, 180 - (2 * degrees)); 
     g.drawArc(smallX, smallY1, 2 * (int)Math.round(smallRadius), 2 * (int)Math.round(smallRadius), 
        270 - degrees, 180 - (2 * (90 - degrees))); 
     g.drawArc(smallX, smallY2, 2 * (int)Math.round(smallRadius), 2 * (int)Math.round(smallRadius), 
        90 - degrees, 180 - (2 * (90 - degrees))); 

     } 

    } 

    public static int getRadius(){ 

     return radius; 

    } 

    public static int getX(){ 

     return centreX; 

    } 

    public static int getY(){ 

     return centreY; 

    } 
+0

對不起,花了我這麼久,我最近再次打開這個項目來完成它。那時我不明白你爲什麼提到反鋸齒,但現在我明白了。看來使用RenderingHints是解決這個問題的唯一方法。我目前正在嘗試應用雙三次插值法,但沒有成功。你有沒有進展?我也遇到了兩極問題,我試圖從我在網上找到的算法中應用高斯加權量化(沒有成功(我正在使用這個算法來「羣集」我的極點))。 – Renobatio

1

如果你需要大膽的線條,試試這樣:

.... 
Graphics2D g2d = (Graphics2D)g; 

BasicStroke pen1 = new BasicStroke(5); // this is stroke with 5px width, 
g2d.setStroke(pen1); 

g2d.drawArc(100,100, 100, 200, 90, 180); 
g2d.drawArc(100, 100, 100, 200, 180, 360); 
... 

製作兩種類型的線條的兩個筆劃,並使用它。

+0

我需要繪製從beggining張貼的,如果你有一個空的面板I refered爲「大膽的」,以圖片的大膽的線條,讓人們知道我的意思這行,但感謝你的事業它就會有當我終於畫出他們的時候,我會大膽的。對不起,如果我的英語不太好,誤導你。 – Renobatio

+0

看看帖子吧!) –