Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (755)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
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 2160 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));
      theFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
      addKeyListener(new KeyInputHandler());
      buffage = getBufferStrategy();
   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=>java</a>.

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: 508
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 »

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:

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:

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

music and music apps:
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: 508
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:
public class MyApplet extends Applet implements Runnable {
    private Canvas canvas;
    public void init() {
        canvas = (Canvas)add(new Canvas());
        new Thread(this).start();
    public void run() {
        BufferStrategy strategy = canvas.getBufferStrategy();
        while(true) {
            //update logic
            do {
                do {
                    Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

Pages: [1]
  ignore  |  Print  

DesertCoockie (36 views)
2018-05-13 18:23:11

nelsongames (81 views)
2018-04-24 18:15:36

nelsongames (73 views)
2018-04-24 18:14:32

ivj94 (755 views)
2018-03-24 14:47:39

ivj94 (85 views)
2018-03-24 14:46:31

ivj94 (626 views)
2018-03-24 14:43:53

Solater (101 views)
2018-03-17 05:04:08

nelsongames (182 views)
2018-03-05 17:56:34

Gornova (408 views)
2018-03-02 22:15:33

buddyBro (1068 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!