📄 lmimaze.java
字号:
//*****************************************************************************
//
// lmimaze.java - Java applet to display the full maze in real-time on a web
// page.
//
// Copyright (c) 2007 Luminary Micro, Inc. All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws. All rights are reserved. You may not combine
// this software with "viral" open-source software in order to form a larger
// program. Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 1952 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************
//*****************************************************************************
//
// This file is not automatically built as part of the build process; it
// requires the Java Development Kit which is not installed on a typical
// system. The kit can be obtained from www.java.com.
//
// Once installed, this file can be compiled with:
//
// javac -target 1.5 -d html lmimaze.java
//
// Which will produce the Java byte code file and place it into the game
// filesystem.
//
//*****************************************************************************
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
//*****************************************************************************
//
// An applet that draws the maze data read from the quick start application.
//
//*****************************************************************************
public class lmimaze extends Applet implements Runnable
{
byte maze[], monster[], player[];
int buffer, refresh, refreshOne;
Image backbuffer1, backbuffer2;
boolean threadSuspended;
Graphics backg1, backg2;
Thread t = null;
//*************************************************************************
//
// Initializes the applet.
//
//*************************************************************************
public void init()
{
//
// Create an image and drawing context for the first backing image.
//
backbuffer1 = createImage(635, 470);
backg1 = backbuffer1.getGraphics();
//
// Create an image and drawing context for the second backing image.
//
backbuffer2 = createImage(635, 470);
backg2 = backbuffer2.getGraphics();
//
// Set the window background color to black.
//
setBackground(Color.black);
//
// The first backing image should be displayed on the screen first.
//
buffer = 0;
//
// Allocate a byte array to hold the maze data, monster position data,
// and player position data.
//
maze = new byte[127 * 94];
monster = new byte[100 * 4];
player = new byte[4];
//
// Initialize the refresh and single refresh variables.
//
refresh = 1;
refreshOne = 0;
}
//*************************************************************************
//
// Starts the background thread for the applet.
//
//*************************************************************************
public void start()
{
//
// See if the background thread has been created.
//
if(t == null)
{
//
// Create the background thread.
//
t = new Thread(this);
//
// Indicate that the background thread is allowed to run.
//
threadSuspended = false;
//
// Start the background thread.
//
t.start();
}
else
{
//
// See if the background thread is currently suspended.
//
if(threadSuspended)
{
//
// Indicate that the background thread is allowed to run.
//
threadSuspended = false;
//
// Syncronously notify the background thread.
//
synchronized(this)
{
notify();
}
}
}
}
//*************************************************************************
//
// Suspend the background thread.
//
//*************************************************************************
public void stop()
{
//
// Indicate that the background thread is not allowed to run.
//
threadSuspended = true;
}
//*************************************************************************
//
// The thread code for the background thread.
//
//*************************************************************************
public void run()
{
//
// Catch exceptions that may occur during execution of the background
// thread.
//
try
{
//
// Loop forever.
//
while(true)
{
//
// See if the background thread is suspended, or if screen
// refresh has been turned off.
//
if(threadSuspended || ((refresh == 0) && (refreshOne == 0)))
{
//
// Synchronize with the main thread.
//
synchronized(this)
{
//
// Wait while the background thread is suspended or if
// screen refrehs has been turned off.
//
while(threadSuspended ||
((refresh == 0) && (refreshOne == 0)))
{
wait();
}
}
}
//
// Catch excpetions that may occur during the web accesses.
//
try
{
HttpURLConnection connection;
InputStream in;
int x, y, idx;
Graphics g;
URL server;
//
// Construct the URL to the maze data file.
//
server = new URL(getCodeBase() + "maze.dat");
//
// Create a connection to the URL.
//
connection = (HttpURLConnection)server.openConnection();
//
// Disable caching.
//
connection.setUseCaches(false);
//
// Connect to the server.
//
connection.connect();
//
// Get an input stream for the maze data file.
//
in = connection.getInputStream();
//
// Read the maze data file from the server. A loop is
// needed since all the data may not be returned by a
// single read.
//
idx = 0;
while(idx != (127 * 94))
{
x = in.read(maze, idx, (127 * 94) - idx);
if(x == -1)
{
break;
}
idx += x;
}
//
// Close the connection to the server.
//
connection.disconnect();
//
// Construct the URL to the monster position data file.
//
server = new URL(getCodeBase() + "monster.dat");
//
// Create a connection to the URL.
//
connection = (HttpURLConnection)server.openConnection();
//
// Disable caching.
//
connection.setUseCaches(false);
//
// Connect to the server.
//
connection.connect();
//
// Get an input stream for the monster position data file.
//
in = connection.getInputStream();
//
// Read the monster position data file from the server. A
// loop is needed since all the data may not be returned by
// a single read.
//
idx = 0;
while(idx != (100 * 4))
{
x = in.read(monster, idx, (100 * 4) - idx);
if(x == -1)
{
break;
}
idx += x;
}
//
// Close the connection to the server.
//
connection.disconnect();
//
// Construct the URL to the player position data file.
//
server = new URL(getCodeBase() + "player.dat");
//
// Create a connection to the URL.
//
connection = (HttpURLConnection)server.openConnection();
//
// Disable caching.
//
connection.setUseCaches(false);
//
// Connect to the server.
//
connection.connect();
//
// Get an input stream for the player position data file.
//
in = connection.getInputStream();
//
// Read the player position data file from the server. A
// loop is needed since all the data may not be returned by
// a single read.
//
idx = 0;
while(idx != 4)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -