📄 guinterface.java
字号:
// GUInterface.java
//
// Copyright (c) 2000-2001 Symbian Ltd. All rights reserved
package com.symbian.devnet.whist.awt;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Image.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import com.symbian.devnet.whist.game.*;
import com.symbian.devnet.quartz.awt.*;
/**
* The class which controls the contents of the output to the screen.
* This class is responsible for all objects painted to the screen and
* ensures that they are disposed of correctly.
* @author Symbian Devnet
*/
// Needs to be made more efficient.
// Graphics and redrawing need improving!!!
public class GUInterface implements GameEventListener, ActionListener, MouseListener
{
/** The array of names used when initialising the Ring. */
private static String[] names = new String[4];
/** The array of address used when initialising the Ring. */
private static String[] addresses = new String[4]; //= { "udp://10.159.43.5:9991", "udp://10.159.43.5:9992", "udp://10.159.43.5:9993", "udp://10.159.43.5:9994" };
/** The protocol method to be used, 0 for UDP, 1 for WDPSMS. */
private static int protocol = 0;
/** An instance of a Game. */
private static Game game;
/** The data received from the Game. */
private String dataReceived;
/** The String dataReceived represented in a char array. */
private char[] data;
/** My position in the game. */
private int pNo = 5;
/**
* The current card order, allowing the cards to be reordered to be
* displayed in the correct order.
*/
private int[] currentCardOrder = new int[3];
/** Used to determine if the player number has been set. */
private boolean pNoSet = false;
/** Reference to the <code>Whist</code> class */
private Whist whistRef;
/** Determines if there are are cards from the current trick to be displayed */
private boolean bcurrent = false;
/** Array of the current <code>CardImage</code>s */
private CardImage[] currentValues;
/** Label containing the name of the player on that machine */
private Label playerName;
/** The main <code>Panel</code> */
private Panel panel1;
/** A <code>Button</code> labelled "Yes" */
private Button yesButton;
/** A <code>Button</code> labelled "No" */
private Button noButton;
/** Array of Strings to be used if the player trys to make an illegal move */
private String[] illegalMove = new String[2];
/** A <code>GameDialog</code> for the end of a game */
private GameDialog gameEnded;
/** A <code>GameDialog</code> for when a player terminates a game during play */
private GameDialog endGame;
/** A <code>Panel</code> containing the cards in the hand */
private Panel cardPanel;
/** A <code>Panel</code> used to contain the player's name */
private Panel northPanel;
/**
* An array of <code>Image</code>s representing all 13 cards in the
* suit of Hearts
*/
private Image[] heartImages = new Image[13];
/**
* An array of v all 13 cards in the
* suit of Diamonds
*/
private Image[] diamondImages = new Image[13];
/**
* An array of <code>Image</code>s representing all 13 cards in the
* suit of Spades
*/
private Image[] spadeImages = new Image[13];
/**
* An array of <code>Image</code>s representing all 13 cards in the
* suit of Clubs
*/
private Image[] clubImages = new Image[13];
/** Flag to show if there are cards to be displayed */
private boolean displayCards = false;
/**
* An array of <code>Image</code>s representing the Hearts in the
* current hand
*/
private CardImage[] hearts;
/**
* An array of <code>Image</code>s representing the Diamonds in the
* current hand
*/
private CardImage[] diamonds;
/**
* An array of <code>Image</code>s representing the Clubs in the
* current hand
*/
private CardImage[] clubs;
/**
* An array of <code>Image</code>s representing the Spades in the
* current hand
*/
private CardImage[] spades;
/** A reference to the <code>MediaTracker</code> */
private MediaTracker tracker;
/**
* An array of <code>PanelWithStruts</code>s used for holding the
* <code>CardImage</code>s for displaying
*/
private PanelWithStruts[] panelArray = new PanelWithStruts[5];
/** Array of names of the Heart iamges */
private String[] heartImageNames = { "H2.gif", "H3.gif", "H4.gif", "H5.gif", "H6.gif", "H7.gif", "H8.gif", "H9.gif", "H10.gif", "HJ.gif", "HQ.gif", "HK.gif", "HA.gif" };
/** Array of names of the Club iamges */
private String[] clubImageNames = { "C2.gif", "C3.gif", "C4.gif", "C5.gif", "C6.gif", "C7.gif", "C8.gif", "C9.gif", "C10.gif", "CJ.gif", "CQ.gif", "CK.gif", "CA.gif" };
/** Array of names of the Diamond iamges */
private String[] diamondImageNames = { "D2.gif", "D3.gif", "D4.gif", "D5.gif", "D6.gif", "D7.gif", "D8.gif", "D9.gif", "D10.gif", "DJ.gif", "DQ.gif", "DK.gif", "DA.gif" };
/** Array of names of the Spade iamges */
private String[] spadeImageNames = { "S2.gif", "S3.gif", "S4.gif", "S5.gif", "S6.gif", "S7.gif", "S8.gif", "S9.gif", "S10.gif", "SJ.gif", "SQ.gif", "SK.gif", "SA.gif" };
/** Reference to the <code>Panel</code> containing the Heart images */
private static final int HEART_PANEL = 1;
/** Reference to the <code>Panel</code> containing the Club images */
private static final int CLUB_PANEL = 3;
/** Reference to the <code>Panel</code> containing the Diamond images */
private static final int DIAMOND_PANEL = 2;
/** Reference to the <code>Panel</code> containing the Spade images */
private static final int SPADE_PANEL = 0;
/** Determines whether it is the turn of this player */
private boolean myTurn = false;
/** Determines whether the name has been set */
private boolean setName = false;
/** Defines the <code>Color</code> of the background */
private Color cardTableGreen;
/** Defines the <code>Color</code> of the background for the trump suit*/
private Color trumpGreen;
/** Defines the <code>Color</code> of the background for the cards played */
private Color currentColor;
/** A bold font */
private Font boldFont;
/** The normal Quartz font */
private Font normalFont;
private int[] myHand;
private boolean stopped = false;
/**
* Adds a gameEventListener to the
* game and then calls the run() method in game.
*/
public void runGame()
{
game.addGameEventListener(this);
Thread t = new Thread(game);
t.start();
}
/**
* Takes the data received from the game and performs various actions
* depending on the values in the data received. These tasks include
* prompting the user to select a card to play, displaying the current
* card, and displaying the score.
*/
private void processData()
{
names = game.getNames();
dataReceived.trim();
data = dataReceived.toCharArray();
for (int i = 0; i < data.length; i++)
{
if (data[i] == 'H')
{
showTrumpSuit();
myHand = game.getHand();
myTurn = true;
showCards(myHand);
}
else if (data[i] == 'C')
displayCurrent();
else if (data[i] == 'O')
displayLastRound();
else if (data[i] == 'E')
{
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.");
gameEnded = new GameDialog(whistRef, "End of Game", array);
gameEnded.setVisible(true);
System.exit(0);
}
else if (data[i] == 'F')
{
String g = "n";
int[] winners = game.getWinner();
String[] array = new String[4];
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.");
gameEnded = new GameDialog(whistRef, "End of Game", array);
gameEnded.setVisible(true);
Label end1 = new Label("Would you like to play \n another game?", Label.CENTER);
panel1.add(end1);
panel1.add(yesButton);
panel1.add(noButton);
whistRef.setVisible(true);
}
else if (data[i] == 'Q')
{
game.gameStopped = true;
stopped = true;
int[] winners = game.getWinner();
String[] array = new String[6];
array[0] = "The game has been ended by another player";
array[1] = (names[0]+ " & " + names[2]);
array[2] = (" have won " + winners[0] + " games.");
array[3] = (names[1]+ " & " + names[3]);
array[4] = (" have won " + winners[1] + " games.");
array[5] = ("Thank you for playing Whist.");
endGame = new GameDialog(whistRef, "End of Game", array);
endGame.setVisible(true);
synchronized(game.lock)
{
game.lock.notify();
game.lock.notifyAll();
}
try
{
wait(15000);
}
catch (Exception e)
{}
whistRef.showSMSBaseView();
}
}
if (!stopped)
redrawPanel();
else
stopped = false;
}
/**
* Gets the trump suit and changes the background colour of
* the line of cards in the trump suit.
*/
private void showTrumpSuit()
{
String tr = game.getTrump();
if (tr.equals("Heart"))
panelArray[HEART_PANEL].setBackground(trumpGreen);
else if (tr.equals("Club"))
panelArray[CLUB_PANEL].setBackground(trumpGreen);
else if (tr.equals("Spade"))
panelArray[SPADE_PANEL].setBackground(trumpGreen);
else if (tr.equals("Diamond"))
panelArray[DIAMOND_PANEL].setBackground(trumpGreen);
}
/**
* Gets the cards currently in play from the game and displays
* them.
*/
private void displayCurrent()
{
if (!pNoSet)
{
pNo = game.getPlayerNumber();
switch (pNo)
{
case 1:{
currentCardOrder[0] = 3;
currentCardOrder[1] = 2;
currentCardOrder[2] = 1;
break;
}
case 2: {
currentCardOrder[0] = 0;
currentCardOrder[1] = 3;
currentCardOrder[2] = 2;
break;
}
case 3: {
currentCardOrder[0] = 1;
currentCardOrder[1] = 0;
currentCardOrder[2] = 3;
break;
}
default: {
currentCardOrder[0] = 2;
currentCardOrder[1] = 1;
currentCardOrder[2] = 0;
break;
}
}
pNoSet = true;
}
String[] temp = game.getCurrentCards();
currentValues = new CardImage[temp.length];
for (int i = 0; i < temp.length; i++)
{
char[] c = temp[i].toCharArray();
if (c[0] == 'H')
{
if (temp[i].equals("H2"))
currentValues[i] = new CardImage(heartImages[0], "H", "2");
else if (temp[i].equals("H3"))
currentValues[i] = new CardImage(heartImages[1], "H", "3");
else if (temp[i].equals("H4"))
currentValues[i] = new CardImage(heartImages[2], "H", "4");
else if (temp[i].equals("H5"))
currentValues[i] = new CardImage(heartImages[3], "H", "5");
else if (temp[i].equals("H6"))
currentValues[i] = new CardImage(heartImages[4], "H", "6");
else if (temp[i].equals("H7"))
currentValues[i] = new CardImage(heartImages[5], "H", "7");
else if (temp[i].equals("H8"))
currentValues[i] = new CardImage(heartImages[6], "H", "8");
else if (temp[i].equals("H9"))
currentValues[i] = new CardImage(heartImages[7], "H", "9");
else if (temp[i].equals("H10"))
currentValues[i] = new CardImage(heartImages[8], "H", "10");
else if (temp[i].equals("HJ"))
currentValues[i] = new CardImage(heartImages[9], "H", "J");
else if (temp[i].equals("HQ"))
currentValues[i] = new CardImage(heartImages[10], "H", "Q");
else if (temp[i].equals("HK"))
currentValues[i] = new CardImage(heartImages[11], "H", "K");
else if (temp[i].equals("HA"))
currentValues[i] = new CardImage(heartImages[12], "H", "A");
}
else if (c[0] == 'D')
{
if (temp[i].equals("D2"))
currentValues[i] = new CardImage(diamondImages[0], "D", "2");
else if (temp[i].equals("D3"))
currentValues[i] = new CardImage(diamondImages[1], "D", "3");
else if (temp[i].equals("D4"))
currentValues[i] = new CardImage(diamondImages[2], "D", "4");
else if (temp[i].equals("D5"))
currentValues[i] = new CardImage(diamondImages[3], "D", "5");
else if (temp[i].equals("D6"))
currentValues[i] = new CardImage(diamondImages[4], "D", "6");
else if (temp[i].equals("D7"))
currentValues[i] = new CardImage(diamondImages[5], "D", "7");
else if (temp[i].equals("D8"))
currentValues[i] = new CardImage(diamondImages[6], "D", "8");
else if (temp[i].equals("D9"))
currentValues[i] = new CardImage(diamondImages[7], "D", "9");
else if (temp[i].equals("D10"))
currentValues[i] = new CardImage(diamondImages[8], "D", "10");
else if (temp[i].equals("DJ"))
currentValues[i] = new CardImage(diamondImages[9], "D", "J");
else if (temp[i].equals("DQ"))
currentValues[i] = new CardImage(diamondImages[10], "D", "Q");
else if (temp[i].equals("DK"))
currentValues[i] = new CardImage(diamondImages[11], "D", "K");
else if (temp[i].equals("DA"))
currentValues[i] = new CardImage(diamondImages[12], "D", "A");
}
else if (c[0] == 'C')
{
if (temp[i].equals("C2"))
currentValues[i] = new CardImage(clubImages[0], "C", "2");
else if (temp[i].equals("C3"))
currentValues[i] = new CardImage(clubImages[1], "C", "3");
else if (temp[i].equals("C4"))
currentValues[i] = new CardImage(clubImages[2], "C", "4");
else if (temp[i].equals("C5"))
currentValues[i] = new CardImage(clubImages[3], "C", "5");
else if (temp[i].equals("C6"))
currentValues[i] = new CardImage(clubImages[4], "C", "6");
else if (temp[i].equals("C7"))
currentValues[i] = new CardImage(clubImages[5], "C", "7");
else if (temp[i].equals("C8"))
currentValues[i] = new CardImage(clubImages[6], "C", "8");
else if (temp[i].equals("C9"))
currentValues[i] = new CardImage(clubImages[7], "C", "9");
else if (temp[i].equals("C10"))
currentValues[i] = new CardImage(clubImages[8], "C", "10");
else if (temp[i].equals("CJ"))
currentValues[i] = new CardImage(clubImages[9], "C", "J");
else if (temp[i].equals("CQ"))
currentValues[i] = new CardImage(clubImages[10], "C", "Q");
else if (temp[i].equals("CK"))
currentValues[i] = new CardImage(clubImages[11], "C", "K");
else if (temp[i].equals("CA"))
currentValues[i] = new CardImage(clubImages[12], "C", "A");
}
else
{
if (temp[i].equals("S2"))
currentValues[i] = new CardImage(spadeImages[0], "S", "2");
else if (temp[i].equals("S3"))
currentValues[i] = new CardImage(spadeImages[1], "S", "3");
else if (temp[i].equals("S4"))
currentValues[i] = new CardImage(spadeImages[2], "S", "4");
else if (temp[i].equals("S5"))
currentValues[i] = new CardImage(spadeImages[3], "S", "5");
else if (temp[i].equals("S6"))
currentValues[i] = new CardImage(spadeImages[4], "S", "6");
else if (temp[i].equals("S7"))
currentValues[i] = new CardImage(spadeImages[5], "S", "7");
else if (temp[i].equals("S8"))
currentValues[i] = new CardImage(spadeImages[6], "S", "8");
else if (temp[i].equals("S9"))
currentValues[i] = new CardImage(spadeImages[7], "S", "9");
else if (temp[i].equals("S10"))
currentValues[i] = new CardImage(spadeImages[8], "S", "10");
else if (temp[i].equals("SJ"))
currentValues[i] = new CardImage(spadeImages[9], "S", "J");
else if (temp[i].equals("SQ"))
currentValues[i] = new CardImage(spadeImages[10], "S", "Q");
else if (temp[i].equals("SK"))
currentValues[i] = new CardImage(spadeImages[11], "S", "K");
else if (temp[i].equals("SA"))
currentValues[i] = new CardImage(spadeImages[12], "S", "A");
}
}
bcurrent = true;
}
/**
* Gets the cards played in the last round and displays them.
*/
private void displayLastRound()
{
String[] array = new String[5];
String w = "Trick winner: " + game.getTrickWinner();
String[] temp = game.getPreviousRound();
for (int i = 0; i < temp.length; i++)
array[i] = (names[i] + ": " + temp[i] + " ");
array[4] = w;
GameDialog oldCardsDialog = new GameDialog(whistRef, "Previous Round", array);
oldCardsDialog.setVisible(true);
}
/**
* Displays the cards in the array passed into the method. Adds
* mouseListeners to each card.
* @param the array of cards in the hand
*/
private void showCards(int[] hand)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -