Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Frame vs. Applet  (Read 2502 times)
0 Members and 1 Guest are viewing this topic.
Offline ido

Junior Member





« Posted 2010-04-15 11:05:26 »

Hi,
I'm trying to understand how to deploy an app both as an applet and as a normal java desktop app as painlessly as possible.

What I came up with so far is this.

GameApplet (derived from the 4k applet template thread) and GameFrame are both thin wrappers around the same functionality, and are basically identical except for extending different AWT classes.

And yet while GameApplet seem to work well, GameFrame just displays a blank grey window and doesn't quit upon window close.

Any idea what I'm doing wrong?

Offline Wildern

Junior Member





« Reply #1 - Posted 2010-04-15 12:50:11 »

These are the methods I use, it will run either as an applet or a standalone application.
If this isn't a good way to do it, hopefully, someone else will offer improvements.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
public class GameApplet extends Applet {

   private static JFrame parentFrame=null;
   private static boolean blnApplet = false;
   private static int frameWidth;
   private static int frameHeight;

   public GameApplet() {
      super();
      enableEvents(AWTEvent.MOUSE_EVENT_MASK);
      enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
      enableEvents(AWTEvent.KEY_EVENT_MASK);
      blnApplet = true;
   }

   public GameApplet(JFrame f) {
      this();
      parentFrame = f;
      initialized = false;
      blnApplet = false;
   }

   public void init() {
      blnApplet = true;
   }

   public void destroy() {
   }

   public void reset() {
      ...
      Initialize game objects here, load images, etc.
      ...
      initialized = true;
   }

   public void start() {
      new Thread() {
         public void run() {
            render();
            ...
            game loop goes here
            ...
            if (!blnApplet) {
               System.exit(0);
            }
         }
      }.start();
   }

   public void stop() {
   }

   public void render() {
      if (initialized) {
         ...
         draw on gDisplay here
         ...
         Graphics g;
         try {
            g = this.getGraphics();
            if (g != null) {
               g.drawImage(imgDisplay, 0, 0, null);
               g.dispose();
            }
         } catch (Exception e) {
         }
      }
   }

   public void update(Graphics g) {
      paint(g);
   }

   public void addNotify() {
      super.addNotify();
      GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
      DisplayMode displayMode = device.getDisplayMode();
      frameWidth = displayMode.getWidth();
      frameHeight = displayMode.getHeight();
      frameWidth = 800;
      frameHeight = 600;
      if (!blnApplet && parentFrame != null) {
         parentFrame.setSize(parentFrame.getInsets().left + parentFrame.getInsets().right + frameWidth, parentFrame.getInsets().top + parentFrame.getInsets().bottom + frameHeight);
      }
      imgDisplay = createImage(frameWidth, frameHeight);
      gDisplay = imgDisplay.getGraphics();
      reset();
   }

   public static void main(String[] args) {
      GameFrame frame = new GameFrame(gameName);
      GameApplet game = new GameApplet(frame);
      frame.add("Center", game);
      frame.setUndecorated(true);
      frame.setIgnoreRepaint(true);
      frame.setVisible(true);
      game.start();
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class GameFrame extends JFrame {
   public GameFrame(String str) {
      super (str);
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
   }

   protected void processEvent(AWTEvent evt) {
      switch (evt.getID()) {
         case WindowEvent.WINDOW_CLOSING:
            dispose();
            System.exit(0);
         default:
            super.processEvent(evt);
      }
   }
}
Offline ido

Junior Member





« Reply #2 - Posted 2010-04-15 16:11:41 »

Thanks, that's quite useful!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2010-04-15 16:40:54 »

another way is to write an applet viewer in java, I have done such viewer some years ago but never polish it, you can check source here it is still a WIP but already workable only requiere the add of the runned applet jars in classpath of the URLclassloader :

http://demo.dzzd.net/appletViewer/appletViewer.zip


what is interressting in this approach is that you dont have to make special code and it can work with any existing applet without the need to modify or recompile them (no need of the source code too)

Offline ido

Junior Member





« Reply #4 - Posted 2010-04-15 18:23:40 »

Wildern's solution seems to work well.

Offline ido

Junior Member





« Reply #5 - Posted 2010-04-17 20:32:09 »

<deleted comment, please ignore>

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (24 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (35 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (26 views)
2014-09-07 01:12:14

Longarmx (33 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!