Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jdstar/JDStar.java


1   /*
2    * Copyright (C) 1999-2001 Max Gilead <gilead@linart.pl>
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  package jdstar;
19  
20  import javax.swing.JFrame;
21  import javax.swing.JLabel;
22  import javax.swing.JPanel;
23  import javax.swing.JOptionPane;
24  import java.awt.BorderLayout;
25  import java.awt.Button;
26  import java.awt.Frame;
27  import java.awt.Panel;
28  import java.awt.TextArea;
29  import java.awt.event.ActionListener;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.WindowAdapter;
32  import java.awt.event.WindowEvent;
33  import java.io.File;
34  import java.io.FileOutputStream;
35  import java.io.BufferedOutputStream;
36  import java.io.PrintStream;
37  import java.io.IOException;
38  import java.io.FileNotFoundException;
39  
40  /**
41   * This is a main executable class for <b>JDStar</b> game.
42   */
43  public class JDStar
44    {
45    static final String VERSION = "0.9.1 beta";
46  
47    static final String GAME_DIRECTORY = System.getProperty("user.dir") + File.separator
48      +"jdstar"+ File.separator;
49    static final String TILES_DIRECTORY = GAME_DIRECTORY +"tiles"+ File.separator;
50    static final String LEVELS_DIRECTORY = GAME_DIRECTORY +"levels"+ File.separator;
51    static final String TILESETS_DEFINITION_FILE = "tilesets";
52    static final String DEBUG_FILE = "jdstar_debug.txt";
53    static final String README_FILE = "README";
54    static
55      {
56      if (!new File(README_FILE).exists())
57        throw new CriticalException("Cannot find README file");
58      }
59  
60    private JDStar()
61      {
62      checkJDK();
63      final JFrame f = new JFrame("JDStar "+ JDStar.VERSION);
64      JLabel s = new JLabel("Game started");
65      Tilesets t = new Tilesets();
66      Input i = new Input();
67      f.addKeyListener(i);
68      Board b = new Board(i, s);
69      b.tileset = t.getDefaultTileset();
70      Display d = new Display(b, f);
71      GameLoop l = new GameLoop(b, i, d);
72      Komander k = new Komander(b, i, d, l, s, f);
73      l.komander = k;
74      k.k_null_atStart();
75      Menu m = new Menu(k, t);
76  
77      f.addWindowListener(new WindowAdapter()
78        {
79        public void windowClosing(WindowEvent evt)
80          {
81          f.setVisible(false);
82          System.exit(0);
83          }
84        });
85      JPanel panel = new JPanel();
86      //
87      panel.setLayout(new BorderLayout());
88      panel.add(d, BorderLayout.CENTER);
89      panel.add(s, BorderLayout.SOUTH);
90      f.setJMenuBar(m);
91      //
92      f.setContentPane(panel);
93      f.pack();
94      f.setVisible(true);
95      
96      JOptionPane.showMessageDialog(null,
97        "              --------- BETA VERSION ---------              \n"+
98        "\n"+
99        "This is a BETA version of JDStar.\n"+
100       "While it is supposed to work OK there might be problems with\n"+
101       "it. If you encounter ANY problem or have some suggestions please\n"+
102       "point your browset to http://jdstar.sf.net and submit a bug report\n"+
103       "or suggestion. If you are unable to do that then please email the\n"+
104       "author at <gilead@sf.net> or <gilead@linart.pl>\n"+
105       "\n"+
106       "Check out soon for a final version at http://jdstar.sf.net !!!",
107       "BETA version remainder", JOptionPane.INFORMATION_MESSAGE);
108     }
109 
110   public static void main(String[] args)
111     {
112     try
113       {
114       new JDStar();
115       }
116       catch(CriticalException ce)
117         {
118         JOptionPane.showMessageDialog(null,
119           "Critical game error occured. JDStar cannot\n"+
120           "continue to work. This error is most probably\n"+
121           "caused by missing file or similar error.\n"+
122           "Try to unpack a fresh copy of JDStar and try again.\n"+
123           "If the error will appear again, please contact\n"+
124           "author (<gilead@sf.net>) for assistance."+
125           "\n"+
126           "Error message:\n"+
127           ce.getMessage(),
128           "Critical game exception", JOptionPane.ERROR_MESSAGE);
129           System.exit(1);
130         }
131       catch(RuntimeException e)
132         {
133         BufferedOutputStream bout = null;
134         try
135           {
136           FileOutputStream fout = new FileOutputStream(new File(DEBUG_FILE));
137           bout = new BufferedOutputStream(fout);
138           PrintStream out = new PrintStream(bout);
139           out.println(
140             "=====================================================\n"+
141             "JDStar debug file\n"+
142             "\n"+
143             "Please send this file to <gilead@sf.net>.\n"+
144             "\n"+
145             e.getMessage() +"\n");
146           e.printStackTrace(out);
147           out.println(
148             "=====================================================\n");
149           }
150           catch(FileNotFoundException fnfe)
151             {} // so log is lost...
152           finally
153             {
154             try
155               {
156               bout.flush();
157               bout.close();
158               }
159               catch(IOException ioe) {}
160             }
161         
162         
163         JOptionPane.showMessageDialog(null,
164           e.getMessage() +"\n"+
165           "\n"+
166           "This is an unexpected internal game exception\n"+
167           "Please report this error to the author with\n"+
168           "the detailed description about what you were\n"+
169           "doing while this exception occured.\n"+
170           "This message has been written to the file\n"+
171           DEBUG_FILE +".\n"+
172           "\n"+
173           "Thank you for reporting and we're sorry for\n"+
174           "the trouble",
175           "Internal game exception", JOptionPane.ERROR_MESSAGE);
176           System.exit(1);
177         }
178     }
179   
180   private void checkJDK()
181     {
182     int major = 0;
183     int minor = 0;
184     try
185       {
186       String s = System.getProperty("java.specification.version");
187       if (s.length() != 3)
188         return; // ?? ...
189       major = new Integer(s.charAt(0)).intValue();
190       minor = new Integer(s.charAt(2)).intValue();
191       }
192       catch(NumberFormatException nfe)
193         {
194         // WHAT?!
195         }
196     if (major < 1 || minor < 2)
197       {
198       Frame f = new Frame("Version incompatibility");
199       Panel p = new Panel();
200       p.setLayout(new BorderLayout());
201       TextArea t = new TextArea();
202       t.setText(
203         "For JDStar to run you must have Java Runtime version 1.2 or higher.\n"+
204         "Currently running version "+ major +"."+ minor +".\n"+
205         "\n"+
206         "Please upgrade your Java Runtime Environment.\n"+
207         "Take a look at http://java.sun.com for upgrades");
208       p.add(t, BorderLayout.CENTER);
209       Button quit = new Button("Quit");
210       quit.addActionListener(new ActionListener()
211         {
212         public void actionPerformed(ActionEvent evt)
213           {
214           System.exit(0);
215           }
216         });
217       p.add(quit, BorderLayout.SOUTH);
218       f.add(p);
219       f.pack();
220       f.setVisible(true);
221       }
222     }
223   }
224