package Pak;
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.applet.AudioClip;
import java.applet.*;
import java.applet.Applet.*;
import java.net.*;
import java.io.*;
import sun.audio.*;
import sun.audio.AudioPlayer;
public class pig
{
int stInt=5;
int langMove=3;
int guan=1;
int longInt=1;
int sX=0,sY=0;
int lX=0,lY=0;
int a = 0;
int k=1;
int turn;
int move=0;
int seconds=0;
int timeSeconds=0;
int count=0;
int i,j;
int x=250,y=250;
int langX,langY;
int stoneX,stoneY;
String strUser;
//******************登录窗口
JFrame frameLog;
JPanel panelLog;
JLabel labelUser;
JTextField textUser;
JButton buttonLog;
//*************************
Random rand;
JFrame frame;
JLabel labelTitle;
JPanel panelTotal;
JPanel panelNorth;
JPanel panelSouth;
JPanel panelCenter;
JLabel labelPig;
JLabel labelStone;
JLabel labelLang;
JLabel labelBar;
JLabel StoneXY[]=new JLabel[25];
JLabel Bars[]=new JLabel[stInt];
JLabel labelTime;
JLabel labelNum;
JButton buttonStart;
JButton buttonReStart;
JButton buttonPause ;
JButton buttonContinue ;
JButton buttonSave;
JButton buttonLook;
AudioStream play_music;
AudioClip music;
//相对路径
String pictureAddress = System.getProperty("user.dir");
Icon pigUp1=new ImageIcon(pictureAddress+"\\image\\pigUp1.gif");
Icon pigDown1=new ImageIcon(pictureAddress+"\\image\\pigDown1.gif");
Icon pigLeft1=new ImageIcon(pictureAddress+"\\image\\pigLeft1.gif");
Icon pigRight1=new ImageIcon(pictureAddress+"\\image\\pigRight1.gif");
Icon pigUp2=new ImageIcon(pictureAddress+"\\image\\pigUp2.gif");
Icon pigDown2=new ImageIcon(pictureAddress+"\\image\\pigDown2.gif");
Icon pigLeft2=new ImageIcon(pictureAddress+"\\image\\pigLeft2.gif");
Icon pigRight2=new ImageIcon(pictureAddress+"\\image\\pigRight2.gif");
Icon stone=new ImageIcon(pictureAddress+"\\image\\stone.gif");
Icon lang=new ImageIcon(pictureAddress+"\\image\\狼.jpg");
Icon bar=new ImageIcon(pictureAddress+"\\image\\石头.jpg");
public pig()
{
classMain();
new Audio_play().start();
}
public void classMain()
{
//***************************************登录窗口
frameLog=new JFrame("登录窗口");
panelLog=new JPanel();
labelUser=new JLabel("请输入用户名:");
textUser=new JTextField(10);
buttonLog=new JButton("登录");
buttonLog.setForeground(Color.blue);
frameLog.getContentPane().add(panelLog);
panelLog.add(labelUser);
panelLog.add(textUser);
panelLog.add(buttonLog);
buttonLog.setFocusable(false);
//******************************************
rand=new Random();
frame=new JFrame("小猪吃豆");
labelTitle=new JLabel();
labelTitle.setForeground(Color.blue);
labelNum=new JLabel();
buttonStart=new JButton("开始");
buttonReStart=new JButton();
buttonPause=new JButton("暂停");
buttonContinue=new JButton("继续");
buttonLook=new JButton("查看记录");
buttonSave=new JButton("保存分数");
buttonStart.setForeground(Color.red);
buttonPause.setForeground(Color.pink);
buttonContinue.setForeground(Color.pink);
buttonLook.setForeground(Color.blue);
buttonSave.setForeground(Color.blue);
panelTotal=new JPanel();
panelNorth=new JPanel();
panelSouth=new JPanel();
panelCenter=new JPanel();
labelPig=new JLabel(pigLeft1);
labelTime=new JLabel();
labelLang=new JLabel(lang);
frame.getContentPane().add(panelTotal);
panelNorth.add(labelTitle);
panelCenter.add(labelPig);
panelCenter.add(labelLang);
panelCenter.setBackground(Color.white);
panelSouth.add(labelTime);
panelSouth.add(buttonStart);
panelSouth.add(buttonPause);
buttonPause.setVisible(false);
panelSouth.add(buttonContinue);
buttonContinue.setVisible(false);
panelSouth.add(buttonLook);
panelSouth.add(buttonSave);
buttonSave.setVisible(false);
panelSouth.add(buttonReStart);
buttonReStart.setVisible(false);
panelSouth.add(labelNum);
//释放buttonStart焦点
buttonStart.setFocusable(false);
buttonPause.setFocusable(false);
buttonContinue.setFocusable(false);
buttonLook.setFocusable(false);
buttonSave.setFocusable(false);
buttonReStart.setFocusable(false);
panelTotal.setLayout(new BorderLayout());
panelTotal.add(panelNorth,BorderLayout.NORTH);
panelTotal.add(panelSouth,BorderLayout.SOUTH);
panelTotal.add(panelCenter,BorderLayout.CENTER);
//监听开始按钮
MyListener listen=new MyListener();
buttonStart.addActionListener(listen);
buttonPause.addActionListener(listen);
buttonContinue.addActionListener(listen);
buttonSave.addActionListener(listen);
buttonLook.addActionListener(listen);
buttonReStart.addActionListener(listen);
//***********************
buttonLog.addActionListener(listen);
frame.addKeyListener(new Keylisten());
panelCenter.setLayout(null);
labelPig.setBounds(x,y,pigLeft1.getIconWidth(),pigLeft1.getIconHeight());//初始化小猪的位置
//随机产生石头的坐标
for(i=0;i<stInt;i++)
{
int barX,barY;
barX=rand.nextInt(460);
barY=rand.nextInt(460);
for(j=0;j<i;j++)
{
//不让石头覆盖小猪和不让石头重叠
while(((barX>(x-150)&&barX<(x+60))&&(barY>(y-180)&&barY<(y+60)))||((barX>=(Bars[j].getX()-50)&&barX<=(Bars[j].getX()+60))&&(barY>=(Bars[j].getY()-50)&&barY<=(Bars[j].getY()+55))))
{
barX=rand.nextInt(460);
barY=rand.nextInt(460);
}
}
labelBar=new JLabel(bar);
panelCenter.add(labelBar);
labelBar.setBounds(barX,barY,bar.getIconWidth(),bar.getIconHeight());
Bars[i]=labelBar;
}
//随机产生25个豆的坐标
for(i=0;i<25;i++)
{
stoneX=rand.nextInt(400);
stoneY=rand.nextInt(400);
for(int j=0;j<stInt;j++)
{
//不让豆产生在小猪上和石头上
while(((stoneX>(x-50)&&stoneX<(x+50))&&(stoneY>(y-50)&&stoneY<(y+100)))||((stoneX>(Bars[j].getX()-50)&&stoneX<(Bars[j].getX()+50))&&(stoneY>(Bars[j].getY()-60)&&stoneY<(Bars[j].getY()+60))) )
{
stoneX=rand.nextInt(400);