View Javadoc

1   package erland.game.tetris;
2   /*
3    * Copyright (C) 2003 Erland Isaksson (erland_i@hotmail.com)
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *
19   */
20  
21  import erland.game.GamePanelHandlerInterface;
22  import erland.game.GamePanelHandler;
23  import erland.game.GamePanelHandlerForApplication;
24  import erland.game.GamePanelHandlerApplicationInterface;
25  import erland.util.*;
26  
27  import javax.swing.*;
28  import java.awt.*;
29  
30  public class Tetris extends GamePanelHandlerForApplication implements GamePanelHandlerApplicationInterface {
31      private ParameterValueStorageExInterface storage;
32      private FileStorage file;
33      private ImageHandlerInterface images;
34      private String hostname;
35      private String name;
36      private TetrisMain panel;
37      private boolean bMultiplayer;
38  
39      public static void main(String[] args) {
40          Tetris game;
41          if(args.length>1) {
42              game = new Tetris(args[1],args[0]);
43          }else if(args.length==1) {
44              game = new Tetris("erland.homeip.net",args[0]);
45          }else {
46              game = new Tetris("erland.homeip.net","player");
47          }
48          GamePanelHandler.run(game,game);
49          System.exit(-1);
50      }
51  
52      public Tetris(String hostname,String name) {
53          this.hostname = hostname;
54          this.name = name;
55      }
56      public void askUserForOptions() {
57          Object[] possibleValues = { "Standalone single player", "Networked multiplayer"};
58          int selectedValue  = JOptionPane.showOptionDialog(null,
59                  "Choose game mode", "Game mode",
60                  JOptionPane.DEFAULT_OPTION,
61                  JOptionPane.INFORMATION_MESSAGE, null,
62                  possibleValues, possibleValues[0]);
63          if(selectedValue==0) {
64              bMultiplayer = false;
65          }else {
66              bMultiplayer = true;
67          }
68          if(bMultiplayer) {
69              String hostname = JOptionPane.showInputDialog("Please enter hostname",this.hostname);
70              if(hostname!=null) {
71                  this.hostname=hostname;
72              }
73              String name = JOptionPane.showInputDialog("Please enter your name",this.name);
74              if(name!=null) {
75                  this.name = name;
76              }
77          }
78      }
79      public DisplayMode[] getDisplayModes() {
80          DisplayMode[] displayModes = new DisplayMode[] {
81              new DisplayMode(640,480,32,0),
82              new DisplayMode(640,480,16,0),
83              new DisplayMode(640,480,8,0)};
84          return displayModes;
85      }
86  
87      public void initGames(GamePanelHandlerInterface handler) {
88          if(bMultiplayer) {
89              panel = new TetrisMain(new TetrisModelNetworked(hostname,name,name));
90          }else {
91              panel = new TetrisMain(new TetrisModelStandalone());
92          }
93          handler.addPanel("Game","Starting the game",panel);
94          handler.cheatWord("erland");
95      }
96  
97      public ImageHandlerInterface getImageHandler() {
98          if(images==null) {
99              images = new ImageHandlerForApplicationJarFile(getScreenHandler().getContainer(),"images/tetris");
100         }
101         return images;
102     }
103 
104     public ParameterValueStorageExInterface getStorage() {
105         if(storage==null) {
106             file = new FileStorage("tetris.xml");
107             storage = new ParameterStorageString(file,null,"tetris");
108         }
109         return storage;
110     }
111 
112 }