第三天 體驗班 演示魚

第三天開始以演示爲主:


package day03.pm;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.p_w_picpath.BufferedImage;

import java.io.File;


import javax.p_w_picpathio.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.p_w_picpath.BufferedImage;

import java.io.File;


import javax.p_w_picpathio.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.p_w_picpath = ImageIO.read(new File("fish05_00.png"));

nemo.x=500;

nemo.y=100;

nemo.width = nemo.p_w_picpath.getWidth();//Width 寬

nemo.height = nemo.p_w_picpath.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.p_w_picpath, nemo.x, nemo.y, null);

}

}

class Fish{//魚類

int x;

int y;

int width;

int height;

BufferedImage p_w_picpath;

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章