用Java画圆弧

发布于 2024-09-30 03:20:36 字数 187 浏览 3 评论 0原文

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

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

这里的问题是起始角度大于结束角度。反之亦然我有设法画出弧线。任何帮助都会很棒。

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:-

        |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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-10-07 03:20:36

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

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

输入图像描述这里

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

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

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 Arc2D class as well. One more thing to note that in java, this is the default co-ordinate mechanism.

        |90  
        |
180-----------0 
        |
        |270
铁轨上的流浪者 2024-10-07 03:20:36

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

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

深爱不及久伴 2024-10-07 03:20:36

扩展 @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

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.

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)

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文