在 Java 中绘制圆弧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4052070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:34:47  来源:igfitidea点击:

Drawing Arc in Java

javagraphicscoordinate-systems

提问by Emil

I need to draw a Pie Arc in Java with start angle 350 and end angle 20.The cordinate system I follow is as follows:-

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

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

The problem here is that the start angle is greater than the end angle.For the other way round I have managed to draw the arc.Any help would be great.

这里的问题是开始角度大于结束角度。反过来,我已经设法绘制了弧线。任何帮助都会很棒。

回答by bragboy

You will have a start angle and an 'extent' angle and not an end angle. So, I don't think you would be having problem drawing an arc.

您将有一个起始角和一个“延伸”角,而不是一个结束角。所以,我认为你画弧线不会有问题。

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

在此处输入图片说明

Alternatively, you can use the Arc2Dclass as well. One more thing to note that in java, this is the default co-ordinate mechanism.

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

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

回答by Sagar S. De

Use (450 - angle) % 360to switch angles. Concept 450 = 180 + 270;

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

回答by DarceVader

Extending on what @bragbog 's working code, I had to navigate through a similar situation where I had to transpose the co-ordinate system similar to that of the OP to the Java co-ordinate system.

扩展@bragbog 的工作代码,我不得不在类似的情况下导航,在这种情况下,我必须将类似于 OP 的坐标系转换为 Java 坐标系。

This is what I came up with:

这就是我想出的:

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);
    }
}

It may be confusing, but the Java system and the system I was working with, had 45 and 225 remain the same so the transpose being the systems are flipped on it's slope (where 45 and 225 have the same angle from either axis)

这可能令人困惑,但 Java 系统和我使用的系统的 45 和 225 保持不变,因此转置是系统在其斜率上翻转(其中 45 和 225 与任一轴的角度相同)

The absModAngle ensures my resulting angle is within my [0 - 360) range.

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

I created an additional image, but I don't have enough rep to add it. Essetentially

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

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