首页 | 注册 | 登陆
首页 >> 技术专栏 >> java文章 >> java基础 

雪花漂漂


作者java 来源java 加入时间:2006年02月21日
摘要:
雪花漂漂
   程序运行图:


import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*…

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info2/a_a27d28b846f70fce.htm




雪花漂漂


站点:爱心种子小博士 关键字:雪花漂漂




雪花漂漂
   程序运行图:


import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.Random; public class TestSnow extends JFrame{ AnimationCanvas canvas; JButton startButton,stopButton; public TestSnow(){ super("Snow"); Container container=getContentPane(); canvas=new AnimationCanvas(); container.add(canvas); startButton=new JButton("Start"); startButton.addActionListener(new ButtonListener()); stopButton=new JButton("Stop"); stopButton.addActionListener(new ButtonListener()); JPanel panel=new JPanel(); panel.add(startButton);panel.add(stopButton); container.add(BorderLayout.SOUTH,panel); addWindowListener(new WindowEventHandler()); setSize(600,500); show(); } class WindowEventHandler extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } } class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ JButton temp=(JButton)e.getSource(); if(temp.equals(startButton)){ canvas.start(); }else if(temp.equals(stopButton)){ canvas.stop(); } } } public static void main(String args[]){ new TestSnow(); } } class AnimationCanvas extends JPanel implements Runnable { Thread mainThread ; Image image; BufferedImage bi; Random rand ; int snows, wind, threadSleep, snowSize ; int[] snowX, snowY ; // long stopTime = 0; Dimension dim ; MediaTracker mt ; AnimationCanvas() { rand = new Random(); setSize(600,450); dim=getSize(); snows =150; snowSize =3 ; threadSleep =80; snowX = new int[snows]; snowY = new int[snows]; for(int i = 0; i < snows; i++) { snowX[i] = rand.nextInt() % (dim.width / 2) + dim.width / 2; snowY[i] = rand.nextInt() % (dim.height / 2) + dim.height / 2; } image=getToolkit().getImage("022.jpg"); MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForID(0); } catch(InterruptedException _ex) { return; } } public void start() { if(mainThread == null) { mainThread = new Thread(this); mainThread.start(); } } public void stop() { if(mainThread!=null) mainThread.interrupt(); mainThread = null; } public void run() { while(mainThread != null) { try { Thread.sleep(threadSleep); } catch(InterruptedException _ex) { return ; } repaint(); } } public void drawBackSnow(Graphics2D g) { g.setColor(Color.white); for(int i = 0; i < snows; i++) { g.fillOval(snowX[i], snowY[i], snowSize, snowSize); snowX[i] += rand.nextInt() % 2 + wind; snowY[i] += (rand.nextInt() % 6 + 5) / 5 + 1; if(snowX[i] >= dim.width) snowX[i] = 0; if(snowX[i] < 0) snowX[i] = dim.width - 1; if(snowY[i] >= dim.height || snowY[i] < 0) { snowX[i] = Math.abs(rand.nextInt() % dim.width); snowY[i] = 0; } } wind = rand.nextInt() % 5 - 2 ; } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; Dimension d=getSize(); bi=new BufferedImage(d.width,d.height,BufferedImage.TYPE_INT_ARGB); Graphics2D big=bi.createGraphics(); big.setColor(Color.black); big.fillRect(0, 0, d.width,d.height); big.drawImage(image, 0, 0, this); drawBackSnow(big); g2d.drawImage(bi, 0, 0, null); big.dispose(); } }



发布人:love
→ 推荐给我的好友 → 报告错误链接
上篇文章:自己实现ORM
下篇文章:基于Java的代理设计模式
〖文章打印〗
〖关闭窗口〗
发表评论
查看评论
中“雪花漂漂”相关内容 中“雪花漂漂”相关内容
中“雪花漂漂”相关内容 中“雪花漂漂”相关内容
中“雪花漂漂”相关内容 中“雪花漂漂”相关内容

关于我们网站留言友情链接与我在线与我聊天领取红包管理TOP