Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  application to applet  (Read 1220 times)
0 Members and 1 Guest are viewing this topic.
Offline gladwda

Junior Newbie





« Posted 2011-10-17 21:01:06 »

Hello all
Having spent 3 weeks now learning Java I'm still very much a novice.

What i'm struggling to do at the moment is convert my application to an applet to run on a webpage. It's a small game and Im extending canvas, this is causing me no end of grief as I don't think I'm grasping the fundemental differences. Obvious canvas has to go as it relates to windows, but what can replace it in order for me to still be able to use and implement something like bufferstrategy? Argh, its frustrating. Can somebody help? I'll paste exerpts of the code below. Thanks if you can!

***import stuff here***
public class Game extends Canvas {
   private BufferStrategy buffage;
***private boolean/arraylists/ints initialised here***
public Game() {
      JFrame theFrame = new JFrame("Name Goes Here");
      JPanel thePanel = (JPanel) theFrame.getContentPane();
      thePanel.setPreferredSize(new Dimension(800,600));
      thePanel.setLayout(null);
      setBounds(0,0,800,600);
      thePanel.add(this);
      setIgnoreRepaint(true);
      theFrame.pack();
      theFrame.setResizable(false);
      theFrame.setVisible(true);
      theFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      addKeyListener(new KeyInputHandler());
      requestFocus();
      createBufferStrategy(2);
      buffage = getBufferStrategy();
      initWidgets();
   }
   private void startGame() {

Offline zoto

Senior Devvie


Medals: 4



« Reply #1 - Posted 2011-10-18 03:31:32 »

Applet and Frame both extend Container so will add your Canvas in a similar way. You could start refactoring by creating a class that extends  JFrame and moving the jframe code out of your canvas. Once your frame is creating an instance of your canvas and drawing it successfully try creating a class that extends Applet to run it.

Once the applet is created you will need the webpage where you want to see it add html like:

<APPLET height="570" width="760" codebase="java/" code="GameApplet">
You are missing <a href=http://www.java.com/download>java</a>.
</APPLET>

where GameApplet is the name of your class that extends applet and codebase is a folder path to the GameApplet.class file.

There is still a lot to learn but hopefully this will get you started.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-10-18 04:02:42 »

If you want to make an applet, you don't need JFrame. You should have a class that extends Applet and then add Canvas to it. That's all.
Read about how applets work here.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gladwda

Junior Newbie





« Reply #3 - Posted 2011-10-18 20:40:04 »

Thanks for replying.

I'm still a bit stuck by this. Say I adjust game to extend JApplet

I then change public game() to public void init(),
I obviously need to extend Canvas at some stage in order to be able to use bufferStrategy.
But how would I do this?
And if I don't need to use JFrame, how do I then specify the window specifics? Unless there is a way to call JFrame into an applet?

I'm really not getting this. I understand how it's supposed to look, but because I've used bufferStrategy and Canvas I think i've confused myself now that I'm trying to simplify it. I read somewhere you can't use Canvas in an applet and thus bufferStrategy?

Offline zoto

Senior Devvie


Medals: 4



« Reply #4 - Posted 2011-10-18 22:29:42 »

This is what I was trying to explain, Frame and Applet extend Container they serve the same basic purpose so you only need 1 or the other depending on your distribution method.

You could still use a canvas with either just have 2 classes one that extends applet and 1 that extends canvas and use applet.add(canvas).

it would probably be a good idea to read up on some basic OOP ideas before you get too much further in.
Offline philfrei
« Reply #5 - Posted 2011-10-19 00:25:37 »

Quote
And if I don't need to use JFrame, how do I then specify the window specifics?

You specify the size, in part, in the HTML that calls the Applet, or via a JNLP file, depending upon your calling method, as well as specifying a preferred dimension from within the Java code. (I know with a JNLP file, you can inspect the HTML from within Java to find out how much room you have to fill up. My memory is poor and I can't recall if you can do this with the calling HTML as well.)

There is a nifty design pattern of using a routine called "createGUI()" to handle all your GUI building within your container of choice. If you do it right, EITHER JApplet or JFrame can call CreateGUI, making it easy to convert from Applet to Application or vice versa.

I'm looking for the section that discusses this. So far, only found this example:
http://download.oracle.com/javase/tutorial/uiswing/components/applet.html

Ah, here is the discussion...a little more terse than I remembered...
"Benefits of Separating Core Functionality From the Final Deployment Mechanism" at the bottom of this link:
http://download.oracle.com/javase/tutorial/deployment/webstart/developing.html

All of this is stuff in the tutorials that ra4king previously linked.

"It's after the end of the world! Don't you know that yet?"
Offline gladwda

Junior Newbie





« Reply #6 - Posted 2011-10-23 14:35:56 »

Thanks for your reply guys!
I've saved the links for reading later on.

I have decided to drop the JFrame concepts as clearly the size etc can be formed in the applet html.
However what I need now is for the game to extend JApplet, but I also need to add Canvas.

Can somebody show me what this code should look like? If I wanted to include Canvas in my applet?
I have tried Game extends Applet {

however it comes up with an error extending it, however I can extend JApplet with no issue.
Does this matter?

I then can just pop applet.add(Canvas);
into my Game() constructor yes?

I'm getting there I promise! Smiley
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-10-23 15:50:12 »

You don't need to extend Canvas. You can just instantiate a Canvas object by doing "new Canvas()" and then create and get its BufferStrategy.

Also, if you aren't going to use Swing widgets, use Applet instead of JApplet.

A basic applet:
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  
public class MyApplet extends Applet implements Runnable {
    private Canvas canvas;
   
    public void init() {
        setIgnoreRepaint(true);
       
        canvas = (Canvas)add(new Canvas());
        canvas.setIgnoreRepaint(true);
        canvas.createBufferStrategy(3);
       
        new Thread(this).start();
    }
   
    public void run() {
        BufferStrategy strategy = canvas.getBufferStrategy();
       
        while(true) {
            //update logic
           
            do {
                do {
                    Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
                   
                    //drawing
                   
                    g.dispose();
                }while(strategy.contentsRestored());
               
                strategy.show();
            }while(strategy.contentsLost());
           
            //sleep
        }
    }
}

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.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!