📄 the java game programming tutorial part ii.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://www.intergate.bc.ca/personal/iago/javatut/jtp2.htm -->
<HTML><HEAD><TITLE>The Java Game Programming Tutorial: Part II</TITLE>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<META content="Garry Morse" name=Author>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content="An expanding tutorial on Java 2D/3D game programming"
name=Description>
<META
content=Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java
name=KeyWords></HEAD>
<BODY aLink=#ff0000 bgColor=#e9d3fe link=#0000ee text=#000000 vLink=#551a8b>
<H1><A href="http://www.intergate.bc.ca/personal/iago/javatut/index.htm"><IMG
align=ABSCENTER border=0 height=85
src="The Java Game Programming Tutorial Part II_ficheiros/coffee.gif"
width=116></A><FONT color=#804040><FONT size=+2><A
href="http://www.intergate.bc.ca/personal/iago/javatut/index.htm">The Java Game
Programming Tutorial</A></FONT></FONT></H1>
<H1>
<HR width="100%">
</H1>
<H2>Part II: What about Threads?</H2>
<UL>
<P>Programs run from start to finish, executing a single thread of<BR>control.
Tasks are performed in linear sessions, where one task<BR>must wait for
another task to be completed before it can have its<BR>turn. This is the
schematic of a single-threaded program. </P>
<P>Naturally, multithreading is the execution of several threads at<BR>the
same time where each task is performed in parallel with other<BR>tasks. Each
thread receives a priority value and is allocated a<BR>certain amount of
system time or attention based on this. </P>
<P>Threads apply well to Java because each applet can run in its own
<BR>thread without interferring with or hogging system resources. On<BR>web
pages, this enables users to download files in the background<BR>while
listening to sounds and viewing animations. More than one<BR>applet can run on
the same page and each will receive an amount<BR>of time to advance in its
current task. </P>
<P>I will now step through parts of the next thread example (Lines.java):</P><PRE><B> public class Lines extends Applet implements Runnable {</B></PRE>
<P>The format here is the same as for the class Hello in <A
href="http://www.intergate.bc.ca/personal/iago/javatut/jtp1.htm">Part 1</A>
except<BR>it now implements the runnable interface, meaning that Lines is
a<BR>multithreaded applet class.</P><PRE> <B>Thread lineThread;</B></PRE>
<P>This is a thread variable declaration for the operation of<BR>recalculating
and drawing lines. When an operation continues<BR>for a long time, as an
animation, it is often given its own<BR>thread separate from the applet
thread. </P><PRE><B> int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;</B></PRE><PRE><B> float red, green, blue;
Color lineColor;</B></PRE>
<P>These are just variable declarations for the line coordinates,<BR>drawn
from (x1,y1) to (x2,y2) and the line color arguments. </P><PRE><B> public void init() {
setBackground(Color.black);
}</B></PRE>
<P>As discussed in Part 1, this is the first stage in the
applet's<BR>activity; the init method. Here, our method will override
the<BR>default init method, and set the background color to black. </P><PRE><B> public void start() {
if(lineThread==null) {
lineThread = new Thread(this);
lineThread.start();
}
}</B></PRE>
<P>The next stage is the start method above which creates a new<BR>thread if
it has not already been created. The new keyword<BR>helps to allocated dynamic
memory for the line's thread before<BR>it is called to start. </P><PRE><B> public void stop() {
if(lineThread!=null) {
lineThread.stop();
lineThread = null;
}
}</B></PRE>
<P>The stop method is the second to last stage of the applet life<BR>cycle
which mirrors the start method. It checks to see if the<BR>thread is still
running and if so, it is stopped and de-allocated. </P><PRE><B> public void paint(Graphics g) {
g.setColor(lineColor);
g.drawLine(x1,y1,x2,y2);
}</B></PRE>
<P>This is the paint method which receives the graphics context or<BR>applet
window as a parameter. After the window is erased, this<BR>method is called to
draw graphics whenever the window is changed.<BR>Our method simply sets the
drawing color to the current line<BR>color and draws a single line, using the
variable coordinates. </P>
<P>The next method is more complex so I will try to break it down<BR>into
parts. All the code within it runs within its own thread.<BR>This is the run
method, required because we declared the class<BR>as runnable. </P><PRE><B> public void run() {
while(true) {</B></PRE>
<P>This statement starts an infinite loop within the run method to<BR>keep the
thread going forever until the applet is shut down. </P><PRE><B> x1 = (int)(Math.random() * this.size().width);
x2 = (int)(Math.random() * this.size().width);
y1 = (int)(Math.random() * this.size().height);
y2 = (int)(Math.random() * this.size().height);
red = (float)Math.random();
green = (float)Math.random();
blue = (float)Math.random();
lineColor = new Color(red,green,blue);</B></PRE>
<P>These code fragments are less important to threads but I will<BR>explain.
The next line coordinates are random numbers within<BR>the applet window
boundaries. Three random color arguments<BR>are also generated to reset the
line color. </P><PRE><B> repaint();</B></PRE>
<P>Calling repaint ensures that our paint method is called, which<BR>draws a
new line using the freshly calculated coordinates/color. </P><PRE><B> try {
lineThread.sleep(50);
}
catch(InterruptedException e) {}</B></PRE><PRE><B> }
}</B></PRE>
<P>This is the end of the run method but the code above is very<BR>important.
Without this part in the infinite loop of the run<BR>method, the line would be
drawn too fast and not allow other<BR>applets to execute as efficiently. Thus,
the line thread is<BR>told to go to sleep for 50 milliseconds. This
conveniently<BR>delays our drawing and allows other threads to go to
work.<BR>This is a common way of doing things for simple applets. </P><PRE><B> }</B></PRE>
<P>Already, we've reached the end of the applet! </P>
<P>Here is the HTML for a blank web page containing the Lines applet: </P>
<UL>
<P><B><HTML><BR><APPLET CODE="Lines.class" WIDTH=100
HEIGHT=100><BR></APPLET><BR></HTML></B></P></UL>
<P>Here's the complete Java code: </P>
<UL><PRE><B>// Lines.java
// by Garry Morse
import java.awt.*;
import java.applet.*;
public class Lines extends Applet implements Runnable {
Thread lineThread;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
float red, green, blue;
Color lineColor;
public void init() {
setBackground(Color.black);
}
public void start() {
if(lineThread==null) {
lineThread = new Thread(this);
lineThread.start();
}
}
public void stop() {
if(lineThread!=null) {
lineThread.stop();
lineThread = null;
}
}
public void paint(Graphics g) {
g.setColor(lineColor);
g.drawLine(x1,y1,x2,y2);
}
public void run() {
while(true) {
x1 = (int)(Math.random() * this.size().width);
x2 = (int)(Math.random() * this.size().width);
y1 = (int)(Math.random() * this.size().height);
y2 = (int)(Math.random() * this.size().height);
red = (float)Math.random();
green = (float)Math.random();
blue = (float)Math.random();
lineColor = new Color(red,green,blue);
repaint();
try {
lineThread.sleep(50);
}
catch(InterruptedException e) {}
}
}
}</B></PRE></UL></UL>
<P><FONT size=+1><A
href="http://www.intergate.bc.ca/personal/iago/javatut/lines.htm">Click here to
see the Lines applet in action!</A></FONT></P>
<P><FONT color=#ffacff><FONT size=-2>The Java Game Programming Tutorial and all
tutorials within are created by Garry Morse, Copyright
1997</FONT></FONT></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -