This is for anyone who might find it helpful. This template...
* uses double-buffering, which can help game-makers fix that glitchy screen problem
* can be exported as a runnable jar file. If you're like me and had trouble making your Applet executable, this is a possible fix.
* Additionally it resizes the interior of the frame, not the frame itself
* is easily customizable. Take out or add in whatever you need to make it work, it's all yours
package applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class quasiApplet extends JFrame
implements ActionListener
{
/*-----add other variables here-----*/
//game/animation
Timer paintTimer; int paintDelay = 30;//ms
Timer calcTimer; int calcDelay = 30;//ms
//double-buffering
Image dbImage;
//flags
boolean readyToRockNRoll = false;
//starting the applet==============================
public void launch()
{
/*-----initialize things here-----*/
this.setVisible(true);
this.setFocusable(true);
this.requestFocus();
calcTimer = new Timer(calcDelay, this);
paintTimer = new Timer(paintDelay, this);
calcTimer.start(); paintTimer.start();
readyToRockNRoll = true;
}
//visual stuff==============================
@Override public void paint(Graphics g)
{
if(readyToRockNRoll)//this is necessary because the frame paints itself before the launch() method is called
{
/*draw things here*/
}
}
@Override public void resize(int width, int height)
{
this.getContentPane().setPreferredSize(new Dimension(width, height));
this.pack();
}
public void dbRedraw()
{
//create db components if needed
if(dbImage==null)
{
dbImage = this.createImage(this.getWidth(), this.getHeight());
} else {
//draw everything to backstage image
this.paint(dbImage.getGraphics());
//draw final image to screen
this.getContentPane().getGraphics().drawImage(dbImage, 0, 0, null);
}
}
//computational stuff==============================
public void recalc()
{
/*-----update model here-----*/
}
/*-----add miscellaneous methods here-----*/
//fundamental stuff==============================
public static void main(String[] args)
{
quasiApplet applet = new quasiApplet();
applet.launch();
}
@Override public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(paintTimer))
dbRedraw();
else if(e.getSource().equals(calcTimer))
recalc();
}
}