用Java绘制弧形

时间:2010-10-29 12:56:31

标签: java graphics coordinate-systems

我需要在Java中使用起始角度350和结束角度20绘制一个Pie Arc。我遵循的坐标系统如下: -

        |0  
        |
270-----------90 
        |
        |180

这里的问题是起始角度大于结束角度。换句话说,我已设法绘制弧线。任何帮助都会很棒。

3 个答案:

答案 0 :(得分:6)

您将具有起始角度和“范围”角度,而不是结束角度。所以,我认为你不会在绘制弧线时遇到问题。

import java.awt.Graphics;
import javax.swing.JFrame;

public class Test extends JFrame{
    public static void main(String[] args){
        new Test();
    }
    public Test(){
        this.setSize(400,400);
        this.setVisible(true);
    }
    public void paint(Graphics g) {
        g.fillArc(100, 100, 100, 100, 70, 30);
    }
}

enter image description here

或者,您也可以使用Arc2D类。还有一点要注意,在java中,这是默认的坐标机制。

        |90  
        |
180-----------0 
        |
        |270

答案 1 :(得分:2)

使用(450 - 角度)%360 切换角度。概念450 = 180 + 270;

答案 2 :(得分:0)

在@bragbog的工作代码上,我不得不浏览一个类似的情况,在该情况下,我必须将类似于OP的坐标系转置为Java坐标系。

这是我想出的:

float coordChangeOffset = ((arcDegree % 180) - 45) * 2;
filterPanel.setArc(absModAngle(arcDegree - coordChangeOffset), 360 - sectorAngle);

private float absModAngle(float deg) {
    return modAngle((deg + 360));
}

public class FilterPanel extends JPanel {

    private final int x, y, w, h;
    private int startAngle, arcFill;

    public FilterPanel(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;

        setBackground(UiColorPalette.TRANSPARENT);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) this.getGraphics();

        g2d.setColor(UiColorPalette.FILTER_FILL);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillArc(x, y, w, h, startAngle, arcFill);
    }

    void setArc(float startAngle, float arcFill) {
        this.startAngle = (int) startAngle;
        this.arcFill = (int) arcFill;
        System.err.out("Java Coordinate System - StartAngle: " + startAngle + ", arcFill: " + arcFill);
    }
}

这可能会造成混淆,但是Java系统和我正在使用的系统的45和225保持不变,因此转置是将系统在其斜率上翻转(其中45和225与任一轴的夹角相同)

absModAngle确保我的最终角度在我的[0-360)范围内。

我创建了一个附加图像,但是没有足够的代表来添加它。本质上

y = x - F(x), where F(x) is coordChangeOffset noted above ((x Mod 180) - 45) * 2
相关问题