Results 1 to 4 of 4
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Applet to Application (Game)

    I designed a pretty basic game (some recreation of snake with some extra things). Anyway, when I first designed it I made it in an applet. I want to make it into a stand alone application, but still work with the Graphics, Thread, and such things that it does. I'm not really sure how to convert it, so I did some research... but quite frankly, I suck at research and the tutorials I found just confused me more. Instead of answering questions it just made more... :P

    1. Is there a difference between an Applet and a JApplet?
    2. How can I put an Applet (or JApplet) in a program using JFrame?
    3. Any other useful tips? :P

    But yeah... here's my code. I took out most of the stuff just to show you what I'm working with here.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.Random;
    import java.util.ArrayList;
    
    @SuppressWarnings("serial")
    public class Window extends Applet implements Runnable, KeyListener
    {
    	//Variables and such
    
    	public void init()
    	{
    	requestFocus();
    	
    	setSize(WIDTH, HEIGHT);
    	setFont(new Font("Courier", Font.BOLD, 16));
    	setBackground(Color.white);
    	addKeyListener(this);	
    	backbuffer = createImage(WIDTH, HEIGHT);
    	backg = backbuffer.getGraphics();
    	
    	//Some stuff
    	//Images
    	//Audio
    	repaint();
    	}
    	
    	public void start()
    	{
    		thread = new Thread(this);
    		thread.start();
    	}
    	
    	public void run()
    	{
    		Thread current = Thread.currentThread();
    		while(current == thread)
    		{
    			try
    			{
    				Thread.sleep(speed);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    			repaint();
    		}
    	}
    	
    	public void stop()
    	{
    		thread = null;
    	}
    	
    	public void update(Graphics g)
    	{
    		//Logic Stuff
    		//Draw things to backg
    		paint(g);
    	}
    
    	public void paint(Graphics g)
    	{
    		g.drawImage(backbuffer, 0, 0, this);
    	}
    	
    	public void keyPressed(KeyEvent e)
    	{
    		//Key Events
    	}
    	
    	public void keyReleased(KeyEvent e)
    	{}
    	
    	public void keyTyped(KeyEvent e)
    	{}
    	
    	public void GAMEOVER(Graphics backg)
    	{
    		//Game Over
    	}
    	
    	public boolean collision(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh)
    	{
    		//Detect collision
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1. Is there a difference between an Applet and a JApplet?
    Applet is the earlier version of Java's applet class which uses the AWT graphics library. JApplet is the more recent version that is based in the more robust and powerful Swing library. Unless there are contraindications, you're better off creating JApplets than Applets.

    2. How can I put an Applet (or JApplet) in a program using JFrame?
    You can't and you don't want to either. Both JApplets and JFrames are root containers and as such, one doesn't belong in the other. Your best bet here is to write your program so that it is geared to produce a JPanel. Then if you want it to run as a JApplet, you place the JPanel in the JApplet's contentPane, and if you want it to run as a stand-alone program, you place the JPanel into the JFrame's contentPane. Either way, you are far better off just re-writing your app.
    3. Any other useful tips? :P
    Again, you should re-write your app. Also, go through the Sun Swing tutorials step by step. Also, you'll want to get rid of the Thread.sleep(...) calls and instead read up and use a Swing Timer to time your animation.

    Unfortunately I know of no short-cuts in learning this stuff other than hard work, time, and commitment.

    Much luck.
    Last edited by Fubarable; 04-11-2009 at 10:50 PM.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Ok thanks, I'll definitely have a look into that. I just started teaching myself about swing recently, so I suppose I'll just continue that.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Once you've converted your code to producing a JPanel, you can have a main() and an init() in the same class, which will run as both an applet and an app
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. An Applet Game
    By T3000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 05:18 PM
  2. starting a Swing application from an applet
    By FakeRabbit in forum Java Applets
    Replies: 9
    Last Post: 09-21-2008, 08:00 PM
  3. Battery 1.02 (Applet Game)
    By M77 in forum Java Software
    Replies: 3
    Last Post: 05-30-2008, 01:54 AM
  4. Looking for team members for creating the applet game.
    By M77 in forum Reviews / Advertising
    Replies: 10
    Last Post: 05-28-2008, 09:25 AM
  5. Help with game applet in java
    By barney in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •