夜晚美丽的天空(练习2)

/*

*运用产生随机数的方法,在弯月下,大小不一的五颜六色的星星布满黑色的天际,效果如下:

夜晚美丽的填空(练习2) - lishirong - The CTO of LiShirong

 代码如下:↓

*/

 

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

public class Star extends JWindow{
 Star(){
  this.setSize(1440,900);
  this.setVisible(true);
 }
 public static void main(String args[]){
  Star s=new Star();
  MyStarPanel msp=new MyStarPanel();

  //如果直接在JWindow下设置背景色则看不见,因为JWindow会初始化一个JPanel,它以FlowLayout方式占据整个JWindow,因此只能

  //直接在Panel或者JPanel中进行背景色的设置
  msp.setBackground(Color.black);
  s.add(msp);
  s.show();
 }
}
class MyStarPanel extends Panel{
 public void paint(Graphics g){
  for(int i=0;i<150;i++){

   //产生随机大小的星星
   Font f=new Font("",Font.PLAIN,(int)(Math.random()*30));
   g.setFont(f);

   //产生随机的颜色给星星
   g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));

   //星星随机的散落在屏幕上
   g.drawString("*", (int)(Math.random()*1440), (int)(Math.random()*900));
  }

  //画月亮
  g.setColor(Color.white);
  g.fillOval(100, 100, 50, 50);
  g.setColor(Color.black);
  g.fillOval(120, 100, 50, 50);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值