java作业

本文介绍了一个简单的Java程序示例,展示了如何使用Swing和AWT在Java中绘制不同颜色的弧形。该程序通过循环改变颜色并调整角度来绘制一系列彩色的弧形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.swing.JFrame;  
import javax.swing.JPanel;  
import java.awt.Color;  
import java.awt.Graphics;  
 
public class DrawArcs extends JFrame { 
  public DrawArcs() {  
    setTitle("画弧形");  
    getContentPane().add(new ArcsPanel());  
  }  
 
  /** 主方法 */ 
  public static void main(String[] args) {  
    DrawArcs frame = new DrawArcs();  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(250, 300);  
    frame.setLocationRelativeTo(null);  
    frame.setVisible(true);  
  }  
}  
 
// 在面板上画弧形的类  
class ArcsPanel extends JPanel {  
	Color[] colors = {
			  Color.WHITE,Color.BLUE, Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED
	  };
  protected void paintComponent(Graphics g) {  
    super.paintComponent(g);  
    g.setColor(Color.BLUE); //设置弧形的颜色为蓝色  
 
    int i=0;  
    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;  
	int counters = 5;
    //使用while循环画弧形  
    while(i<360){
			g.setColor(colors[counters]);
			counters--;
        g.fillArc(x, y, 2 * radius, 2 * radius, i, 30);  
        i+=60;  
    }  
  }  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值