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

📄 gameroomwindowdialog.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on Mar 9, 2006
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.GTADS.client;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ScrollPaneConstants;

import org.GTADS.messenger.MessageAdapter;
import org.GTADS.protocol.MetaData;
import org.GTADS.proxy.ProxyGUI;
import org.GTADS.usermanager.TranslateManager;
import org.GTADS.client.swing.*;

/**
 * @author sday
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class GameroomWindowDialog extends GTADSFrame implements ActionListener, KeyListener, MouseListener{
	public static GameroomWindowDialog instance;
	private String gameroomTitle;
	private GTADSTextArea gameroomChatArea;
	private GTADSTextField gameroomChatField;
	private GTADSScrollPane gameroomChatScroll;
	private GTADSScrollPane gameroomListScroll;
	private GTADSList gameroomUserList;
	private GTADSButton sendChat;
	private GTADSButton startGame;
	private GTADSButton closeGameroom;
	private GTADSLabel explanationLabel;
	private String clientHelp = TranslateManager.getInstance().printLocale("Please wait for the host to start the " +
			"game proxy to proceed to gameplay.<br> Once the Proxy is started you must press START and wait for the gameroom <br>" +
			"to say that you are connected to the host proxy. Please allow the host a<br> few moments to start up their game " +
			"before connecting. When connecting<br> inside the game please use TCP/IP and connect to the IP 127.0.0.1 or locahost.");
	private String hostHelp = TranslateManager.getInstance().printLocale("To begin gameplay press the START PROXY button below " +
			"and the proxy<br> window will appear automatically started. Wait for the gameroom to indicate that the player has<br> " +
			"successfully connected to the proxy and open your game and host on TCP/IP.<br> In a few moments the player(s) should appear " +
			"and you may <br>begin your normal multiplayer gameplay.");
	private boolean isHost = true;
	
	private Vector gameroomVectorList;
	
	public static GameroomWindowDialog getInstance(){
		return instance;
	}
	
	public static GameroomWindowDialog getInstance(String gameroom){
		if (instance == null){
			instance = new GameroomWindowDialog(gameroom);
		}
		return instance;
	}
	
	public static void clearInstance(){
		if (instance != null){
			ProxyGUI.clearInstance();
			instance.hide();
			instance.dispose();
			instance = null;
		}
	}
	
	public static boolean hasInstance(){
		return instance != null ? true : false;
	}
	
	public GameroomWindowDialog(String gameroom){
		gameroomTitle = gameroom;
		this.setTitle(TranslateManager.getInstance().printLocale("Gameroom") + ": " + gameroomTitle);
		
		this.setDefaultCloseOperation(GTADSFrame.DISPOSE_ON_CLOSE);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e){
				leaveGameroom();
				GameroomWindowDialog.clearInstance();
			}
		});
		setupComponents();
		setContentPane(getMainContentPanel());
		
		pack();
		
		MetaData whoIsHost = new MetaData(ClientLoginScreen.getInstance().getUsername(),MetaData.GAME, MetaData.IS_HOST);
		try {
			MessageAdapter.sendData(null, DSChatClient.ClientSocket, whoIsHost, gameroomTitle);
		} catch (IOException ioe){
			return;
		}
	}
	
	private void setupComponents() {
		gameroomVectorList = new Vector();
		gameroomChatArea = new GTADSTextArea();
		gameroomChatArea.setColumns(40);
		gameroomChatArea.setRows(10);
		gameroomChatArea.setLineWrap(true);
		gameroomChatArea.setAutoscrolls(true);
		gameroomChatArea.setEditable(false);
		
		gameroomChatScroll = new GTADSScrollPane(gameroomChatArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		gameroomChatScroll.setAutoscrolls(true);
		
		gameroomChatField = new GTADSTextField();
		gameroomChatField.setColumns(40);
		gameroomChatField.addKeyListener(this);
		
		gameroomUserList = new GTADSList();
		gameroomUserList.setVisibleRowCount(4);
		gameroomUserList.setPrototypeCellValue("###############");
		gameroomUserList.addMouseListener(this);
		
		gameroomListScroll = new GTADSScrollPane(gameroomUserList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		gameroomListScroll.setAutoscrolls(true);
		
		explanationLabel = new GTADSLabel();
		this.setExplanationLabel(isHost);
		
		startGame = new GTADSButton("Start Proxy");
		startGame.addActionListener(this);
		sendChat = new GTADSButton("Send");
		sendChat.addActionListener(this);
		closeGameroom = new GTADSButton("Close Gameroom");
		closeGameroom.addActionListener(this);
		gameroomVectorList.add(ClientLoginScreen.getInstance().getUsername());
		this.refreshListData();
	}
	
	private GTADSPanel getMainContentPanel(){
		GTADSPanel mainPanel = new GTADSPanel();
		GTADSPanel panel1 = new GTADSPanel();
		GTADSPanel panel2 = new GTADSPanel();
		GTADSPanel panel3 = new GTADSPanel();
		GTADSPanel panel4 = new GTADSPanel();
		
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel3.setLayout(new FlowLayout(FlowLayout.RIGHT));
		panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		panel4.setBounds(this.bounds());
		
		panel1.add(gameroomChatScroll);
		panel1.add(Box.createHorizontalStrut(10));
		panel1.add(gameroomListScroll);
		panel2.add(gameroomChatField);
		panel2.add(Box.createHorizontalStrut(10));
		panel2.add(sendChat);
		panel3.add(startGame);
		panel3.add(closeGameroom);
		panel4.add(explanationLabel);
		
		mainPanel.add(panel1);
		mainPanel.add(panel2);
		mainPanel.add(panel3);
		mainPanel.add(panel4);

		return mainPanel;
	}
	
	public void setExplanationLabel(boolean isHost){
		if (isHost){
			explanationLabel.setText("<html>* " + hostHelp + "</html>");	
		}
		else{
			explanationLabel.setText("<html>* " + clientHelp + "</html>");	
		}
	}
	
	public void appendToChatroom(String gameroomChat){
		if (gameroomChat != null){
			gameroomChat += "\n";
			gameroomChatArea.setText(gameroomChatArea.getText() + gameroomChat);
			
			int end = gameroomChatArea.getText().length();
			gameroomChatArea.select(end,end);
		}
	}
	
	public void addUserToList(String userName){
		if (!gameroomVectorList.contains(userName)){
			gameroomVectorList.add(userName);
		}
		refreshListData();
	}
	
	public void removeUserFromList(String userName){
		if (gameroomVectorList.contains(userName)){
			gameroomVectorList.remove(userName);
		}
		refreshListData();
	}
	
	public void refreshListData(){
		int placeValue = gameroomUserList.getSelectedIndex();
		gameroomUserList.setListData(gameroomVectorList);
		gameroomUserList.setSelectedIndex(placeValue);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(this.sendChat)){
			sendChatMessageAction();
		}
		if (e.getSource().equals(this.closeGameroom)){
			leaveGameroom();
			GameroomWindowDialog.clearInstance();
		}
		if (e.getSource().equals(this.startGame)){
			MetaData startProxy = new MetaData(ClientLoginScreen.getInstance().getUsername(), MetaData.GAME,
					MetaData.START_PROXY);
			try {
				MessageAdapter.sendData(null,DSChatClient.ClientSocket,startProxy,gameroomTitle);
			} catch (IOException ioe){
				return;
			}
		}

	}
	public void keyPressed(KeyEvent e) {
		if (e.getSource().equals(gameroomChatField)){
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				sendChatMessageAction();
			}
		}

	}
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	
	public void sendChatMessageAction(){
		if (!gameroomChatField.getText().equals("")){
			sendDataToGameroom(gameroomChatField.getText());
			gameroomChatField.setText("");
		}
	}
	
	public String getGameroomName(){
		return this.gameroomTitle;
	}
	
	public boolean sendDataToGameroom(String message){
		boolean isSuccess;
		MetaData sendMessageHeader = new MetaData(ClientLoginScreen.getInstance().getUsername(),
				MetaData.GAME,gameroomTitle);
		try {
		MessageAdapter.sendData(null,DSChatClient.ClientSocket,sendMessageHeader,message);
		isSuccess = true;
		} catch (IOException ioe){
			isSuccess = false;
		}
		return isSuccess;
	}
	
	public void leaveGameroom(){
		MetaData leaveGameroom = new MetaData(ClientLoginScreen.getInstance().getUsername(),
				MetaData.GAME,MetaData.PART_CHATROOM);
		try {
			MessageAdapter.sendData(null, DSChatClient.ClientSocket,leaveGameroom,gameroomTitle);
		} catch (IOException ioe){
			return;
		}
	}
	public void mouseClicked(MouseEvent e) {
		if (e.getSource().equals(gameroomUserList)){
			if (e.getButton() == MouseEvent.BUTTON1){
				if (e.getClickCount() == 2){
					InstantMessageWindow.getInstance((String)gameroomUserList.getSelectedValue()).show();
				}
			}
		}

	}
	
	public void disableStartButton(){
		startGame.setVisible(false);
		isHost = false;
		this.setExplanationLabel(isHost);
	}
	
	public void startGameProxy(String IPAddress){
		if (isHost){
			ProxyGUI.getInstance(null);
		}
		else {
			ProxyGUI.getInstance(IPAddress);
		}
	}
	
	public boolean isGameHost(){
		return isHost;
	}
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	public static void main(String[] args) {
		GameroomWindowDialog.getInstance("Test Room").show();
	}
}

⌨️ 快捷键说明

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