⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guinterface.java

📁 手机无线网络纸牌游戏源代码。非常适合学习使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	{
		displayCards = true;
		
		int count = 0;
		int index = 0;
		
		while (hand[count++] != 9999)
			index++;
		hearts = new CardImage[index];

		index = 0;
		while (hand[count++] != 9999)
			index++;
		clubs = new CardImage[index];

		index = 0;
		while (hand[count++] != 9999)
			index++;
		diamonds = new CardImage[index];

		index = 0;
		while (hand[count++] != 9999)
			index++;
		spades = new CardImage[index];

		count = 0;

		String st = "";
		
		for (int i = 0; i < hearts.length; i++)	
		{
			if (hand[count] == 11)
					st = "J";
				else if (hand[count] == 12)
					st = "Q";
				else if (hand[count] == 13)
					st = "K";
				else if (hand[count] == 14)
					st = "A";		
				else
					st = String.valueOf(hand[count]);
		
			hearts[i] = new CardImage(heartImages[(hand[count++]-2)], "H", st);
		}
		count++;
			
		for (int i = 0; i < clubs.length; i++)
		{
			if (hand[count] == 11)
					st = "J";
				else if (hand[count] == 12)
					st = "Q";
				else if (hand[count] == 13)
					st = "K";
				else if (hand[count] == 14)
					st = "A";		
				else
					st = String.valueOf(hand[count]);
			clubs[i] = new CardImage(clubImages[(hand[count++]-2)], "C", st);
		}
		count++;
						
		for (int i = 0; i < diamonds.length; i++)
		{
			if (hand[count] == 11)
					st = "J";
				else if (hand[count] == 12)
					st = "Q";
				else if (hand[count] == 13)
					st = "K";
				else if (hand[count] == 14)
					st = "A";		
				else
					st = String.valueOf(hand[count]);
			diamonds[i] = new CardImage(diamondImages[(hand[count++]-2)], "D", st);	
		}	
		count++;
			
		for (int i = 0; i < spades.length; i++)
		{
			if (hand[count] == 11)
					st = "J";
				else if (hand[count] == 12)
					st = "Q";
				else if (hand[count] == 13)
					st = "K";
				else if (hand[count] == 14)
					st = "A";		
				else
					st = String.valueOf(hand[count]);
			spades[i] = new CardImage(spadeImages[(hand[count++]-2)], "S", st);
		}
	
		for (int i = 0; i < hearts.length; i++)
			hearts[i].addMouseListener(this);
		for (int i = 0; i < clubs.length; i++)
			clubs[i].addMouseListener(this);
		for (int i = 0; i < diamonds.length; i++)
			diamonds[i].addMouseListener(this);	
		for (int i = 0; i < spades.length; i++)	
			spades[i].addMouseListener(this);	
	}
			
	/**
	 * Called by the game to indicate an event has happened
	 * @param gameEvent Incoming event
	 */			
	public void gameEventReceived(GameEvent event)
	{		
		dataReceived = event.getData();
		try
		{
			System.out.println("Trying to interpret GameEvent data.");
			processData();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Could not interpret GameEvent.");
			System.exit(0);
		}
	}
	
	private String getMyIPAddress()
	{
		String s = "";
		char[] c;
		try
		{
			c = InetAddress.getLocalHost().toString().toCharArray();
			int i = 0;
			while (c[i] != '/')
				i++;
			i++;
			char[] c2 = new char[c.length - i];
			for (int j = 0; j < c2.length; j++)
				c2[j] = c[j + i];
			s = String.copyValueOf(c2);
		}
		catch (UnknownHostException uhe)
		{
			System.out.println("Could not get my IP address: " + uhe.getMessage());
			System.exit(1);
		}	
		
		return s;	
	}		
	
	
	/** Starts GUI, loads all the card images. */
	public GUInterface(Panel p, Whist w) 
	{	
		game = new Game();
		whistRef = w;
		panel1 = p;
		panel1.setLayout(new BorderLayout());		
		cardPanel = new Panel();		
		
		cardPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		northPanel = new Panel();
		northPanel.setLayout(new FlowLayout());
		
			
		for (int i = 0; i < 5; i++)
			panelArray[i] = new PanelWithStruts();

		tracker = new MediaTracker(whistRef);

		String[] desc = { "Whist v0.01", "05/09/2000", "(c) Copyright Symbian Ltd", "http://www.symbiandevnet.com"};
		whistRef.createAboutDialog(desc);
		
		yesButton = new Button("Yes");
		noButton = new Button("No");
		
		whistRef.score.addActionListener(this);
		whistRef.end.addActionListener(this);
		yesButton.addActionListener(this);
		noButton.addActionListener(this);
				
		loadImages();
		
		cardTableGreen = new Color(70, 140, 60);
		trumpGreen = new Color(70, 180, 60);
		currentColor = new Color(200, 0, 20);

		panel1.validate();
		whistRef.setVisible(true);
	}
	
	/** 
	 * Loads the images from file and adds them to the MediaTracker.
	 */ 
	private void loadImages()
	{
		Toolkit t = Toolkit.getDefaultToolkit();
		
		for (int i = 0; i < 13; i ++)
		{	
			heartImages[i] = t.getImage(heartImageNames[i]);
			clubImages[i] = t.getImage(clubImageNames[i]);
			diamondImages[i] = t.getImage(diamondImageNames[i]);
			spadeImages[i] = t.getImage(spadeImageNames[i]);
			tracker.addImage(heartImages[i], 0);
			tracker.addImage(clubImages[i], 0);
			tracker.addImage(spadeImages[i], 0);
			tracker.addImage(diamondImages[i], 0);		
		}	

		try
		{
			tracker.waitForAll();
		}
		catch(InterruptedException ie) 
			{System.out.println("Caught MediaTracker ie exception: " + ie.getMessage());}	
	}
		
	/** Processes ActionEvents. 
	 * @param ae an ActionEvent. 
	 */
	public void actionPerformed(ActionEvent ae)
	{
		try
		{
			if (ae.getSource().equals(whistRef.score))
			{
				int[] score = game.getScore();
				String[] temp = new String[4];
				for (int i = 0; i < names.length; i++)
					temp[i] = (names[i] + ": " + score[i]);		
				GameDialog scoreDialog = new GameDialog(whistRef, "Score", temp);
				scoreDialog.setVisible(true);
			}
			
			else if (ae.getSource().equals(whistRef.end))
			{
				int[] winners = game.getWinner();
				String[] array = new String[5];
				array[0] = (names[0]+ " & " + names[2]); 
				array[1] = (" have won " + winners[0] + " games.");
				array[2] = (names[1]+ " & " + names[3]);
				array[3] = (" have won " + winners[1] + " games.");
				array[4] = ("Thank you for playing Whist.");
				endGame = new GameDialog(whistRef, "End of Game", array);
				endGame.setVisible(true);
				game.quitGame();
			
				synchronized(game.lock)
				{			
					game.lock.notifyAll();		
				}
				whistRef.showSMSBaseView();
			}

			else if (ae.getSource().equals(noButton))
			{
				String end = "n";
				
				String[] array = {"Thank you for playing Whist."};
				gameEnded = new GameDialog(whistRef, "End of Game", array);
				gameEnded.setVisible(true);
				game.startNewGame = false;
				
				synchronized(game.lock)
				{
					game.lock.notify();		
				}
				System.exit(0);
			}
			else if (ae.getSource().equals(yesButton))
			{
				String end = "y";
				
				String[] array = {"Starting new game."};
				gameEnded = new GameDialog(whistRef, "New Game", array);
				gameEnded.setVisible(true);
				game.startNewGame = true;
				panel1.removeAll();
				
				synchronized(game.lock)
				{
					game.lock.notify();		
				}	
			}		
		}
		catch (Exception e)
		{
			System.out.println("oops!");
			e.printStackTrace();
			System.exit(0);
		}	
	}

	/** 
	 * Clears all <code>Panel</code>s and adds the required components to them.
	 */
	private void redrawPanel() 
	{ 	
		whistRef.showGUI();
		for (int i = 0; i < 5; i++)
			panelArray[i].removeAll();
	
		cardPanel.removeAll();	
		panel1.removeAll();			
		if (!setName)
		{	
			int index = game.getPlayerNumber();
			String name = names[index-1];
			playerName = new Label(name, Label.CENTER);
			
			northPanel.add(playerName);
			setName = true;		
			boldFont = new Font("boldFont", Font.BOLD, 10);	
			normalFont = new Font("normalFont", Font.PLAIN, 10);
		}
		if (myTurn)
			playerName.setFont(boldFont);
		else 
			playerName.setFont(normalFont);	
		
		if (displayCards)
		{						
			int arrayLength = hearts.length;
			int count = 0;			
			
			for (int i = 0, len = hearts.length; i < len; i++)
			{						
				panelArray[1].add(hearts[i]);
			}
					
			for (int i = 0, len = diamonds.length; i < len; i++)	
				panelArray[2].add(diamonds[i]);

			for (int i = 0, len = clubs.length; i < len; i++)
				panelArray[3].add(clubs[i]);

			for (int i = 0, len = spades.length; i < len; i++)
				panelArray[0].add(spades[i]);			
		}
		if (bcurrent)
		{
			for (int i = 0; i < currentValues.length; i++)
				panelArray[4].add(currentValues[i]);
			panelArray[4].setBackground(currentColor);
		}

		displayCards = false;	
		bcurrent = false;
		cardPanel.setBackground(cardTableGreen);

		for (int i = 0; i < 5; i++)						
			cardPanel.add(panelArray[i]);
			
		panel1.add(northPanel, BorderLayout.NORTH);
		panel1.add(cardPanel, BorderLayout.CENTER);		
		whistRef.validate();
	
		whistRef.pack();
	}
	
	/** 
	 * Processes <code>MouseClicked</code> events.
	 * @param me a <code>MouseClicked</code> event
	 */
	public void mouseClicked(MouseEvent me)
	{
		if (myTurn)
		{
			CardImage cardImage = (CardImage)me.getSource();
		 	String rank = cardImage.getCardRank();
			String suit = cardImage.getCardSuit();		
			
			System.out.println("click on: " + me.paramString());
			
			boolean valid = game.checkCardIsValid(suit, rank);											
			if (!valid)
			{				
				illegalMove[0] =  "This is not a legal move.";
				illegalMove[1] = "Please select another card.";
				GameDialog cardNotValid = new GameDialog(whistRef, "Illegal move", illegalMove);
				cardNotValid.setVisible(true);
				valid = game.checkCardIsValid(suit, rank);
			}
			if (valid)
			{	
				myTurn = false;
				synchronized(game.lock)
				{
					game.lock.notify();		
				}	
				myHand = game.getHand();
				showCards(myHand);
				redrawPanel();								
			}
		}
	}
	
	/** 
	 * Processes <code>MouseEntered</code> events.  Due to the pen-based
	 * nature of the devices, <code>MouseEntered</code> events do not 
	 * exist.  The method therefore has no implementation
	 * @param me a <code>MouseEntered</code> event
	 */
	public void mouseEntered(MouseEvent me)
	{}
	
	/** 
	 * Processes <code>MouseExited</code> events.  Due to the pen-based
	 * nature of the devices, <code>MouseExited</code> events do not 
	 * exist.  The method therefore has no implementation
	 * @param me a <code>MouseExited</code> event
	 */
	public void mouseExited(MouseEvent me)
	{}
	
	/** 
	 * Processes <code>MousePressed</code> events.  Due to the pen-based
	 * nature of the devices, <code>MousePressed</code> events do not 
	 * exist.  The method therefore has no implementation
	 * @param mp a <code>MousePressed</code> event
	 */
	public void mousePressed(MouseEvent mp)
	{}
	
	/** 
	 * Processes <code>MouseReleased</code> events.  Due to the pen-based
	 * nature of the devices, <code>MouseReleased</code> events do not 
	 * exist.  The method therefore has no implementation
	 * @param mr a <code>MouseReleased</code> event
	 */
	public void mouseReleased(MouseEvent mr)
	{}
	
	/** 
	 * Calls the {@link com.symbian.devnet.whist.game.Game#setUpSMS() setUpSMS}
	 * method to initiate an SMS Receiver.
	 */
	public void setUpSMS()
	{
		game.setUpSMS();
	}
	
	/**
	 * Calls the {@link com.symbian.devnet.whist.game.Game#startGame() startGame}
	 * method to start a game.
	 * @param names the array of names of the people invited to play the game
	 * @param addresses the array of addresses corresponding to the names
	 */
	 public void startGame(String[] names, String[] addresses)
	{
		game.startGame(names, addresses);
	}
	
	/**
	 * Calls the getPort method to return the port number which the listener is listening on.
	 * @return the port number the listener is listening on
	 */
	public String getPort()
	{	
		return game.getPort();
	}
	
}

⌨️ 快捷键说明

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