package gametemplate; import java.awt.*; import java.awt.image.*; import javax.imageio.ImageIO; import java.io.*; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; public abstract class Game { GameApplet applet; GamePanel gp; public final void setup(GameApplet a,GamePanel g){ gp=g; applet=a; init(); } //Override this method if you want to do something before the game starts. public void init(){ } //You must override this method. In it, you should first update the game status, //And then paint in the screen using Graphics g. public abstract void paint(Graphics g); //This method will be called whenver the aplet is closed. Override it if you want to do //something before closing the applet. public void stop(){ } //Use this method to set the time that will pass between every frame refresh. //It is in milliseconds public final void setSleepTime(int ms){ gp.setSleepTime(ms); } //Use this method to set the width and height of your game panel. I recommend to use this //method only in the beggining of the game, because resizing the dimensions can mess up //some game variables. public final void resize(int w, int h){ applet.resize(w, h); } //Use this method to import images you made into the game. I would reccomend using //.png images if you need transparency, and .jpg images if you need to save storage space //and decrease loading times. //Null will be returned if the program will not find the image. public final Image readBufferedImage(String name){ try{ Image i=applet.getImage(applet.getCodeBase(),name); return i; } catch (Exception e){ e.printStackTrace(System.err); } return null; } //Override this method if you want to set the background color yourself. //The default color is black. This method is being called every time the //screen is redrawn. If null will be returned, no background will be drawn. //Ise this to save efficiency. public Color getBackgroundColor(){ return Color.black; } //A useful method to determine the mouse position. Note that null will be returned //if the mouse is not in the GamPanel, so check it before you use it. public final Point getMousePosition(){ return gp.getMousePosition(); } //The next five are mouse input methods. Override them to handle mouse input. //Use the methods of MouseEvent to find information about the mouse position //and what button was clicked. public void mousePressed(MouseEvent e){ } public void mouseClicked(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } //The next three are keyboard input methods. Override them to handle keyboard input //use KeyEvent methods to find out what key was pressed public void keyPressed(KeyEvent e){ } public void keyTyped(KeyEvent e){ } public void keyReleased(KeyEvent e){ } //A method for setting mouse cursors. You need to supply the image you want to use as //cursor, the hotspot (the actual point in the image which is where the mouse points. For //regular cursors (0,0) will be fine, but use the cnter of the image for a crosshair cursor), // and the cursor name (I do not think that it affects anything) //If there will be a problem with the image, a transparent cursor will be set. //You can also use the setTransparentCursor method, and then use getMousePosition in //the paint method to draw something wherever the cursor is. I would reccomend this option //if you want the mouse cursor to change (animated cursor). public final void setCursor(Image i, Point hotspot, String name){ if(i==null){ setTransparentCursor(); return; } gp.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(i, hotspot, name)); } //Sets a transparent mouse cursor (making it invisible). public final void setTransparentCursor(){ int[] pixels = new int[16 * 16]; Image image = Toolkit.getDefaultToolkit().createImage( new MemoryImageSource(16, 16, pixels, 0, 16)); gp.setCursor(Toolkit.getDefaultToolkit().createCustomCursor (image, new Point(0, 0), "invisiblecursor")); } }