本文介绍了尝试使用 Java 绘制圆弧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用以下代码在 Java 中绘制弧线:

i've been trying to draw an arc in java using this code:

g.fillArc(50, 50, 100, 100, 0, 180)

其中 g 是图形对象.

where g is a graphics object.

产生下面的蓝色对象:

我真正想做的是制作如下所示的东西:

what i'm actually trying to do is produce something that looks like this:

提前感谢您的帮助!

推荐答案

尝试 g.fillArc(50, 50, 100, 100, 180, 180) 代替.

基本上,第一个角度是起点,第二个角度是它应该通过的度数(从起点开始).

Basically, the first angle is the where to start, the second angle is the number of degrees (from the start) it should arc through.

所以如果你只是想要一个 5 度的饼滑,你可以使用类似 g.fillArc(50, 50, 100, 100, 0, 5)

So if you just wanted a pie slip of 5 degrees, you would use something like g.fillArc(50, 50, 100, 100, 0, 5)

看看 Graphics#fillArcGraphics2D 了解更多信息

工作示例

public class PaintTest03 {

    public static void main(String[] args) {
        new PaintTest03();
    }

    public PaintTest03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.RED);
            g.fillArc(0, 0, 100, 100, 180, 180);
        }

    }

}

这篇关于尝试使用 Java 绘制圆弧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:23