这是一个简单的迷宫生成器,程序要求用户通过控制台输入迷宫的大小,并通过小程序显示迷宫。我想通过表单中的文本字段输入大小。我怎么能这样做?
import java.awt.*;
import java.util.*;
import java.applet.*;
public class hexagonal extends Applet implements Runnable
{
public static final int TOPL = 1;
public static final int TOPR = 2;
public static final int RIGHT = 4;
public static final int BOTTOMR = 8;
public static final int BOTTOML = 16;
public static final int LEFT = 32;
int cells[][];
Point current_cell;
Stack hold;
int total, count;
int gridw, gridh, cellw, cellh;
boolean finished = false;
Image offscreen;
Graphics offgr;
Thread t;
public void init()
{
int x, y;
Scanner uInput = new Scanner(System.in);
System.out.print("Enter the size of your hexagonal maze: ");
gridw = uInput.nextInt(); // 10
gridh = uInput.nextInt(); // 8
cellw = 30;
cellh = (int)(0.866*cellw); // approx. regular hexagon
cells = new int[gridw][gridh];
int full = TOPL | TOPR | RIGHT | BOTTOMR | BOTTOML | LEFT;
for (x = 0; x < gridw; x++)
for (y = 0; y < gridh; y++)
int top = (TOPL | TOPR) << 6;
int bottom = (BOTTOML | BOTTOMR) << 6;
for (x = 0; x < gridw; x++)
{
cells[x][0] |= top;
cells[x][gridh-1] |= bottom;
}
int left = (LEFT | BOTTOML) << 6;
int right = (RIGHT | TOPR) << 6;
for (y = 0; y < gridh; y++)
{
cells[0][y] |= left;
cells[gridw-1][y] |= right;
}
total = gridw*gridh;
offscreen = createImage(432, 237);
offgr = offscreen.getGraphics();
}
public void start()
{
t = new Thread(this);
t.start();
}
public void stop()
{
t.stop();
t = null;
}
public void run()
{
int dir;
current_cell = new Point(rnd(gridw)-1,rnd(gridh)-1);
count = 1;
hold = new Stack();
while (count < total)
{
dir = findNewNbr(current_cell);
if (dir == 0)
current_cell = (Point)(hold.pop());
else
{
removeWall(current_cell, dir);
count++;
hold.push(current_cell);
current_cell = getNbr(current_cell, dir);
}
if(total == count)
finished = true;
repaint();
try
{
Thread.sleep(0);
} catch (Exception e) {};
}
repaint();
}
public void update(Graphics g)
{
paint(offgr);
g.drawImage(offscreen,0,0,this);
}
public void paint(Graphics g)
{
int val, x , y;
int basex = 10;
int basey = 10;
g.setColor(Color.white);
g.fillRect(0, 0, gridw*cellw+(cellw+1)*gridh/2, (gridh+2)*cellh);
g.setColor(Color.black);
int x1=0, x2=0, x3=0, y1=0, y2=0, y3=0, y4=0;
for (x = 0; x < gridw; x++)
for (y = 0; y < gridh; y++)
{
val = cells[x][y];
x1 = basex + x*cellw + y*cellw/2;
x2 = x1 + cellw/2;
x3 = x1 + cellw;
y1 = basey + y*cellh;
y2 = y1 + cellh/3;
y3 = y1 + cellh;
y4 = y1 + 4*cellh/3;
if ((val & TOPL) != 0)
g.drawLine(x1, y2, x2, y1);
if ((val & TOPR) != 0)
g.drawLine(x2, y1, x3, y2);
if ((val & LEFT) != 0)
g.drawLine(x1, y2, x1, y3);
if ((val & RIGHT) != 0)
g.drawLine(x3, y2, x3, y3);
if ((val & BOTTOML) != 0)
g.drawLine(x1, y3, x2, y4);
if ((val & BOTTOMR) != 0)
g.drawLine(x2, y4, x3, y3);
}
g.setColor(Color.white);
if ((count < total) && (current_cell != null))
g.fillOval(current_cell.y*(x3-x2)+basex+current_cell.x*cellw, basey+current_cell.y*cellh+4,
cellw, cellh);
g.setColor(Color.black);
if(finished == true)
g.drawString("-", 430/2 -20, 230);
}
int findNewNbr(Point p)
{
int full = TOPL | TOPR | RIGHT | BOTTOMR | BOTTOML | LEFT;
int d = rnd(6)-1;
int k = 0;
while (k < 6)
{
switch(d)
{
case 0:
if ((cells[p.x][p.y] & (TOPL<<6)) != 0) break;
if ((cells[p.x][p.y-1] & 0x3F) == full) return TOPL;
break;
case 1:
if ((cells[p.x][p.y] & (TOPR<<6)) != 0) break;
if ((cells[p.x+1][p.y-1] & 0x3F) == full) return TOPR;
break;
case 2:
if ((cells[p.x][p.y] & (RIGHT<<6)) != 0) break;
if ((cells[p.x+1][p.y] & 0x3F) == full) return RIGHT;
break;
case 3:
if ((cells[p.x][p.y] & (BOTTOMR<<6)) != 0) break;
if ((cells[p.x][p.y+1] & 0x3F) == full) return BOTTOMR;
break;
case 4:
if ((cells[p.x][p.y] & (BOTTOML<<6)) != 0) break;
if ((cells[p.x-1][p.y+1] & 0x3F) == full) return BOTTOML;
break;
case 5:
if ((cells[p.x][p.y] & (LEFT<<6)) != 0) break;
if ((cells[p.x-1][p.y] & 0x3F) == full) return LEFT;
break;
}
d = (d+1) % 6;
k++;
}
return 0;
}
void removeWall(Point p, int d)
{
cells[p.x][p.y] ^= d;
switch(d)
{
case TOPL: cells[p.x][p.y-1] ^= BOTTOMR;
break;
case TOPR: cells[p.x+1][p.y-1] ^= BOTTOML;
break;
case RIGHT: cells[p.x+1][p.y] ^= LEFT;
break;
case BOTTOMR: cells[p.x][p.y+1] ^= TOPL;
break;
case BOTTOML: cells[p.x-1][p.y+1] ^= TOPR;
break;
case LEFT: cells[p.x-1][p.y] ^= RIGHT;
break;
}
}
Point getNbr(Point p, int d)
{
switch(d)
{
case TOPL: return new Point(p.x, p.y-1);
case TOPR: return new Point(p.x+1, p.y-1);
case RIGHT: return new Point(p.x+1, p.y);
case BOTTOMR: return new Point(p.x, p.y+1);
case BOTTOML: return new Point(p.x-1, p.y+1);
case LEFT: return new Point(p.x-1, p.y);
}
return null; // This shouldn't ever happen
}
int rnd(int n)
{
return (int)(Math.random()*n+1);
}
}
答案 0 :(得分:0)
Tutorial how to read file that is hosted in Applet
检查以下链接以阅读java applet中的文件
Can't get applet to read text file
要阅读本地主机上的文件,请点击以下链接
http://www.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem