application to applet

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!

[tr][td]
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() {
[/td][/tr]

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:

You are missing java.

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.

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.

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?

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.

[quote]And if I don’t need to use JFrame, how do I then specify the window specifics?
[/quote]
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.

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

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() {
        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
        }
    }
}