在黑色背景画上随机色彩的圆圈

来源:互联网 发布:nginx 配置虚拟目录 编辑:程序博客网 时间:2024/06/02 14:39

在黑色背景画上随机色彩的圆圈

import javax.swing.*;//在黑色背景画上随机色彩的圆圈import java.awt.*;public class MyDrawPanel extends JPanel{public void paintComponent(Graphics g){g.fillRect(0,0,this.getWidth(),this.getHeight());//以默认颜色填充,前两个参数时起点的坐标,后面2个参数分别是宽度和高度,此处取得本身的身高,因此会把panel填满int red=(int)(Math.random()*255);int green=(int)(Math.random()*255);int blue=(int)(Math.random()*255);Color randomColor=new Color(red,green,blue);//传入三个参数来代表R,C,B的值g.setColor(randomColor);g.fillOval(70,70,100,100);//填满参数指定的椭圆形区域}}

import javax.swing.*;public class SimpleGuil{public static void main(String [] args){JFrame frame=new JFrame();MyDrawPanel my=new MyDrawPanel();frame.getContentPane().add(my);//JButton button=new JButton();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这一行程序会在window关闭时候把程序结束掉//frame.getContentPane().add(button);//frame.setSize(300,300);frame.setVisible(true);}}


0 0
原创粉丝点击