📄 mappytest.java
字号:
//
// ______________________
// ------------------
// * Mappy Test *
// __________________
// ----------------------
//
// (c) Slimer 2001
//
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.util.Date;
//------------------------------------------------------------------------------
/**
* Test Applet for the Mappy Playback Library v1.23. <br>
* <br>
* This test Applet gives you a brief idea how to use the Java Mappy Playback
* Library. I don't understand why everybody's into Applets myself, I think
* they're crap for graphical arcade games! (No vertical sink for starters!..) <br>
* <br>
* Future releases of Java Mappy are very likely <b>not</b> to be JDK1.1
* compatible (more likely to be JDK1.4 compatible!) and so will not work with a
* standard Applet. <br>
* <br>
* Required Files: MappyTest.java Mappy.java TEST.FMP<br><br>
* @version 1.00 (11 December 2001)
* @author <a href="mailto:slimer@alienfactory.co.uk">Slimer</a> of <a href="http://www.alienfactory.co.uk">Alien-Factory</a>.
**/
//------------------------------------------------------------------------------
public class MappyTest extends Applet {
private int m_intWidth = 320; // dimensions of map to display on screen
private int m_intHeight = 256;
private long m_lngTime = 25; // the no. of ms that should pass between each frame, 33 ~ 30Hz
private int m_intInitX = 500; // initial coordinates
private int m_intInitY = 700;
private int m_intLimitX1 = 0;
private int m_intLimitX2 = 0;
private int m_intLimitY1 = 800;
private int m_intLimitY2 = 800;
private int m_intIncX = 2; // how fast to scroll the map
private int m_intIncY = 2;
private int m_intStyle = 0; // 0=bounce, 1=reset
private String m_stringMap;
private transient boolean m_blnAnimateMap = false; // is set to true if the map is moving
private transient Image m_imgBuffer; // the double buffer
private transient Mappy m_mappy; // the Mappy playback library
/**
* Called by the Browser to initialise the <code>Applet</code>.
**/
public void init () {
//get user params from html
m_intWidth = getMyParam ("Width");
m_intHeight = getMyParam ("Height");
m_lngTime = getMyParam ("Time");
m_intLimitX1 = getMyParam ("LimitX1");
m_intLimitX2 = getMyParam ("LimitX2");
m_intLimitY1 = getMyParam ("LimitY1");
m_intLimitY2 = getMyParam ("LimitY2");
m_intInitX = getMyParam ("InitX");
m_intInitY = getMyParam ("InitY");
m_intIncX = getMyParam ("IncX");
m_intIncY = getMyParam ("IncY");
m_intStyle = getMyParam ("Style");
m_stringMap = "TEST.FMP";
m_stringMap = getParameter ("FMPName");
BufferedInputStream stream;
// grab map as stream
stream = new BufferedInputStream (getClass().getResourceAsStream(m_stringMap));
// initialise mappy
m_mappy = new Mappy();
m_mappy.loadMap(stream, this);
m_mappy.setScreenDimensions(0, 0, m_intWidth, m_intHeight);
m_mappy.setCurrentMapLayer(0);
m_mappy.setX(m_intInitX);
m_mappy.setY(m_intInitY);
// create double buffer
m_imgBuffer = createImage(m_intWidth, m_intHeight);
}
/**
* Called by the Browser to start the <code>Applet</code>.
**/
public void start() {
// Use this inner class to hide the public run method
Runnable r = new Runnable() {
public void run() {
animateMap();
}
};
// start a seperate thread to scroll the map
m_blnAnimateMap = true;
Thread animateMap = new Thread(r, "animateMap");
animateMap.start();
}
/**
* Called by the Browser to stop the <code>Applet</code>.
**/
public void stop() {
m_blnAnimateMap = false;
}
/**
* Called by the Browser when <code>Applet</code> is about to die.
**/
public void destroy() {
m_blnAnimateMap = false;
m_imgBuffer.flush();
}
/**
* Draws the double buffer onto the screen
**/
public void paint (Graphics gfx) {
if (gfx == null) return;
if (m_imgBuffer == null) return;
gfx.drawImage(m_imgBuffer, 0, 0, null);
}
/**
* Need to override this to prevent the screen being white-washed!
**/
public void update(Graphics gfx) { }
/**
* This is the thread that animates the map
**/
private void animateMap() {
Graphics gfx;
long lngTime;
while (m_blnAnimateMap) {
lngTime = System.currentTimeMillis(); //(new Date()).getTime();
// update map coordinates
m_mappy.addX(m_intIncX);
if (m_mappy.getX() <= m_intLimitX1) {
if (m_intStyle == 0) m_intIncX = -m_intIncX;
else m_intIncX = m_intInitX;
}
if (m_mappy.getX() >= m_intLimitX2) {
if (m_intStyle == 0) m_intIncX = -m_intIncX;
else m_intIncX = m_intInitX;
}
m_mappy.addY(m_intIncY);
if (m_mappy.getY() <= m_intLimitY1) {
if (m_intStyle == 0) m_intIncY = -m_intIncY;
else m_intIncY = m_intInitY;
}
if (m_mappy.getY() >= m_intLimitY2) {
if (m_intStyle == 0) m_intIncY = -m_intIncY;
else m_intIncY = m_intInitY;
}
m_mappy.animateBlocks();
// render the new coordinates to the buffer
gfx = m_imgBuffer.getGraphics();
m_mappy.drawBackground(gfx, false);
m_mappy.drawForeground(gfx, 1);
gfx.dispose();
// render the buffer to the screen
gfx = getGraphics();
paint(gfx);
gfx.dispose();
// make sure at least the same amount of time has passed
while((System.currentTimeMillis() - lngTime) < (m_lngTime - 5)) {
// Sleep little one, sleep!
try { Thread.sleep(5); } catch (InterruptedException e) { }
}
}
}
/**
* Returns a value from the html parameters.
**/
private int getMyParam (String paramname) {
int i;
try {
i = Integer.parseInt (getParameter (paramname));
}
catch (NumberFormatException e) { return 0; }
return i;
}
/**
* Returns applet info.
**/
public String getAppletInfo () {
return "Test Applet for Mappy Playback Library for Java v1.23 (Release 4) <slimer@alienfactory.co.uk> <mappy@tilemap.co.uk>";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -