码迷,mamicode.com
首页 > 其他好文 > 详细

day4 作业,插入图片以及音乐

时间:2017-01-08 16:26:57      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:无限   作业   static   new   rri   jframe   ber   初始化   raw   

这份代码看起来应该还不错,思路清晰不少

  1 package day3;
  2 
  3 import java.applet.Applet;
  4 import java.applet.AudioClip;
  5 import java.awt.Color;
  6 import java.awt.Dimension;
  7 import java.awt.Font;
  8 import java.awt.Frame;
  9 import java.awt.Graphics;
 10 import java.awt.Panel;
 11 import java.awt.Toolkit;
 12 import java.awt.color.ColorSpace;
 13 import java.io.File;
 14 import java.net.MalformedURLException;
 15 import java.net.URL;
 16 import java.util.Random;
 17 
 18 
 19 import javax.swing.JFrame;
 20 import javax.swing.JPanel;
 21 
 22 import org.omg.CORBA.PUBLIC_MEMBER;
 23 
 24 //project 要求
 25         //画框 背景黑色
 26         //很多组下落的文字,文字由01随机组成,长度在20-40 颜色为绿色,并且一组文字中开头颜色最深,
 27         //先画静态文字
 28         //后面的主键变浅
 29         //有背景音乐
 30         //文字在底部消失后,再随机在顶部出现,下落速度随机
 31 class Main1 extends JPanel{
 32 
 33     //获取屏幕大小
 34     //public Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
 35     //int WIDTH = (int)screen.getWidth();
 36     //int HEIGHT = (int)screen.getHeight();
 37     public static final int WIDTH = 1900;
 38     public static final int HEIGHT = 900;
 39     
 40     Random rms = new Random();
 41     //显示的条纹数量
 42     public int strnum = 40;
 43     //每个条纹的长度,暂且写死
 44     public int[] strlen = new int[40];
 45     //每个条纹的坐标
 46     public int[] axis_x = new int[strnum];
 47     //每个点的坐标,前面是条纹id
 48     public int[][] axis_y = new int[strnum][strlen.length];
 49     //存储的字符串
 50     public int[][] numbers = new int[strnum][strlen.length];
 51     //是颜色变化
 52     public int[] colors = new int[strlen.length];
 53     //生成随机速度
 54     public int[] speeds = new int[strnum];
 55     
 56     
 57     public static void main(String[] args) {
 58         // TODO Auto-generated method stub
 59         JFrame myFrame = new JFrame("matrix");
 60         //播放音
 61         
 62         
 63         myFrame.setSize(WIDTH, HEIGHT);
 64         //不能使用
 65         //myFrame.setBackground(Color.BLACK);
 66         //设置是否有边框,当窗体与屏幕一样大
 67         myFrame.setUndecorated(false);
 68         myFrame.setLocationRelativeTo(null);
 69         //相当于3
 70         myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 71         Main1 mypanel = new Main1();
 72         mypanel.setBackground(Color.BLACK);
 73         myFrame.setBackground(Color.BLACK);
 74         myFrame.add(mypanel);
 75         //mypanel.word();
 76         myFrame.setVisible(true);
 77         //mypanel.start();
 78         mypanel.init();
 79     }
 80     
 81     @Override
 82     public void paint(Graphics gp) {
 83         super.paint(gp);
 84         //start();
 85         gp.setFont(new Font("楷体",1, 15));
 86             for(int i = 0;i < strnum;i++)
 87             {
 88                 for(int h = 0; h < colors.length; h++)
 89                 {
 90                     gp.setColor(new Color(0, colors[h], 0));
 91                     gp.drawString("" + numbers[i][h], axis_x[i], axis_y[i][h]);
 92                 }
 93             }
 94     }
 95     
 96     public void word()
 97     {
 98         //生成一列的随机长度
 99         for(int i = 0; i< strnum; i++)
100         {
101             strlen[i] = rms.nextInt(20) + 20;
102         }
103         //生成每一列的x坐标
104         for(int i = 0; i < strnum; i++)
105         {
106             axis_x[i] = rms.nextInt(WIDTH);
107             //距离设置暂且不管
108         }
109         //随机生成一列的Y坐标
110         //同时为每个点赋值
111         for(int i = 0; i < strnum; i ++)
112         {
113             axis_y[i][0] = rms.nextInt(HEIGHT - 100);
114             numbers[i][0] = rms.nextInt(2);
115             for(int h = 1; h < strlen[i]; h++)
116             {
117                 axis_y[i][h] = axis_y[i][h - 1] + 20;
118                 numbers[i][h] = rms.nextInt(2);
119             }
120         }
121     }
122     
123     //无限画啊画
124     public void start()
125     {
126         
127         //初始化word函数
128         //word();
129             for(int i = 0; i < strnum; i++)
130                 {
131                     for(int h = 0; h < strlen[i]; h++)
132                     {
133                         axis_y[i][h] += speeds[i];
134                     }
135                     if(axis_y[i][strlen[i] - 1] > HEIGHT)
136                     {    
137                         for(int h = 0; h < strlen[i]; h++)
138                         {
139                             axis_y[i][h] = 0 - 20 * h;
140                         }
141                         speedC(i);
142                     }
143                 }
144     }
145     
146     //设置速度改变
147     public void speedC(int location)
148     {
149         speeds[location] = rms.nextInt(7) + 5;
150     }
151     
152     public void init()
153     {
154         //颜色的渐变是恒定的
155                 for(int i = 0; i < colors.length; i++)
156                 {
157                     //以速度6递减
158                     colors[i] = 255 - 6 * i;
159                     //小于0的时候跳出
160                     if(colors[i] < 0)
161                     {
162                         break;
163                     }
164                 }
165                 //初始化速度
166                 for(int i = 0; i < speeds.length; i++)
167                 {
168                     speeds[i] = rms.nextInt(5) + 5;
169                 }
170         try {
171             AudioClip aClip = Applet.newAudioClip(new File("meteors.wav").toURI().toURL());
172             //
173             //aClip.loop();    
174             aClip.play();
175             word();
176             while(true)
177             {
178                 start();
179                 Thread.sleep(50);
180                 repaint();
181             }
182         } catch (MalformedURLException e) {
183             // TODO Auto-generated catch block
184             e.printStackTrace();
185         } catch (InterruptedException e) {
186             // TODO Auto-generated catch block
187             e.printStackTrace();
188         }
189     }
190 }

 

day4 作业,插入图片以及音乐

标签:无限   作业   static   new   rri   jframe   ber   初始化   raw   

原文地址:http://www.cnblogs.com/NEFPHYS/p/6262089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!