gameface.java

来自「软件工程实践课程的答案哦」· Java 代码 · 共 556 行

JAVA
556
字号
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class GameFace extends JFrame implements ActionListener
{   /**The size of the playing field*/
	final static int WIDTH = 900;
	final static int HEIGHT = 705;
	
	/**The unit size of a square*/
	final static int unitSize = 28; 
	static Color bgColor = new Color(31,123,95);
	
	/**The thread creating random numbers for creating a bar*/
	NumberCreator numCreator;
	
	Bar bar;
	
	int score;
	boolean hasHost = false;
	boolean gameStart = false;
	boolean gameOver = false;
	/**this array used to record the locations needed to be changed*/
	LOVList lovList = new LOVList();
	
	LOVList[] otherLovList;
	
	Timer timer;
	
	static OtherClientsThread otherThread;
	
	/**The playing field for local client*/
	public SelfPanel selfPanel;
	
	/**The panel for chatting*/
	ChatStartExitPanel chat;
	
	/**The array holing the information of other clients*/
	OtherClients[] otherClients;
	
	/**The panel displaying*/
	PaintNextPanel nextPanel;
	
	/**The name of the client*/
	String clientName;
	
	/**The score of the local client*/
	int ClientScore;
	
	public static Back1 back1;
	public static Begin begin;
	
	JMenuBar menubar = new JMenuBar();
	JMenu game=new JMenu("Game");///////////
	      JMenuItem setUp;///////////
	      JMenuItem join;///////////
	      JMenuItem exit;/////////
	JMenu about=new JMenu("About");/////////////
	      JMenuItem us;/////////////////////
	static GameFace face;//////////////////
	Server server;
	
	/**The constructor*/
	public GameFace()
	{
		begin=new Begin();
		begin.init();
		
		otherLovList = new LOVList[3];
		for(int i=0;i<3;i++)
		{
			otherLovList[i] = new LOVList();
		}
		///////////JFrame settings//////////////
		Container panel = this.getContentPane();
		panel.setLayout(new BorderLayout());
		
		this.setTitle("Columns Server v1.0");
		this.setSize(WIDTH,HEIGHT);
		this.setJMenuBar(menubar);
		/**********************************************************/
		menubar.add(game);
		menubar.add(about);
		setUp=new JMenuItem("Host game");
		join=new JMenuItem("Join game");
		exit=new JMenuItem("Exit");
		us=new JMenuItem("About us");
		game.add(setUp);
		game.add(join);
		game.add(exit);
		about.add(us);
		setUp.addActionListener(this);
		join.addActionListener(this);
		exit.addActionListener(this);
		us.addActionListener(this);
		
		Action memory_key1=new AbstractAction() {
			public void actionPerformed(ActionEvent r)
			{
				server=new Server();
				server.start();
				Name_Input n=new Name_Input(face);
				
			}
		};
		Action memory_key2=new AbstractAction() {
			public void actionPerformed(ActionEvent r)
			{
			    IP_Input i=new IP_Input(face);	
			}
		};
		Action memory_key3=new AbstractAction() {
			public void actionPerformed(ActionEvent r)
			{
				final JOptionPane optionPane=new JOptionPane(
					"Do you really want to have a rest?",JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
			    final JDialog dialog=new JDialog(face,"Think about it again!",true);
			    dialog.setContentPane(optionPane);
			    dialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
			    int option=((Integer)optionPane.getValue()).intValue();
			    if(option==JOptionPane.YES_OPTION)
			    {
			    	System.exit(0);
			    }
			    
			}
		};
		
		setUp.getInputMap().put(KeyStroke.getKeyStroke("F1"),"setUp");
		setUp.getActionMap().put("setUp",memory_key1);
		join.getInputMap().put(KeyStroke.getKeyStroke("F2"),"join");
		join.getActionMap().put("join",memory_key2);
		exit.getInputMap().put(KeyStroke.getKeyStroke("F3"),"exit");
		exit.getActionMap().put("exit",memory_key3);
		
		/**********************************************************/
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(((int)screen.getWidth()-WIDTH)/2,((int)screen.getHeight()-HEIGHT)/2);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		////////////SelfPanel settings/////////
		selfPanel = new SelfPanel();
		selfPanel.canvas.setGameFace(this);
		////////////-----------------//////////
		
		
		////////////ChatPanel settings/////////
		chat = new ChatStartExitPanel(this);
		///////////-------------------//////////
		
		////////////chat && selfPanel/////////
		JPanel selfPanelChat = new JPanel(new BorderLayout());
		selfPanelChat.add(selfPanel,BorderLayout.CENTER);
		selfPanelChat.add(chat,BorderLayout.EAST);
		
		///////////------------------/////////
		
		
		///////////OtherClientsContainer settings///////
		JPanel clientsBorderLayout = new JPanel(new BorderLayout());
		JPanel otherClientsContainer = new JPanel(new GridLayout(2,2,45,38));
		nextPanel = new PaintNextPanel();
		nextPanel.setGameFace(this);
		otherClients = new OtherClients[3];
		for(int i=0;i<3;i++)
		{
			otherClients[i] = new OtherClients();
			otherClients[i].ocp.set(otherLovList[i]);
		}
		
		otherClientsContainer.add(otherClients[0]);
		otherClientsContainer.add(nextPanel);
		otherClientsContainer.add(otherClients[1]);
		otherClientsContainer.add(otherClients[2]);
		otherClientsContainer.setBackground(bgColor);
		clientsBorderLayout.add(otherClientsContainer,BorderLayout.CENTER);
		clientsBorderLayout.add(new JLabel("                                                                                                                      "),BorderLayout.NORTH);
		clientsBorderLayout.add(new JLabel(" "),BorderLayout.WEST);
		clientsBorderLayout.add(new JLabel(" "),BorderLayout.SOUTH);
		clientsBorderLayout.setBackground(GameFace.bgColor);
		///////////------------------------------////////
		
		
		panel.add(clientsBorderLayout,BorderLayout.WEST);
		panel.add(selfPanelChat,BorderLayout.CENTER);
	
		this.setVisible(true);
		
	}	
	
	//for selfPanel
	/**Coloring the specified bar on the main playing field
	 * @param bar the specific bar to be colored
	 */
	public synchronized void set(Bar bar)
	{
		
		for(int i=0;i<3;i++)
		{
			lovList.add(new LOV(bar.squares[i].x,bar.squares[i].y,bar.squares[i].color));
		}
		selfPanel.canvas.repaint();
		
	}
	
	//for selfPanel
	/**Coloring the specified location on the main playing field
	 * @param location the location to be colored
	 */
	public synchronized void set(Location location)
	{
		
		lovList.add(new LOV(location.x,location.y,location.color));
		
		selfPanel.canvas.repaint();
		
		
	}
	//for clients
	/**Coloring the specified location on the  playing field of another client specified by the id
	 * @param location the location to be colored
	 * @param id the id of another client
	 */
	public  void set(Location location,int id)
	{ 
		
		otherLovList[id-1].add(new LOV(location.x,location.y,location.color));
		

		otherClients[id-1].ocp.repaint();
	
    }
    
	//for selfPanel
  	/**Clearing the specified bar on the main playing field
  	 * @param bar the bar to be cleared
  	 */
  	public synchronized void clear(Bar bar)
	{
		for(int i=0;i<3;i++)
		{
			lovList.del(new LOV(bar.squares[i].x,bar.squares[i].y,bar.squares[i].color));
		}
		selfPanel.canvas.repaint();
	
	}
	//for selfPanel
  	/**Clearing the specified location on the main playing field
  	 * @param location the location to be cleared
  	 */
  	public synchronized void clear(Location location)
	{
	
		
		lovList.del(new LOV(location.x,location.y,location.color));
		
		selfPanel.canvas.repaint();
	
	}
	//for clients
  	/**Coloring the specified location on the  playing field of another client specified by the id
	 * @param location the location to be cleared
	 * @param id the id of another client
	 */
	public synchronized void clear(Location location,int id)
	{  
		otherLovList[id-1].del(new LOV(location.x,location.y,location.color));
		
	    otherClients[id-1].ocp.repaint();
    }
    
    
    /**Display the playing field of another client
     * @param s the playing field informtaion encapsulated in a string
     * @param id the id of another client
     */
    public synchronized void displayPlayingField(String s,int id)
    {   
    
        otherLovList[id-1].clear();
        
    	StringTokenizer token=new StringTokenizer(s);
    	
    	String X=new String();
    	String Y=new String();
    	String COLOR=new String();
    	
    	int x=0;
    	int y=0;
    	int color=0;
    	
    	int i=0;
    	
    	while(token.hasMoreTokens())
    	{
    		X=token.nextToken();
    		Y=token.nextToken();
    		COLOR=token.nextToken();
    		
    		x=Integer.valueOf(X).intValue();
    		y=Integer.valueOf(Y).intValue();
    		color=Integer.valueOf(COLOR).intValue();
    		
    		otherLovList[id-1].add(new LOV(x,y,color));
    		
    		
    	}
    	
    	otherClients[id-1].ocp.repaint();
    	
    }
    
    
    /**Display the score of the local client
     * @param score the score of the local client
     */
    public void setScore(int score)
    {
    	this.score = score;
    	this.selfPanel.selfInfo.setScore(score);
    
    }
    
    
    /**Display the score of another client
     * @param score the score of another client
     * @param id the id of another client
     */
    public void setScore(int score,int id)
	{  
	   this.otherClients[id-1].setScore(score);
	}
	
	
	/** Display the name of the local client
	 * @param the name of the local client
	 */
	public void setName(String name)
	{
		this.clientName = name;
		this.selfPanel.selfInfo.setName(name);
	
	}
	
	
	/**Display the name of another client
	 * @param name the name of another client
	 * @param id the id of another client
	 */
	public void setName(String name,int id)
	{
		this.otherClients[id-1].setName(name);
	}
	
	public void gameOver()
	{   System.out.println("GameOverGameOverGameOver");
		if(GamePool.isFull())
		{
			Lost lost=new Lost();
			lost.init();
			lost.start();
			lost=null;
			this.bar = null;
			this.gameOver = true;
			this.gameStart = false;
			selfPanel.canvas.repaint();
			this.timer = null;
			this.numCreator = null;
		
			Communication.IamOver();
		
		}
	}

	/** the game is over
	 * 
	 */
	public void endGame()
	{
		back1.back1.stop();
		
		
		this.bar = null;
		this.gameOver = true;
		this.gameStart = false;
		selfPanel.canvas.repaint();
		this.timer = null;
		this.numCreator = null;
		
		
	}
    
    
    /**Display the place of the local client
     * @param order the place of the local client
     */
    public void displayOrder(int order)
    {   
    	selfPanel.canvas.setOrder(order);
    	
    	selfPanel.canvas.repaint();
    }
	
	/**Initial the game
	 * 
	 */
	public void initGame()
	{
		if(this.gameOver)
		{
			GamePool.init();
			this.setScore(0);
			for(int i=0;i<3;i++)
				{
					otherClients[i].setScore(0);
					otherClients[i].setName("");
				}
			lovList.clear();
			selfPanel.canvas.repaint();
			this.hasHost = false;
		}
		
	}
	
	public void startGame()
	{  System.out.println("GameFace.startGame()");
		if(!gameStart)
			{
				System.out.println("ok234");
				begin.begin.stop();
				ReadyGo readyGo = new ReadyGo();
				readyGo.init();
				readyGo.start();
				
				back1=new Back1();
				back1.init();
				back1.start();
				System.out.println("ok8907");
				this.lovList = new LOVList();
				this.numCreator = new NumberCreator();
				this.numCreator.numCreatorStart();
				System.out.println("NumCreator started!");
				this.initGame();//gameface, gamepool,lovList all init
				timer = new Timer(this);
				System.out.println("Timer started!");
				this.gameStart = true;
				this.gameOver = false;
				timer.start();
				selfPanel.canvas.requestFocus();
			}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand()=="Start")
		{
			if(hasHost)
			{
				Communication.ClientReady("Ready");
				selfPanel.canvas.requestFocus();
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Host or Join Game first");
			}
			
		
		}
	
		else if(e.getActionCommand()=="Exit")
		{
			System.exit(0);
		}
		
		if(e.getSource()==setUp)
		{
			hasHost = true;
			server=new Server();
			server.start();
			IP_Input i=new IP_Input(face);
		}
	    else if(e.getSource()==join)
	    {
	    	hasHost = true;
	    	IP_Input i=new IP_Input(face);
	    }
		else if(e.getSource()==exit)
		{
			System.exit(0);
		}
		else if(e.getSource()==us)
		{
			About about=new About();
			about.run();
		}
	}

	
	/**Create a bar according to a ColorSet object
	 * @param colors the colored bar to be created
	 * @param next the next bar to be created
	 */
	public  void createBar(ColorSet colors,ColorSet next)
	{   System.out.println("Create Bar");
		
		nextPanel.setColorSet(next);
		nextPanel.repaint();
		
		Location[] locations = new Location[3];
		locations[0] = new Location(0,0,colors.getColor0());
		locations[1] = new Location(1,0,colors.getColor1());
		locations[2] = new Location(2,0,colors.getColor2());
		bar = new Bar(locations);

		this.set(bar);
		selfPanel.canvas.repaint();
		Communication.set(locations[0]);
		Communication.set(locations[1]);
		Communication.set(locations[2]);
		
	}

	/**Set the place of another client
	 * @param order the place of another client
	 * @param id the id of another client
	 */
	public void setOrder(int order,int id)
	{    
		this.otherClients[id-1].setOrder(order);
		if (this.selfPanel.canvas.order==1)
		{
			Win win=new Win();
			win.init();
			win.start();
		}
		else
		{
			Lost lost=new Lost();
			lost.init();
			lost.start();
			lost=null;
		}
		
	}
	
	public static void main(String[] args)
	{   Queue queue=new Queue();
		face = new GameFace();
		GamePool gamePool=new GamePool(face);
		GameFace.otherThread=new OtherClientsThread(face);
		GameFace.otherThread.start();
	
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?