package t7;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;
public class FishPanel extends JPanel {
int count=0; //计时数字
Random rand = new Random();
public static Fish myFish;
Fish btn_start;
//获取所有的系统鱼集合
public static List<Fish> fishList = FishUtil.getFishList(null);
public static boolean isStart=false ;
@Override
protected void paintComponent(Graphics g) {
if(isStart){
drawBg(g);
drawMyFish(g);
drawSysFish(g);
drawScore(g);
repaint();
}else{
drawStart(g);
repaint();
}
}
/**
* 画开始图片
*/
private void drawStart(Graphics g) {
// 画出下部分背景图片
g.drawImage(FishUtil.IMAGES.get("haidishijie.jpg"), 0, 0, 1024, 673, this);
BufferedImage img = FishUtil.IMAGES.get("btn_play.png").getSubimage(0, 0, 179, 75);
g.drawImage(img, (FishUtil.WINDOW_WIDTH - 179) / 2, (FishUtil.WINDOW_HEIGHT - 75) / 2, this);
btn_start = new Fish();
btn_start.setX((FishUtil.WINDOW_WIDTH - 179) / 2);
btn_start.setY((FishUtil.WINDOW_HEIGHT - 75) / 2);
btn_start.setW(img.getWidth());
btn_start.setH(img.getHeight());
}
/**
* @param g
*/
private void drawBg(Graphics g) {
// 获得背景图片
BufferedImage bg = FishUtil.IMAGES.get("backgroundpicture.jpg");
// 画背景图,根据背景图的宽高,可以缩放
g.drawImage(bg, 0, 0, bg.getWidth(), bg.getHeight(), this);
}
/**
* @param g
*/
private void drawMyFish(Graphics g) {
// 画自己的鱼
//画player的myFish的初始图片
BufferedImage cutImage = null;
//System.out.println(myFish.getIndex());
//BufferedImage cutImage = image.getSubimage(myFish.getIndex()*myFish.getW(), 0, myFish.getW(), myFish.getH());
if(myFish.getIndex()>=myFish.getMaxIndex()){
//截取源图片里的某一段,变成一条完整的鱼
cutImage = myFish.getImg().getSubimage(myFish.getW() * (myFish.getIndex()-2), 0, myFish.getW(),
myFish.getH());
}else{
//System.out.println(myFish.getW());
//System.out.println(myFish.getIndex());
cutImage = myFish.getImg().getSubimage(myFish.getW() * myFish.getIndex(), 0, myFish.getW(),
myFish.getH());
}
//处理镜像图片
if(myFish.getState().equals(FishUtil.STATE_SWIM)&&myFish.getDirection()==FishUtil.DIRECTION_RIGHT){
cutImage = FishUtil.getOppoImage(cutImage);
}
if(myFish.getState().equals(FishUtil.STATE_EAT)&&myFish.getDirection()==FishUtil.DIRECTION_RIGHT){
cutImage = FishUtil.getOppoImage(cutImage);
}
if(myFish.getState().equals(FishUtil.STATE_TURN)&&myFish.getDirection()==FishUtil.DIRECTION_LEFT){
cutImage = FishUtil.getOppoImage(cutImage);
}
g.drawImage(cutImage,myFish.getPosition().x,myFish.getPosition().y,myFish.getW()/3,myFish.getH()/3,this);
//在鱼上画边框
g.setColor(Color.red);
g.drawRect(myFish.getHurtArea().x, myFish.getHurtArea().y, myFish.getHurtArea().width, myFish.getHurtArea().height);
}
/**
* @param g
*/
private void drawSysFish(Graphics g) {
for (Fish sysFish : fishList) {
// 截取鱼图片中的某一段
BufferedImage subImg = null;
//如果当前读的鱼图片的索引,大于或等于最大的索引值
if(sysFish.getIndex()>=sysFish.getMaxIndex()){
//截取源图片里的某一段,变成一条完整的鱼
subImg = sysFish.getImg().getSubimage(sysFish.getW() * (sysFish.getIndex()-2), 0, sysFish.getW(),sysFish.getH());
}else{
System.out.println(sysFish.getNum());
System.out.println(sysFish.getImg().getWidth());
System.out.println(sysFish.getW());
System.out.println(sysFish.getIndex());
System.out.println("11111111111111111111");
subImg = sysFish.getImg().getSubimage(sysFish.getW() * sysFish.getIndex(), 0, sysFish.getW(),sysFish.getH());
}
//根据方向判断是要镜像
if(sysFish.getState().equals(FishUtil.STATE_SWIM)&&sysFish.getDirection() == FishUtil.DIRECTION_RIGHT){
subImg = FishUtil.getOppoImage(subImg);
}
if(sysFish.getState().equals(FishUtil.STATE_TURN)&&sysFish.getDirection()==FishUtil.DIRECTION_LEFT){
subImg = FishUtil.getOppoImage(subImg);
}
//所有截取的图片在游戏第一关都除以3缩放了
//后面关卡如果升级,可以再设置系数,让显示在面板上的图片增大
g.drawImage(subImg, sysFish.getPosition().x, sysFish.getPosition().y, sysFish.getW()/3, sysFish.getH()/3, this);
//显示野鱼区域的框
g.setColor(Color.yellow);
g.drawRect(sysFish.getHurtArea().x, sysFish.getHurtArea().y, sysFish.getHurtArea().width,sysFish.getHurtArea().height);
}
}
/**
* @param g
*/
private void drawScore(Graphics g) {
//在面板的左上角绘制当前玩家鱼的分数
g.setColor(Color.red);
g.setFont(new Font("黑体",Font.BOLD|Font.ITALIC,50));
g.drawString(myFish.getScore()+"", 80, 80);
// 读取所有的系统鱼集合,画系统的鱼
}
/**
* 鱼移动的方法
*/
public void fishMove(){
if( myFish.getIndex() == myFish.getMaxIndex()){
//当玩家鱼处于Eat状态,图片也放到了最后一张,应马上转换到Swim状态
if(myFish.getState().equals(FishUtil.STATE_EAT)){
myFish.setState(FishUtil.STATE_SWIM);
}
//当玩家鱼处于Turn状态,图片到了最后一张,应马上转为Swim
if(myFish.getState().equals(FishUtil.STATE_TURN)){
myFish.setState(FishUtil.STATE_SWIM);
}
myFish.setIndex(0);
}else{
//否则,索引往后加
int index = myFish.getIndex() + 1;
myFish.setIndex(index);
}
//改变每条野鱼的Index
for (Fish sysFish : fishList){
if(sysFish.getIndex()==sysFish.getMaxIndex()){
//已经放映到最后一张图片时,需要判断状态
//若是turn状态,必须回到swim状态
if(sysFish.getState().equals(FishUtil.STATE_TURN)){
sysFish.setState(FishUtil.STATE_SWIM);
}
sysFish.setIndex(0);
}else{
//否则,索引往后加
int index = sysFish.getIndex() + 1;
sysFish.setIndex(index);
}
//还需改变每条鱼的point
//f.setPoint(new Point(f.getPoint().x,f.getPoint().y-10));
//游动到目的地,x,y分别需要移动的距离
int dx = sysFish.getDestination().x-sysFish.getPosition().x;
int dy = sysFish.getDestination().y-sysFish.getPosition().y;
//真实距离
double d = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
//x,y 相对d 的比率
double rateX = dx/d;
double rateY = dy/d;
//每0.1秒在x,与y轴移动的距离=比率*速度
int sx = (int)(rateX * FishUtil.getSysFish().getSpeed());
int sy = (int)(rateY * FishUtil.getSysFish().getSpeed());
//获取移动后鱼的坐标
int newX = sysFish.getPosition().x+sx;
int newY = sysFish.getPosition().y+sy;
if(Math.abs(sysFish.getDestination().x-newX)<=5){
//已经到达了目的地
//产生新目的地
int x = rand.nextInt(FishUtil.WINDOW_WIDTH-sysFish.getW());
int y = rand.nextInt(FishUtil.WINDOW_HEIGHT-sysFish.getH());
sysFish.setDestination(new Point(x,y));
//产生了新目标,又有可能需要转变方向
//f.setDirection(GameUtil.getDirectionByPoint(new Point(newX,newY),f.getTarget()));
sysFish.setDirection(FishUtil.getDirectionByPoint(new Point(newX,newY),sysFish.getDestination()));
}
//移动之后,将鱼设置到新位置
sysFish.setPosition(new Point(newX,newY));
}
}
/**
*
* 描述:普通内部类继承线程,不断的调用移动的方法 作者:XF 2015年12月30日上午10:55:43
*/
public class FishThread extends Thread {
public void run() {
if(!isStart){
//FishUtil.playMusic(true, FishUtil.AUDIOS.get("2.mid"));
}
while(true){
System.out.println();