第三天 体验班 演示鱼

第三天开始以演示为主:


package day03.pm;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;


import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class Demo02 {

public static void main(String[] args) throws Exception {

JFrame frame = new JFrame();

FishPane pane = new FishPane();

frame.add(pane);

frame.setSize(800, 510);

frame.setLocationRelativeTo(null);

//Default 默认的 Close关闭 Operation操作 

// EXIT_ON_CLOSE 在关闭时候离开(系统)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

pane.action();

}

}

class FishPane extends JPanel{

int score;

BufferedImage background;

BufferedImage fish;

int x = 500;

public FishPane() throws Exception {

score = 100;

background = ImageIO.read(new File("bg.jpg"));

fish = ImageIO.read(new File("fish01_00.png"));

}

public void action() throws Exception {

while(true){

x = x-2;

repaint();//重绘方法,尽快调用paint(g)

Thread.sleep(1000/10);//Thread线程 sleep休眠

}

}

public void paint(Graphics g) {

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

g.setColor(Color.WHITE);

g.drawString("SCORE:"+score, 20, 30);

g.drawImage(fish, x, 100, null);

}

}




捕鱼游戏:

package day03.pm2;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;


import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class Demo03 {

public static void main(String[] args) throws Exception {

JFrame frame = new JFrame("Demo03");

FishPane pane = new FishPane();

frame.add(pane);

frame.setSize(800, 510);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

pane.action();

}

}

class FishPane extends JPanel{

int score;

BufferedImage background;

Fish nemo;

public FishPane() throws Exception {

score = 100;

background = ImageIO.read(new File("bg.jpg"));

nemo = new Fish();//创建了鱼对象,就是55个变量

nemo.image = ImageIO.read(new File("fish05_00.png"));

nemo.x=500;

nemo.y=100;

nemo.width = nemo.image.getWidth();//Width 宽

nemo.height = nemo.image.getHeight();//Height 高

}

public void action() throws Exception{

while(true){

nemo.x = nemo.x - 2;

if(nemo.x <= -nemo.width){//鱼出界了

nemo.x = 800;

}

repaint();

Thread.sleep(1000/10);

}

}

public void paint(Graphics g) {

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

g.setColor(Color.white);

g.drawString("SCORE:"+score, 20,30);

g.drawImage(nemo.image, nemo.x, nemo.y, null);

}

}

class Fish{//鱼类

int x;

int y;

int width;

int height;

BufferedImage image;

}



你可能感兴趣的:(null,import,package,public,frame)