与你所熟悉的编程语言一样,Java中也提供了较为丰富的图形处理程序,本文给出了一个简单的图形绘制的源代码:五个不同颜色的同心圆。
/***程序要求:新建一个600*600像素的应用程序窗口,并在窗口中绘制5个不同颜色的同心圆,*所有圆心都是屏幕的中心点,相邻两个圆直接的半径相差50像素*效果图如下图所示(颜色随机设置),源程序保存为Ex7_1.java。*作者:wwj*日期:2012/4/25*功能:显示一个有5个不同颜色的同心圆**/
import javax.swing.*;import java.awt.*;import java.awt.Color;public class Ex7_1 extends JFrame{ int red,green,blue; Color color;
public Ex7_1() { super("一个有5个不同颜色的同心圆");//显示窗口名称 setSize(600,600);//设置窗口大小 setVisible(true);//设置为可见 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭动作 }
public void paint(Graphics g) { //第一个圆red=(int)(Math.random()*255);green=(int)(Math.random()*255);blue=(int)(Math.random()*255);color=new Color(red,green,blue);g.setColor(color);g.fillOval(175,175,250,250);//第二个圆red=(int)(Math.random()*255);green=(int)(Math.random()*255);blue=(int)(Math.random()*255);color=new Color(red,green,blue);g.setColor(color);g.fillOval(200,200,200,200);//第三个圆red=(int)(Math.random()*255);green=(int)(Math.random()*255);blue=(int)(Math.random()*255);color=new Color(red,green,blue);g.setColor(color);g.fillOval(225,225,150,150);//第四个圆red=(int)(Math.random()*255);green=(int)(Math.random()*255);blue=(int)(Math.random()*255);color=new Color(red,green,blue);g.setColor(color);g.fillOval(250,250,100,100);//第五个圆red=(int)(Math.random()*255);green=(int)(Math.random()*255);blue=(int)(Math.random()*255);color=new Color(red,green,blue);g.setColor(color);g.fillOval(275,275,50,50);
} public static void main(String[] args) { Ex7_1 e = new Ex7_1(); }}
程序最终效果如下:
本文源自CSDN博客,原文地址:http://blog.csdn.net/wwj_748/article/details/7522672