一份画图的java小作业

如题

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawRainbow extends JPanel
{
// define indigo and violet
    private final static Color VIOLET = new Color(128, 0, 128);
    private final static Color INDIGO = new Color(75, 0, 130);

// colors to use in the rainbow, starting from the innermost
// The two white entries result in an empty arc in the center
    private Color[] colors =
        { Color.WHITE, Color.WHITE, VIOLET, INDIGO, Color.BLUE,
        Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED };

// constructor
    public DrawRainbow()
    {
        setBackground(Color.WHITE); // set the background to white 23 }
    }
// draws a rainbow using concentric arcs
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int radius = 20; // radius of an arc 31
// draw the rainbow near the bottom-center
        int centerX = getWidth() / 2;
        int centerY = getHeight() - 10;

// draws filled arcs starting with the outermost
        for (int counter = colors.length; counter > 0; counter--){
// set the color for the current arc
            g.setColor(colors[counter - 1]);

// fill the arc from 0 to 180 degrees
            g.fillArc(centerX - counter * radius,
                centerY - counter * radius,
                counter * radius * 2, counter * radius * 2, 0, 180);
        }
    }
}
import javax.swing.JFrame;
public class DrawRainbowTest {
    public static void main(String[] args)
    {
        DrawRainbow panel = new DrawRainbow();
        JFrame application = new JFrame();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(400, 250);
        application.setVisible(true);
    }
} // end class DrawRainbowTest
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值