类的设计
Pool 继承 JPanel
background
Fish[] allFish
FishingNet fishingNet
Fish
x
y
width
height
p_w_picpaths
index
p_w_picpath
FishingNet
x
y
width
height
p_w_picpath
素材在最下面:ps技术有限 不喜欢的可以自己画!!!
package xyz.rhel.fish;import java.awt.p_w_picpath.BufferedImage;import java.io.File;import javax.p_w_picpathio.ImageIO;public class FishingNet { private int x; private int y; private int width; private int height; private BufferedImage p_w_picpath; private boolean show; public FishingNet(String img) throws Exception { p_w_picpath = ImageIO.read(new File(img)); width = p_w_picpath.getWidth(); height = p_w_picpath.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 p_w_picpath; } public void setImage(BufferedImage p_w_picpath) { this.p_w_picpath = p_w_picpath; } public boolean isShow(){ return show; } public void setShow(boolean show){ this.show = show; } }
package xyz.rhel.fish;import java.awt.p_w_picpath.BufferedImage;import java.io.File;import java.util.Random;import javax.p_w_picpathio.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[] p_w_picpaths; private BufferedImage p_w_picpath; 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 p_w_picpaths; } public void setImages(BufferedImage[] p_w_picpaths) { this.p_w_picpaths = p_w_picpaths; } public BufferedImage getImage() { return p_w_picpath; } public void setImage(BufferedImage p_w_picpath) { this.p_w_picpath = p_w_picpath; } public Fish(String perfix) throws Exception { p_w_picpaths = new BufferedImage[10]; for (int i = 0; i < 10; i++) { String file = perfix + i + ".png"; p_w_picpaths[i] = ImageIO.read(new File(file)); } p_w_picpath = p_w_picpaths[0]; width = p_w_picpath.getWidth(); height = p_w_picpath.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(); } p_w_picpath = p_w_picpaths[index++%p_w_picpaths.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.p_w_picpathio.ImageIO.read;import java.awt.Graphics;import java.awt.Image;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;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 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(); }}