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

📄 mainframe.java

📁 Massively Multiplayer Space Trading and Combat game. This is an online strategy game, not a 3D space
💻 JAVA
字号:
/*
 * MainFrame.java
 *
 * Copyright (C) 2000 Jason M. Hanley
 * Released under the GNU General Public License (GPL)
 * See license.txt for additional information.
 *
 * Created on July 27, 2000, 11:46 PM
 */

package fate.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

import fate.*;
import fate.network.*;
import fate.messages.*;
import fate.ui.*;
import fate.util.*;

/**
 * Frame window that comes up when the client is first started.
 *
 * @author  preylude@s3m.com
 * @version 0.1.0
 */
public class MainFrame extends JFrame implements ActionListener {

    JLabel labelStatus;
    public JTextField editServerIP, editUsername;
    public JPasswordField editPassword;

    /** Creates new MainFrame */
    public MainFrame( String title ) {
        super( title );
        
        JPanel bottomPanel = new FatePanel( new BorderLayout() );
        
        Box boxTop = new Box( BoxLayout.Y_AXIS );
        
        JPanel panel = new FatePanel();
        panel.setOpaque( false );
        Box boxEdit = new Box( BoxLayout.Y_AXIS );
        editServerIP = SwingUtil.addTextField( boxEdit, "Server IP", 32 );
        editUsername = SwingUtil.addTextField( boxEdit, "Username", 32 );
        editPassword = SwingUtil.addPasswordField( boxEdit, "Password", 32 );
        panel.add( boxEdit );

        JPanel panel2;
        panel2 = new JPanel();
        panel2.setOpaque( false );
        SwingUtil.addButton(  panel2, "Create New Account", 'n', "NewAccount", this );
        SwingUtil.addButton(  panel2, "Login to Existing", 'l', "Login", this );
       
        boxTop.add( panel );
        boxTop.add( panel2 );

        bottomPanel.add( boxTop, BorderLayout.NORTH );

        JTextArea welcomeMsg = new JTextArea( 8, 40 );
        welcomeMsg.setOpaque( false );
        welcomeMsg.setText(
        "Welcome to " + FateClient.programName + ". \n\n" +
        "Instructions:\n" +
        "First, ensure you are running the server.  Second, press the " +
        "'Create New Account' button.  This will create an account " +
        "with the a default login and password.  Now, press the " +
        "'Login to Existing' button.  After a second or two, you will see " +
        "the main UI with the moving planet view.  To change your login " +
        "and/or password press the 'Change Info' button at the upper right " +
        "hand corner, and enter the new information in the dialog.\n\n" +
        "Visit Fate on the web at http://fate.sourceforce.net\n\n" +
        "This version is able to create/edit account, accept client " +
        "connections, and relay chat messages.  See readme.txt for " +
        "additional information.\n\n" +
        FateClient.programName + " is released under the GNU General " +
        "Public License (GPL).  " +
        "See license.txt or http://www.gnu.org for more information.\n\n" +
        "This program is distributed in the hope that it will be useful, " +
        "but WITHOUT ANY WARRANTY; without even the implied warranty of " +
        "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the " +
        "GNU General Public License for more details.\n\n" +
        "Copyright 2000 Jason M. Hanley (preylude@s3m.com)" );
        welcomeMsg.setLineWrap( true );
        welcomeMsg.setWrapStyleWord( true );
        welcomeMsg.setEditable( false );
        JScrollPane welcomeScroll = new JScrollPane( welcomeMsg );
        welcomeScroll.setBorder( BorderFactory.createEtchedBorder() );
        bottomPanel.add( welcomeScroll, BorderLayout.CENTER );

        panel = new JPanel();
        panel.setOpaque( false );
        labelStatus = new JLabel( "Loading..." );
        panel.add( labelStatus );
        bottomPanel.add( panel, BorderLayout.SOUTH );

        getContentPane().add( bottomPanel, BorderLayout.CENTER );
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onExit();
            }
        });

        editServerIP.setText( "127.0.0.1" );
        // Read default settings from file
        try {
            Debug.trace( "Attempting to read connect settings" );
            FileInputStream fileIn = new FileInputStream( FateClient.ConnectFile );
            ObjectInputStream in = new ObjectInputStream( fileIn );
            editServerIP.setText( (String)in.readObject() );
            editUsername.setText( (String)in.readObject() );
            editPassword.setText( (String)in.readObject() );
            in.close();
            fileIn.close();
        } catch( Exception e ) {
            Debug.trace( "Error reading player database" );
        }

        status( "FateClient initialized." );
    }

    /** Exit handler */
    public void onExit() {
        try {
            Debug.trace( "Attempting to write connect settings" );
            FileOutputStream fileOut = new FileOutputStream( FateClient.ConnectFile );
            ObjectOutputStream out = new ObjectOutputStream( fileOut );
            out.writeObject( editServerIP.getText() );
            out.writeObject( editUsername.getText() );
            out.writeObject( editPassword.getText() );
            out.close();
            fileOut.close();
        } catch( Exception e ) {
            Debug.trace( "Error writing player database" );
            e.printStackTrace();
        }

        System.exit(0);
    }

    /** Handles ActionEvent */
    public void actionPerformed(ActionEvent e) {
        String strAction = e.getActionCommand();

        Debug.trace( "client.MainFrame: ActionCommand: " + strAction );

        if ( strAction.equals( "NewAccount" ) ) {
            createNewAccount();
        }
        else if( strAction.equals( "Login" ) ) {
            loginToServer();
        }
    }

    /** Update status message */
    public void status( String s ) {
        labelStatus.setText( s );
    }

    /** Create a new account on the server */
    public void createNewAccount() {
        try {
            status( "Opening connection to server on " + editServerIP.getText() );
            ClientConnection clientConnection = new ClientConnection(
            editServerIP.getText(), ServerConnection.FATE_PORT );

            LoginMessage loginMessage = new LoginMessage( "(newuser)", "", false );

            // Create the account (this will set loginMessage to a proper login)
            status( "Attempting to connect to server on " + editServerIP.getText() );
            clientConnection.connect( loginMessage );

            editUsername.setText( loginMessage.username );
            editPassword.setText( loginMessage.password );
            status( "New account created.  Username and Password received." );
        } catch( IOException e ) {
            status( "I/O Error.  Create new account failed." );
        }
    }

    /** Login to the server */
    public void loginToServer() {
        try {
            status( "Opening connection to server on " + editServerIP.getText() );
            ClientConnection clientConnection = new ClientConnection(
            editServerIP.getText(), ServerConnection.FATE_PORT );

            LoginMessage loginMessage = new LoginMessage( editUsername.getText(),
            editPassword.getText(), false );

            // Create the account (this will set loginMessage to a proper login)
            status( "Attempting to connect to server on " + editServerIP.getText() );

            if ( clientConnection.connect( loginMessage ) ) {

                status( "Connected ok to server.  Loading console." );

                // Create a new console
                ConsoleFrame console = new ConsoleFrame( this, clientConnection );
                console.pack();
                SwingUtil.centerWindow( console );

                // Set up message processor
                MessageProcessor msgProc = new MessageProcessor( clientConnection, console );
                msgProc.start();

                // Bring up console dialog
                console.setVisible(true);
            } else {
                status( "Error connecting to server (Possible incorrect username/password)." );
            }
        } catch( IOException e ) {
            status( "I/O Error.  Connect to server failed." );
        }
    }
}

⌨️ 快捷键说明

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