Basic Applet in a web site(Applet基础)

Let's start with basic.

You want to put your game on your website but you don't know how to do it?

To do it, you will need the following conditions :

1. Your game need to be an applet.
2. You need to have a server where you can upload your code and your html page.
3. You need to package your applet in a jar file.
4. You need to write an html page to display your applet.
5. You need to upload the jar file containing the applet and the html page on your server.

Ok it might seems a bit of work at first but let's do it one step at a time.

TIP : If you are new to making game you should start with this tutorial :Basic Game

1. Making an applet

Here is a template that I use for my applet games. To make your own game, you just have to add your code in the render(Graphics2D g) and update(int deltaTime) methods. As you will see, it's very similar to the code in the Basic Game tutorial so you don't need to restart all over if you have already a finished game.

I add a little test so you can run this code directly to see what it does.

Code:
import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;

public class BasicApplet extends Applet implements Runnable{
	
	private final int width = 800;
	private final int height = 600;

	Canvas canvas;
	BufferStrategy bufferStrategy;
	Thread gameloopThread;
	
	@Override
	public void init(){
		canvas = new Canvas();
		canvas.setBounds(0, 0, width, height);
		add(canvas);
		canvas.setIgnoreRepaint(true);

		canvas.createBufferStrategy(2);
		bufferStrategy = canvas.getBufferStrategy();

		canvas.addMouseListener(new MouseControl());
		canvas.addMouseMotionListener(new MouseControl());
		canvas.requestFocus();
	}
	
	@Override
	public void start(){
		gameloopThread = new Thread(this);
		gameloopThread.start();
	}
	
	@Override
	public void stop(){
		setRunning(false);
		try {
			gameloopThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private class MouseControl extends MouseAdapter{
		
	}
	
	private long desiredFPS = 60;
        private long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
    
	private boolean running = true;
	
	private synchronized void setRunning(boolean running){
		this.running = running;
	}
	
	private synchronized boolean isRunning(){
		return running;
	}
	
	public void run(){
		
		setRunning(true);
		
		long beginLoopTime;
		long endLoopTime;
		long currentUpdateTime = System.nanoTime();
		long lastUpdateTime;
		long deltaLoop;
		
		while(!isActive()){
			Thread.yield();
		}
		while(isRunning()){
			beginLoopTime = System.nanoTime();
			
			render();
			
			lastUpdateTime = currentUpdateTime;
			currentUpdateTime = System.nanoTime();
			update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
			
			endLoopTime = System.nanoTime();
			deltaLoop = endLoopTime - beginLoopTime;
	        
	        if(deltaLoop > desiredDeltaLoop){
	            //Do nothing. We are already late.
	        }else{
	            try{
	                Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
	            }catch(InterruptedException e){
	                //Do nothing
	            }
	        }
		}
	}

	private void render() {
		try{
			Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
			g.clearRect(0, 0, width, height);
			render(g);
			bufferStrategy.show();
			g.dispose();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//TESTING
	private double x = 0;
	
	/**
	 * Overwrite this method in subclass
	 */
	protected void update(int deltaTime){
		x += deltaTime * 0.2;
		while(x > 500){
			x -= 500;
		}
	}
	
	/**
	 * Overwrite this method in subclass
	 */
	protected void render(Graphics2D g){
		g.fillRect((int)x, 0, 200, 200);
	}



}
2. Getting a webhost to display html page

If you want to put stuff on the internet you need to have a server. There is plenty of free web host solutions around. Check out this page : http://www.free-webhosts.com/webhosting-01.php

3. Packing your applet in a JAR file

a. I put all my class files and ressources (images, music) in an empty folder.
b. I open the command prompt and navigate to this folder.
c. I type the following command : jar cvf Test.jar *

If you need some help to use command prompt with java command check this link : http://www.skylit.com/javamethods/faqs/javaindos.html
If you want more info about building jar file check this link : http://download.oracle.com/javase/tutorial/deployment/jar/build.html

4. Writing your html page

Here is a simplified version of an html page just to give you an idea. The essential part is the <applet> tag. You can customize the rest of the page
Code:
<html> 
 
<head> 
<title>Applet test</title> 
</head> 
 
<body> 
<h2>Applet test</h2> 
 
<applet code = 'basicGame.BasicApplet3' 
    archive = 'Test.jar', 
    width = 800, 
    height = 600 /> 
    
</body> 
 
</html>


5. Upload your jar and html page on your web host.

Usually you can access to the files on your webhost server with an ftp client. To add your files on your server, you simply need to download an ftp client ( filezilla), enter your host, username and password and drag and drop the files from your computer to the server.

Here is the result of these steps : Applet Test

References

Applet : http://download.oracle.com/javase/tutorial/deployment/applet/lifeCycle.html
Webhost : http://www.free-webhosts.com/webhosting-01.php
Command prompt : http://www.skylit.com/javamethods/faqs/javaindos.html
JAR File : http://download.oracle.com/javase/tutorial/deployment/jar/build.html
Applet tag : http://download.oracle.com/javase/tutorial/deployment/applet/html.html
Filezilla : http://filezilla-project.org/

Discussion

You can find a discussion about the content of this tutorial here :

Applet
JAR and deployment


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值