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