类的设计
Pool 继承 JPanel
background
Fish[] allFish
FishingNet fishingNet
Fish
x
y
width
height
images
index
image
FishingNet
x
y
width
height
image
素材在最下面:ps技术有限 不喜欢的可以自己画!!!
package xyz.rhel.fish;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class FishingNet {
private int x;
private int y;
private int width;
private int height;
private BufferedImage image;
private boolean show;
public FishingNet(String img) throws Exception {
image = ImageIO.read(new File(img));
width = image.getWidth();
height = image.getHeight();
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public boolean isShow(){
return show;
}
public void setShow(boolean show){
this.show = show;
}
}package xyz.rhel.fish;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
public class Fish implements Runnable {
private int x;
private int y;
private int width;
private int height;
private int index;
private int step;
private BufferedImage[] images;
private BufferedImage image;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public BufferedImage[] getImages() {
return images;
}
public void setImages(BufferedImage[] images) {
this.images = images;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public Fish(String perfix) throws Exception {
images = new BufferedImage[10];
for (int i = 0; i < 10; i++) {
String file = perfix + i + ".png";
images[i] = ImageIO.read(new File(file));
}
image = images[0];
width = image.getWidth();
height = image.getHeight();
Random random = new Random();
x = random.nextInt(800 - width);
y = random.nextInt(480 - height);
step = random.nextInt(3) + 1;
}
public void getOut() {
Random random = new Random();
x = 800;
y = random.nextInt(480 - height);
step = random.nextInt(3) + 1;
}
public void move() {// 鱼移动
x -= step;
if (x < -width) {
getOut();
}
image = images[index++%images.length];//更换图片
}
public boolean catchBy(FishingNet fishingNet) {// 抓鱼
int dx = fishingNet.getX() - this.x;
int dy = fishingNet.getY() - this.y;
return dx >= 0 && dx < width && dy >= 0 && dy < height;
}
public void run() {// 在Runnable 中定义的抽象方法,在子类中来重写
while (true) {
move();
try {
Thread.sleep(1000 / 10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}package xyz.rhel.fish;
import static javax.imageio.ImageIO.read;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pool extends JPanel {
private BufferedImage background;
private FishingNet fishingNet;
private Fish[] all;
public Pool() throws Exception{
background = read(new File("pool.png"));
fishingNet = new FishingNet("fishingNet.png");
all = new Fish[]{
new Fish("f"),new Fish("f"),new Fish("f"),new Fish("f")
,new Fish("f"),new Fish("f"),new Fish("f"),new Fish("f")
,new Fish("df"),new Fish("df"),new Fish("df"),new Fish("df")
,new Fish("df"),new Fish("df"),new Fish("df"),new Fish("df")
,new Fish("jf"),new Fish("jf"),new Fish("jf"),new Fish("jf")
};
}
public void paint(Graphics g){
g.drawImage(background,0,0,null);
for(Fish fish :all){
int x = fish.getX();
int y = fish.getY();
g.drawImage(fish.getImage(),x,y,null);
}
if(fishingNet.isShow()){
Image img = fishingNet.getImage();
int x = fishingNet.getX() - fishingNet.getWidth()/2;
int y = fishingNet.getY() - fishingNet.getHeight()/2;
g.drawImage(img,x,y,null);
}
}
protected void catchFish(){
for(Fish fish:all){
if(fish.catchBy(fishingNet)){
fish.getOut();
}
}
}
public void action()throws Exception{
for(Fish fish:all){
Thread t = new Thread(fish);
t.start();
}
MouseAdapter l = new MouseAdapter(){
public void mousePressed(MouseEvent e) {
catchFish();
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
fishingNet.setX(x);
fishingNet.setY(y);
}
public void mouseEntered(MouseEvent e) {
fishingNet.setShow(true);
}
public void mouseExited(MouseEvent e) {
fishingNet.setShow(false);
}
};
this.addMouseListener(l);
this.addMouseMotionListener(l);
while(true){
repaint();
Thread.sleep(1000/24);
}
}
public static void main(String[] args)throws Exception{
JFrame frame = new JFrame("捕鱼达人");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
Pool pool = new Pool();
frame.add(pool);
frame.setVisible(true);
pool.action();
}
}本文出自 “浪漫的偷笑” 博客,转载请与作者联系!
原文地址:http://lmdtx.blog.51cto.com/6942028/1731946