简体   繁体   中英

Trying to draw an arc using Java

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

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

where g is a graphics object.

which yields the blue object below:

结果

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

为...而奋斗

thanks in advance for any help!

Try g.fillArc(50, 50, 100, 100, 180, 180) instead.

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

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

Have a look at Graphics#fillArc and Graphics2D for more info

Working example

在此输入图像描述

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM