canvas into applet?

hey guys :), i never used applet before , and i want to put my game into an applet, i have a class that exteds of canvas and i use buffer strategy, in this class i have a method

public Canvas getCanvas(){
  return this;
}

and i tried to add it in an applet and didnt work…

could you explain me how i have to do it?

Post the applet code

package com.jdiv.samples.zelda.applet;

import java.applet.Applet;
import java.awt.Panel;
import java.net.URL;

import com.jdiv.JDiv;

public class Main extends Applet{

static Link link;
static int fpg_enemigo,fpg_ambiente;

	public void init(){

	URL urlLink= getClass().getClassLoader().getResource("res/fpg/link.fpg"),
		urlEnemigo=getClass().getClassLoader().getResource("res/fpg/enemigo.fpg");
		
		JDiv.load_fpg(urlLink);
		fpg_enemigo=JDiv.load_fpg(urlEnemigo);
	
		link=new Link();
		new Enemigo();

		Panel panel = new Panel();
                panel.add(JDiv.getCanvas());
                add(panel);
		
		
	}
	public void start(){
		JDiv.appletInit(JDiv.m320x240);
	}
	
}

this code below is the Class who extends Canvas


public void appletInit(int resolution){
		 	setResolution(resolution);
			coreInit();	 
	 }

private void setResolution(int resolution){
	 int width=0, height=0;
	   	switch (resolution){
	  		case 0: width=320; height=200; break;
	  		case 1: width=320; height=240; break;	
	  		case 2: width=320; height=400; break;	
	  		case 3: width=360; height=240; break;	
	  		case 4: width=360; height=260; break;	
	  		case 5: width=376; height=282; break;	
	  		case 6: width=400; height=300; break;	
	  		case 7: width=640; height=400; break;	
	  		case 8: width=640; height=480; break;	
	  		case 9: width=800; height=600; break;	
	  		case 10: width=1024; height=768; break;	
		  }
		this.width=width;
	   	this.height=height;  
 }
	

public void coreInit(){
		
		
	     regions.add(new JRegion(0,0,0,getWidth(),getHeight()));	    
	 
	     addKeyListener(this);    
	   	
	  	 createBufferStrategy(2);
		 bStrategy = getBufferStrategy();  
		 gBackBuffer =(Graphics2D)bStrategy.getDrawGraphics();
		 
		 clearImg=new BufferedImage(getWidth(), getHeight(),BufferedImage.TYPE_INT_RGB);
		 clearImg.createGraphics();
		 Graphics g= clearImg.getGraphics();
		 
		 g.setColor(Color.black);
		 g.fillRect(0,0,getWidth(),getHeight());
		 
		  
                     new JUpdate().start();	  
		     new JLoop().start();	  
		     
		      
	}


this code is called in JUpdate Thread


public void  update(){
 
updatePaint(gBackBuffer);
fps();
bStrategy.show();	
mFpsCount++;
}

all the paint stuff

 

 public void updatePaint(Graphics2D g){
  
  g.drawImage(clearImg,0,0,null);
  
  paintBackground(g);
  paintProcess(g); 
  paintWrites(g);
 
}


public void update( Graphics g ) {

	 if (bStrategy!=null)   bStrategy.show();	
  
  }

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

it works perfectly in a Frame but it doesnt in an Applet

this the code for the frame

                                                                   //title, canvas,w,h
 	JDivWindow window=new JDivWindow(title,this,width,height);
   	window.addKeyListener(this);
   	window.setVisible(true);
   

Constructor of JDivWindow

	public JDivWindow(String title,Canvas canvas,int width,int height){
		
	setTitle(title);
	setSize(new Dimension(width+6,height+32));
	setLayout(null);
		
	    addWindowListener( new WindowAdapter() {
	          public void windowClosing(WindowEvent e) {
	            System.exit(0);
	          }
	        });
	  
          canvas.setBounds(3,29, width, height);
	  add(canvas);

	}

I have no idea. You could try to extend JApplet instead of Applet since you are adding a swing panel:

i tried it and didnt work T_T , i dont know what should i do…

The method I use gets me a java program that can run as either an application or an applet. It might not be the best way, but it works :wink: Shouldn’t be too much work to switch to JFrame instead of Frame.

Here is the applet class


public class GameApplet extends Applet {

	private static Frame parentFrame=null;
	private static boolean blnApplet = false;

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

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

	public void init() {
		blnApplet = true;
	}

	public void destroy() {
	}

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

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

And then the frame class


public class GameFrame extends Frame {
	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);
		}
	}
}

thx man, i tried it, but didnt work for me T_T , i use Frame , maybe is my implementation maybe is wrong. I do all on a canvas, i thought is going to be easy, just add the canvas on the applet and that’s it , but it didnt

Can you explain exactly what “didn’t work”? It threw an exception, or nothing got rendered, or what?

My guess is that you didn’t resize your canvas properly. You should either set the preferred size, or
set the size explicitly.

Another thing: do not cache the graphics context of buffer strategy. You are supposed to get it
every time in the rendering loop before rendering, see the example in the top comment:
http://java.sun.com/javase/6/docs/api/java/awt/image/BufferStrategy.html

Dmitri

[quote]Can you explain exactly what “didn’t work”? It threw an exception, or nothing got rendered, or what?
[/quote]
sure, i mean do nothing, nothing got rendered,

[quote]My guess is that you didn’t resize your canvas properly. You should either set the preferred size, or
set the size explicitly.
[/quote]
i added to the init() method


setPreferredSize(new Dimension(320,240));

and nothing

ohh thx dude :smiley: i didnt notice that :D, very good tip

hello people, i posted this topic long time ago, since i couldn’t figer out the problem i stopped try to make it works

so today i openned the code again, and i tried the suggestion that cylab replied in the post, i didn’t tried before i dont know why xD , so i did it, and it worked ! ^^ , i changed Applet to JApplet
and it rendered my custom Canvas

thx cylab, and sorry for my late reply and testing