java吐泡泡过关,java代码,鱼吐泡泡程序

智荣博客

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.color.*;

import java.util.Vector;

import java.util.Iterator;

class Fish{//鱼类

public void draw(Graphics g) {//画鱼

Color c = g.getColor();

g.setColor(Color.black);

g.drawLine(250,230,110,110);

g.drawLine(250,270,110,390);

g.drawLine(250,230,230,250);

g.drawLine(250,270,230,250);

g.drawLine(110,230,60,200);

g.drawLine(110,270,60,300);

g.drawLine(60,200,60,300);

g.drawLine(110,110,110,390);

g.setColor(Color.black);

g.fillOval(210,220,20,20);

g.setColor(c);

}

}

class Bubble{//泡泡类

int x=250,y=233,x_speed,y_speed;//泡泡的位置以及x,y方向的移动速度

public void draw(Graphics g){//画泡泡

Color c=

g.getColor();

g.setColor(Color.blue);

g.fillOval(x, y, 20, 20);

g.setColor(Color.white);

g.fillOval(x+1, y+1, 16, 16);

g.setColor(c);//把画笔默认颜色还原

x+=8;

}

}

public class FishGame extends Frame implements

ActionListener{//窗体类

Vector bubb = new

Vector();//每吐出的泡泡就添加进Vector集合

Image offScrrenImage = null;//重绘泡泡时消除上一次的轨迹,避免形成连续轨迹

Fish fish = new Fish();

public FishGame(){

this.setLayout(new BorderLayout());

this.setTitle("FishGame");

this.setBackground(Color.white);

this.setSize(600,400);

this.setResizable(false);

this.setVisible(true);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

Button b =

new Button("吐泡泡");

b.addActionListener(this);

this.add(b,BorderLayout.SOUTH);

Thread t =

new Thread(new BubbleThread());

t.start();//启动泡泡移动线程

Thread t2 =

new Thread(new BubbleCountThread());

t2.start();//启动自动吐泡泡线程

}

public void paint(Graphics g){

fish.draw(g);

Iterator it

= bubb.iterator();

while(it.hasNext()){

((Bubble) it.next()).draw(g);

}

}

public void update(Graphics g){//绘制虚拟图片

if(offScrrenImage == null){

offScrrenImage = this.createImage(800,600);//设置虚拟图片和主窗体背景一样宽

}

Graphics goffScreen =

offScrrenImage.getGraphics();//拿到虚拟图片的画笔

Color c = goffScreen.getColor();

goffScreen.setColor(Color.white);

goffScreen.fillRect(0, 0, 800, 600);

paint(goffScreen);

g.drawImage(offScrrenImage, 0, 0, null);

goffScreen.setColor(c);

}

public void actionPerformed(ActionEvent arg0) {

bubb.add(new

Bubble());

}

private class BubbleThread implements Runnable{//控制泡泡移动的线程类

public void run() {

while(true) {

try

{

Thread.sleep(300);

}

catch(InterruptedException e){

e.printStackTrace();

}

repaint();

}

}

}

private class BubbleCountThread implements

Runnable{//控制泡泡自动吐的线程类

public void run() {

while(true) {

try

{

Thread.sleep(1000);//1秒吐一个

}

catch(InterruptedException e){

e.printStackTrace();

}

bubb.add(new Bubble());

}

}

}

public static void main(String args[]){

new FishGame();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值