Java 小程序无法正确绘制
这是主要代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
public class AlcatrazBreakout extends JApplet implements KeyListener, FocusListener {
int choice=0,curloc=0;
boolean focus = false;
boolean left=false, right=false, up=false, down=false, space=false, enter=false;
public double randNum(Graphics canvas)
{
double x;
x=Math.random()*100;
return x;
}
public void init()
{
addKeyListener(this);
addFocusListener(this);
play(getCodeBase(), "SpyHunter.mid");
}
public void paint(Graphics canvas)
{
clearScreen(canvas);
canvas.setColor(Color.BLACK); // Must do this so words will
// display on the screen
if (!focus)
{
canvas.setColor(Color.WHITE);
canvas.drawString("Welcome to Alcatraz Breakout! The objective of the game is to escape Alcatraz.",10,10);
canvas.drawString("Click the screen to begin the game.",10,30);
return;
}
else if (focus)
{
startbody(canvas);
}
}
public void showstats(Graphics canvas)
{
int hp=10,hpmax=10,defense=0,attack=0;
canvas.drawString("HP: " + hp ,1750,10);
canvas.drawString("/" + hpmax,1790,10);
canvas.drawString("Attack: " + attack,1750,30);
canvas.drawString("Defense: " + defense,1750,50);
}
public void startbody(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
showstats(canvas);
canvas.drawString("You are in your cell.",0,10);
canvas.drawString("Left: To the Training Room.",0,30);
canvas.drawString("Right: To the Showers",0,50);
curloc=0;
makeMove(canvas,curloc, left,right,up,down);
canvas.drawString(" "+curloc,0,70);
}
public void train(Graphics canvas)
{
System.out.println("You are at train");
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
showstats(canvas);
canvas.drawString("You are in the training room.",0,10);
canvas.drawString("Right: To your cell.",0,30);
makeMove(canvas,curloc,left,right,up,down);
}
public void showers(Graphics canvas)
{
System.out.println("You are in the showers");
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
showstats(canvas);
canvas.drawString("You are in the showers.",0,10);
canvas.drawString("Left: To your cell.",0,30);
curloc=1;
makeMove(canvas,curloc, left,right,up,down);
}
public void cellBlockC(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("You are in CellBlock C", 0, 10);
canvas.drawString("Left: Back to your cell",0,20);
canvas.drawString("Up: to security room 1", 0, 40);
curloc=2;
makeMove(canvas,curloc, left,right,up,down);
}
public void SecRoom1(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Secrity room 1.", 0, 10);
canvas.drawString("Down: Back to Cell Block C",0,20);
canvas.drawString("Up: Left to Guard Post", 0, 40);
curloc=3;
makeMove(canvas,curloc, left,right,up,down);
}
public void tunnel(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Secrity room 1.", 0, 10);
canvas.drawString("Down: Back to Cell Block C",0,20);
canvas.drawString("Up: Left to Guard Post", 0, 40);
curloc=7;
makeMove(canvas,curloc, left,right,up,down);
}
public void guardPost(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in the Guard Post.", 0, 10);
canvas.drawString("Left: to go to the Court Yard",0,20);
canvas.drawString("Right: to go to Security room 1", 0, 40);
canvas.drawString("Up: to go to Solitary Confinement",0,60);
curloc=4;
makeMove(canvas,curloc, left,right,up,down);
}
public void courtyard(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in the Court Yard", 0, 10);
canvas.drawString("Down: to Security Room 2.",0,20);
canvas.drawString("Up: To Guard Post", 0, 40);
curloc=8;
makeMove(canvas,curloc, left,right,up,down);
}
public void SecRoom2(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Security Room 2.", 0, 10);
canvas.drawString("Up: To the Court Yard", 0, 40);
curloc=9;
makeMove(canvas,curloc, left,right,up,down);
}
public void fight(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your at the Final Fight!!!", 0, 10);
curloc=11;
makeMove(canvas,curloc, left,right,up,down);
}
public void free(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("You Won!!!!", 0, 10);
curloc=11;
makeMove(canvas,curloc, left,right,up,down);
}
public void solitaryConfinement(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Solitary Confinement.", 0, 10);
canvas.drawString("Down: Guard Post",0,20);
curloc=5;
makeMove(canvas,curloc, left,right,up,down);
}
public void makeMove(Graphics canvas,int curloc,boolean left, boolean right, boolean up, boolean down )
{
if (curloc==0)
{
if(left)
{
train(canvas);
left=right=up=down=false;
}
}
}
public void delayGame(int numSecs)
{
int delayTime = 1000 * numSecs;
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < delayTime)
{
endDelay = System.currentTimeMillis();
}
}
public void clearScreen(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,5000,5000);
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left = true;
repaint();
}
if (key == KeyEvent.VK_DOWN)
{
down = true;
repaint();
}
if (key == KeyEvent.VK_RIGHT)
{
right = true;
repaint();
}
if (key == KeyEvent.VK_UP)
{
up = true;
repaint();
}
if (key == KeyEvent.VK_SPACE)
{
space = true;
repaint();
}
if (key == KeyEvent.VK_ENTER)
{
enter = true;
repaint();
}
}
public void focusGained(FocusEvent evt) {focus=true; repaint();}
public void focusLost(FocusEvent evt) {focus=false;}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
public void restart(Graphics g){}
}
,因此 curloc 的编号不会传递到 makeMove 中,并且程序键不起作用。
谢谢
chris
编辑:
You are at train [message repeated 1075 time] You are at train Exception in thread "AWT-EventQueue-1" java.lang.StackOverflowError at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source) at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source) at java.nio.charset.CharsetEncoder.encode(Unknown Source) at sun.nio.cs.StreamEncoder.implWrite(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at java.io.OutputStreamWriter.write(Unknown Source) at java.io.BufferedWriter.flushBuffer(Unknown Source) at java.io.PrintStream.write(Unknown Source) at java.io.PrintStream.print(Unknown Source) at java.io.PrintStream.println(Unknown Source) at AlcatrazBreakout.train(AlcatrazBreakout.java:75) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81)
这是我在 Eclipse 中的控制台输出
Here is the main code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
public class AlcatrazBreakout extends JApplet implements KeyListener, FocusListener {
int choice=0,curloc=0;
boolean focus = false;
boolean left=false, right=false, up=false, down=false, space=false, enter=false;
public double randNum(Graphics canvas)
{
double x;
x=Math.random()*100;
return x;
}
public void init()
{
addKeyListener(this);
addFocusListener(this);
play(getCodeBase(), "SpyHunter.mid");
}
public void paint(Graphics canvas)
{
clearScreen(canvas);
canvas.setColor(Color.BLACK); // Must do this so words will
// display on the screen
if (!focus)
{
canvas.setColor(Color.WHITE);
canvas.drawString("Welcome to Alcatraz Breakout! The objective of the game is to escape Alcatraz.",10,10);
canvas.drawString("Click the screen to begin the game.",10,30);
return;
}
else if (focus)
{
startbody(canvas);
}
}
public void showstats(Graphics canvas)
{
int hp=10,hpmax=10,defense=0,attack=0;
canvas.drawString("HP: " + hp ,1750,10);
canvas.drawString("/" + hpmax,1790,10);
canvas.drawString("Attack: " + attack,1750,30);
canvas.drawString("Defense: " + defense,1750,50);
}
public void startbody(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
showstats(canvas);
canvas.drawString("You are in your cell.",0,10);
canvas.drawString("Left: To the Training Room.",0,30);
canvas.drawString("Right: To the Showers",0,50);
curloc=0;
makeMove(canvas,curloc, left,right,up,down);
canvas.drawString(" "+curloc,0,70);
}
public void train(Graphics canvas)
{
System.out.println("You are at train");
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
showstats(canvas);
canvas.drawString("You are in the training room.",0,10);
canvas.drawString("Right: To your cell.",0,30);
makeMove(canvas,curloc,left,right,up,down);
}
public void showers(Graphics canvas)
{
System.out.println("You are in the showers");
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
showstats(canvas);
canvas.drawString("You are in the showers.",0,10);
canvas.drawString("Left: To your cell.",0,30);
curloc=1;
makeMove(canvas,curloc, left,right,up,down);
}
public void cellBlockC(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("You are in CellBlock C", 0, 10);
canvas.drawString("Left: Back to your cell",0,20);
canvas.drawString("Up: to security room 1", 0, 40);
curloc=2;
makeMove(canvas,curloc, left,right,up,down);
}
public void SecRoom1(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Secrity room 1.", 0, 10);
canvas.drawString("Down: Back to Cell Block C",0,20);
canvas.drawString("Up: Left to Guard Post", 0, 40);
curloc=3;
makeMove(canvas,curloc, left,right,up,down);
}
public void tunnel(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Secrity room 1.", 0, 10);
canvas.drawString("Down: Back to Cell Block C",0,20);
canvas.drawString("Up: Left to Guard Post", 0, 40);
curloc=7;
makeMove(canvas,curloc, left,right,up,down);
}
public void guardPost(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in the Guard Post.", 0, 10);
canvas.drawString("Left: to go to the Court Yard",0,20);
canvas.drawString("Right: to go to Security room 1", 0, 40);
canvas.drawString("Up: to go to Solitary Confinement",0,60);
curloc=4;
makeMove(canvas,curloc, left,right,up,down);
}
public void courtyard(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in the Court Yard", 0, 10);
canvas.drawString("Down: to Security Room 2.",0,20);
canvas.drawString("Up: To Guard Post", 0, 40);
curloc=8;
makeMove(canvas,curloc, left,right,up,down);
}
public void SecRoom2(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Security Room 2.", 0, 10);
canvas.drawString("Up: To the Court Yard", 0, 40);
curloc=9;
makeMove(canvas,curloc, left,right,up,down);
}
public void fight(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your at the Final Fight!!!", 0, 10);
curloc=11;
makeMove(canvas,curloc, left,right,up,down);
}
public void free(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("You Won!!!!", 0, 10);
curloc=11;
makeMove(canvas,curloc, left,right,up,down);
}
public void solitaryConfinement(Graphics canvas)
{
space=enter=left=right=up=down=false;
clearScreen(canvas);
canvas.setColor(Color.WHITE);
canvas.drawString("Your in Solitary Confinement.", 0, 10);
canvas.drawString("Down: Guard Post",0,20);
curloc=5;
makeMove(canvas,curloc, left,right,up,down);
}
public void makeMove(Graphics canvas,int curloc,boolean left, boolean right, boolean up, boolean down )
{
if (curloc==0)
{
if(left)
{
train(canvas);
left=right=up=down=false;
}
}
}
public void delayGame(int numSecs)
{
int delayTime = 1000 * numSecs;
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < delayTime)
{
endDelay = System.currentTimeMillis();
}
}
public void clearScreen(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,5000,5000);
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left = true;
repaint();
}
if (key == KeyEvent.VK_DOWN)
{
down = true;
repaint();
}
if (key == KeyEvent.VK_RIGHT)
{
right = true;
repaint();
}
if (key == KeyEvent.VK_UP)
{
up = true;
repaint();
}
if (key == KeyEvent.VK_SPACE)
{
space = true;
repaint();
}
if (key == KeyEvent.VK_ENTER)
{
enter = true;
repaint();
}
}
public void focusGained(FocusEvent evt) {focus=true; repaint();}
public void focusLost(FocusEvent evt) {focus=false;}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
public void restart(Graphics g){}
}
so the number for curloc isnt bing passed into makeMove and the program keys arn't working.
thanks
chris s
edit:
You are at train [message repeated 1075 time] You are at train Exception in thread "AWT-EventQueue-1" java.lang.StackOverflowError at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source) at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source) at java.nio.charset.CharsetEncoder.encode(Unknown Source) at sun.nio.cs.StreamEncoder.implWrite(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at java.io.OutputStreamWriter.write(Unknown Source) at java.io.BufferedWriter.flushBuffer(Unknown Source) at java.io.PrintStream.write(Unknown Source) at java.io.PrintStream.print(Unknown Source) at java.io.PrintStream.println(Unknown Source) at AlcatrazBreakout.train(AlcatrazBreakout.java:75) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81) at AlcatrazBreakout.makeMove(AlcatrazBreakout.java:196) at AlcatrazBreakout.train(AlcatrazBreakout.java:81)
this is my console output in Eclipse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现的两个问题:
在
paint()
中,您要么绘制初始化屏幕('欢迎来到...'),要么调用startbody()
,具体取决于在焦点
上。在startbody
中您要做的第一件事是space = Enter = left = right = up = down = false;
即“清除”所有“键”的值。这就是为什么您的程序键没有任何作用。您似乎在游戏中不同“位置”的所有方法中都执行此操作您还将
curloc
重置为 0。同样的问题。而且您实际上不需要将curloc
作为参数传递给不同的方法,因为它是一个实例变量,并且在代码中的任何位置都可见。The two problems that I spotted:
In
paint()
you either paint the init screen('Welcome to...') or you callstartbody()
depending onfocus
. The first thing you do instartbody
isspace = enter = left = right = up = down = false;
i.e. you 'clear' the values of all the 'keys'. This is why your program keys aren't having any effect. You seem to be doing this in all of your methods for different 'places' in the gameYou also reset
curloc
to 0. Same problem. And you needn't really passcurloc
as an argument to different methods since it's an instance variable and will be visible everywhere in your code.您不应该:
覆盖小程序的paint()方法。自定义绘制是通过重写 JPanel 的paintComponent() 方法来完成的,然后将面板添加到小程序中。
使用紧密循环来“延迟”游戏。相反,请使用 Swing Timer 来安排游戏的下一步。
You should NOT:
Override the paint() method of the applet. Custom painting is done by overriding the paintComponent() method of a JPanel and then you add the panel to the applet.
Use a tight loop to "delay" the game. Instead use a Swing Timer to schedule the next move of the game.
在 AWT/Swing(或任何 GUI)程序(包括小程序)中,您必须将程序逻辑(即在房间之间移动)与绘画分离。
这意味着,您的 keyPress 和类似的事件驱动方法应该(可能通过调用其他方法间接)更改程序的某些内部状态(即我现在在哪个房间),然后调用repaint(),然后就完成了。
您的
paint
方法应该查看状态,根据此状态绘制小程序,然后完成。您正在做的是在没有递归结束条件的情况下从自身递归调用某些方法,这会导致您观察到的 StackOverflowError (这也是该网站的同名)。
因此,这里是一个对用户输入做出反应的示例小程序:
当然,在游戏中您不想用某种颜色绘制矩形,但您会为当前所在的房间调用正确的绘制方法。您的内部状态将包括房间,按右键会更改为其他房间之一。
In a AWT/Swing (or about any GUI) program (which includes applets), you have to decouple the program logic (i.e. your moving between the rooms) from the painting.
This means, your
keyPress
and similar event-driven methods should (maybe indirectly by calling other methods) change some internal state of the program (i.e. in which room I'm now), then callrepaint()
, and be finished.Your
paint
method should look at the state, paint the applet according to this state, and then be finished.What you are doing is recursively invoking some method from itself without a recursion-end condition, and this causes the StackOverflowError (which also is the eponym for this web site) you observed.
So, here is an example applet reacting on user input:
Of course, in your game you would not want to paint a rectangle in some color, but you would call the right painting method for the room you are currently in. Your internal state would include the room, and it would be changed to one of the other rooms when the right key is pressed.