利用Swing在窗体上绘制线条与文字(三)——绘制弧形

效果

在这里插入图片描述

代码:

package Test;


import javax.swing.*;
import java.awt.*;

public class TestFrame extends JFrame {
    public TestFrame(){
        setTitle("DrawArcs");
        add(new ArcsPanel());
    }

    public static void main(String[] args) {
        TestFrame frame=new TestFrame();
        frame.setTitle("TestFigurePanel");
        frame.setSize(400,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class ArcsPanel extends JPanel{
    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        int xCenter=getWidth()/2;
        int yCenter=getHeight()/2;
        int radius=(int)(Math.min(getWidth(),getHeight()*0.4));

        int x=xCenter-radius;
        int y=yCenter-radius;

        g.fillArc(x,y,2*radius,2*radius,0,30);
        g.fillArc(x,y,2*radius,2*radius,90,30);
        g.fillArc(x,y,2*radius,2*radius,180,30);
        g.fillArc(x,y,2*radius,2*radius,270,30);

    }
}

讲解

    g.fillArc(x,y,2*radius,2*radius,0,30);

就是从(x,y)作为起点,长度和高度是2*radius,从0°开始绘制,到30°结束。其余三个都是以此类推。

因为选择的是fill,所以是实心的,默认为黑色。

角度也可以为负数,从东边开始,逆时针为正数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值