Java 我编译了我的整个程序,没有错误,但运行时没有显示任何内容
我觉得这很奇怪,因为我以前在Eclipse上运行它时,这个程序工作正常。然而,当我尝试使用“java PacMan”运行它时,什么都没有显示。我认为这是因为我缺少一个主要方法,但我不知道如何将它集成到我的程序中,而不把一切都搞砸Java 我编译了我的整个程序,没有错误,但运行时没有显示任何内容,java,eclipse,main,main-method,Java,Eclipse,Main,Main Method,我觉得这很奇怪,因为我以前在Eclipse上运行它时,这个程序工作正常。然而,当我尝试使用“java PacMan”运行它时,什么都没有显示。我认为这是因为我缺少一个主要方法,但我不知道如何将它集成到我的程序中,而不把一切都搞砸 /* PacMan.java This is a lame replica of the game PacMan. The PacMan starts out on a random location on the board with 6 gh
/*
PacMan.java
This is a lame replica of the game PacMan. The PacMan
starts out on a random location on the board with
6 ghosts & cheese. It must try to eat the cheese without
getting eaten by the ghosts. The ghosts only move around
when PacMan does. All characters on the board can only move
around when PacMan does and can only move up, down, left,
and right; diagonal movement isn't allowed. When PacMan eats
all the cheese, the words 'YOU WIN! :)' will be displayed across
the board; if you lose, 'YOU LOST! :(' will be shown.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PacMan extends JApplet implements KeyListener
{
private int chez, ghosts;
private int currentx, currenty, direction, a;
private int [][] grid;
private int [] alienx, alieny;
private DrawingArea canvas;
private boolean lost;
public void init()
{
setSize(500, 560);
generateBoard();
canvas = new DrawingArea(); // drawing area
getContentPane().add(canvas, BorderLayout.CENTER);
JPanel buttonPanel = new ButtonPanel();
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
canvas.setBackground(Color.black);
canvas.addKeyListener(this);
canvas.requestFocus();
}
public void generateBoard()
{
lost = false;
chez = ghosts = currentx = currenty = 0;
grid = new int[10][10]; // sets 2D array size
alienx = new int[6];
alieny = new int[6];
do
{
currentx = (int)(Math.random() * 10);
currenty = (int)(Math.random() * 10);
if(grid[currentx][currenty] == 0 && chez < 6)
{
grid[currentx][currenty] = 1; // placement holder because 0 won't be read in
chez++; // '1' is the cheese
}
if(grid[currentx][currenty] == 0 && ghosts < 6)
{
grid[currentx][currenty] = 2; // placement holder because 0 won't be read in
alienx[ghosts] = currentx;
alieny[ghosts] = currenty;
ghosts++; // '2' is the ghosts
}
}
while(chez < 6 || ghosts < 6);
do // this makes sure PacMan never respawns on a ghost or cheese
{
currentx = (int)(Math.random() * 10);
currenty = (int)(Math.random() * 10);
}
while(grid[currentx][currenty] != 0);
grid[currentx][currenty] = 3; // this is PacMan
direction = 0;
}
public void moveGhosts(int a)
{
int oldx = alienx[a], oldy = alieny[a]; // coordinates of the aliens
grid[oldx][oldy] = 0; // erases original spacing for monster/alien
int stuff; // used for random statement
boolean done = true;
stuff = (int)(Math.random() * 8 + 1); // amount of random moves XD
do
{
switch(stuff)
{
case 1: alienx[a]++; break; // down
case 2: alienx[a]--; break; // up
case 3: alieny[a]++; break; // right
case 4: alieny[a]--; break; // left
case 5: alienx[a]++; alieny[a]--; break; // down, left
case 6: alienx[a]++; alieny[a]++; break; // down, right
case 7: alienx[a]--; alieny[a]--; break; // up, left
case 8: alienx[a]--; alieny[a]++; break; // up, right
}
if(alieny[a] == 10)
alieny[a] = 0;
if(alieny[a] == -1)
alieny[a] = 9;
if(alienx[a] == 10)
alienx[a] = 0;
if(alienx[a] == -1)
alienx[a] = 9;
if(grid[alienx[a]][alieny[a]] == 0 || grid[currentx][currenty] == 3)
done = true; // only runs when ghost is on empty space or eats PacMan
else
done = false; // makes sure ghost never eats cheese
for(int b = 0; b < 6; b++)
if(alienx[b] == alienx[a] && alieny[b] == alieny[a] && b != a)
done = false; // prevents ghosts from overlaping each other
if (!done)
{
oldx = alienx[a];
oldy = alieny[a];
}
} while (!done);
grid[alienx[a]][alieny[a]] = 2;
}
class DrawingArea extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
displayBoard(g);
this.requestFocus();
}
public void displayBoard(Graphics g)
{
g.setColor(Color.gray);
g.fillRect(116, 464, 270, 50); // gray background for board
g.fillRect(30, 10, 445, 445); // gray background for 'cheese remaining'
g.setColor(Color.yellow);
g.drawRect(116, 464, 270, 50); // outlines 'cheese remaining' box
if(grid[currentx][currenty] == 1) // makes cheese disappear when PacMan eats it
{
grid[currentx][currenty] = 0;
chez--;
}
if(grid[currentx][currenty] == 2) // makes PacMan disappear when he hits a ghost
lost = true;
Font progress = new Font("Sans Serif", Font.PLAIN, 25);
g.setFont(progress);
if(!lost && chez > 0)
g.drawString("Cheese Remaining:"+ chez, 130, 500); // shows # of cheese left
else if(lost == true)
g.drawString("YOU LOST! :(", 175, 500);
else if(chez == 0)
g.drawString("YOU WON! :)", 175, 500);
g.setColor(Color.white);
for(int row = 0; row < grid.length; row++)
for(int col = 0; col < grid[row].length; col++)
{
g.setColor(Color.white);
g.fillRect(30 + col * 45, 10 + row * 45, 40, 40); // prints grid
g.setColor(Color.blue); // primitive PacMan
if(!lost)
g.fillArc(30 + currentx * 45, 10 + currenty * 45, 40, 40, direction + 30, 300);
if(grid[col][row] == 1) // prints out cheese randomly if array encounters invisible '1'
{
// while scanning
g.setColor(Color.yellow);
g.fillRect(33 + col * 45, 13 + row * 45, 33, 33); // blocks of cheese
}
if(grid[col][row] == 2) // prints out ghosts randomly if array encounters invisible '2'
{
// while scanning
g.setColor(Color.red);
g.fillOval(30 + col * 45, 10 + row * 45, 40, 40); // ghosts' body
g.setColor(Color.black);
g.fillOval(38 + col * 45, 20 + row * 45, 8, 8); // left eye
g.fillOval(53 + col * 45, 20 + row * 45, 8, 8); // right eye
g.drawLine(38 + col * 45, 37 + row * 45, 61 + col * 45, 37 + row * 45); // mouth
}
}
}
}
public void keyPressed(KeyEvent evt)
{
int value = evt.getKeyCode();
if(!lost && chez > 0)
switch(value)
{
case KeyEvent.VK_LEFT:
{
currentx--; // PacMan moves left
if(currentx == -1) // makes sure the PacMan can 'loop' around through 'portals'
currentx = 9;
direction = 180; //faces left
for(int a = 0; a < 6; a++)
moveGhosts(a);
} break;
case KeyEvent.VK_RIGHT:
{
currentx++; // PacMan moves right
if(currentx == 10) // makes sure the PacMan can 'loop' around through 'portals'
currentx = 0;
direction = 0; //faces right
for(int a = 0; a < 6; a++)
moveGhosts(a);
} break;
case KeyEvent.VK_UP:
{
currenty--; // PacMan moves up
if(currenty == -1) // makes sure the PacMan can 'loop' around through 'portals'
currenty = 9;
direction = 90; //faces up
for(int a = 0; a < 6; a++)
moveGhosts(a);
} break;
case KeyEvent.VK_DOWN:
{
currenty++; // PacMan moves down
if(currenty == 10) // makes sure the PacMan can 'loop' around through 'portals'
currenty = 0;
direction = 270; //faces down
for(int a = 0; a < 6; a++)
moveGhosts(a);
} break;
}
if(value == KeyEvent.VK_SPACE)
generateBoard(); // call on method to regenerate board
canvas.repaint(); // repaints the board
}
class ButtonPanel extends JPanel implements ActionListener
{
private JButton left, right, up, down, reset;
public ButtonPanel() // displays available buttons
{
left = new JButton("LEFT");
left.addActionListener(this);
this.add(left);
right = new JButton("RIGHT");
right.addActionListener(this);
this.add(right);
up = new JButton("UP");
up.addActionListener(this);
this.add(up);
down = new JButton("DOWN");
down.addActionListener(this);
this.add(down);
reset = new JButton("RESET");
reset.addActionListener(this);
this.add(reset);
}
public void actionPerformed(ActionEvent evt)
{
String command = evt.getActionCommand();
if(!lost && chez > 0)
{
if(command.equals("LEFT"))
{
currentx--; // PacMan moves left
if(currentx == -1) // makes sure the PacMan can 'loop' around through 'portals'
currentx = 9;
direction = 180; //faces left
for(int a = 0; a < 6; a++)
moveGhosts(a);
}
else if(command.equals("RIGHT"))
{
currentx++; // PacMan moves right
if(currentx == 10) // makes sure the PacMan can 'loop' around through 'portals'
currentx = 0;
direction = 0; //faces right
for(int a = 0; a < 6; a++)
moveGhosts(a);
}
else if(command.equals("UP"))
{
currenty--; // PacMan moves up
if(currenty == -1) // makes sure the PacMan can 'loop' around through 'portals'
currenty = 9;
direction = 90; //faces up
for(int a = 0; a < 6; a++)
moveGhosts(a);
}
else if(command.equals("DOWN"))
{
currenty++; // PacMan moves down
if(currenty == 10) // makes sure the PacMan can 'loop' around through 'portals'
currenty = 0;
direction = 270; //faces down
for(int a = 0; a < 6; a++)
moveGhosts(a);
}
}
if(command.equals("RESET"))
generateBoard(); // call on method to regenerate board
canvas.repaint(); // repaints the board
}
}
public void keyTyped(KeyEvent evt) {}
public void keyReleased(KeyEvent evt) {}
public void actionPerformed(ActionEvent evt) {}
}
/*
PacMan.java
这是吃豆人游戏的蹩脚复制品。吃豆人
从电路板上的随机位置开始
6鬼与奶酪。它必须尽量不吃奶酪
被鬼魂吃掉。鬼魂只会四处走动
当吃豆人吃的时候。板上的所有字符只能移动
当PacMan这样做时,只能向上、向下、向左移动,
和权利;不允许对角移动。吃豆人的时候
所有的奶酪,上面写着“你赢了!”将显示在整个屏幕上
董事会;如果你输了,‘你输了!’(”将显示。
*/
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类PacMan扩展JApplet实现KeyListener
{
二等兵,幽灵;
专用int currentx,currenty,方向,a;
私有int[][]网格;
private int[]alienx,alieny;
私人绘制区域画布;
私有布尔丢失;
公共void init()
{
设置大小(500560);
生成板();
canvas=新绘图区域();//绘图区域
getContentPane().add(画布,BorderLayout.CENTER);
JPanel buttonPanel=新的buttonPanel();
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
帆布背景(颜色:黑色);
addKeyListener(这个);
requestFocus();
}
公共无效生成板()
{
丢失=错误;
chez=ghosts=currentx=currenty=0;
grid=new int[10][10];//设置二维数组大小
alienx=新整数[6];
alieny=新整数[6];
做
{
currentx=(int)(Math.random()*10);
currenty=(int)(Math.random()*10);
如果(网格[currentx][currenty]==0&&chez<6)
{
网格[currentx][currenty]=1;//位置保持器,因为不会读入0
chez++;//“1”是奶酪
}
如果(栅格[currentx][currenty]==0&&Ghost<6)
{
网格[currentx][currenty]=2;//位置保持器,因为不会读入0
alienx[重影]=当前x;
alieny[重影]=当前;
重影+++;/“2”是重影
}
}
而(chez<6 | | ghosts<6);
这样做可以确保吃豆人永远不会在鬼魂或奶酪上重生
{
currentx=(int)(Math.random()*10);
currenty=(int)(Math.random()*10);
}
而(网格[currentx][currenty]!=0);
网格[currentx][currenty]=3;//这是PacMan
方向=0;
}
公共区域(int a)
{
int oldx=alienx[a],oldy=alieny[a];//外星人的坐标
网格[oldx][oldy]=0;//删除怪物/外星人的原始间距
int stuff;//用于随机语句
布尔完成=真;
stuff=(int)(Math.random()*8+1);//随机移动量XD
做
{
开关(东西)
{
案例1:alienx[a]+;break;//故障
案例2:alienx[a];break;//up
案例3:alieny[a]+;break;//右
案例4:alieny[a];break;//左
案例5:alienx[a]+;alieny[a]--;break;//故障,左侧
案例6:alienx[a]++;alieny[a]++;break;//分解,右
案例7:alienx[a];alieny[a];break;//分手,左边
案例8:alienx[a]--;alieny[a]+;break;//分手,对吗
}
if(alieny[a]==10)
alieny[a]=0;
if(alieny[a]=-1)
alieny[a]=9;
if(alienx[a]==10)
alienx[a]=0;
如果(alienx[a]=-1)
alienx[a]=9;
if(grid[alienx[a]][alieny[a]==0 | | grid[currentx][currenty]==3)
done=true;//仅当ghost在空空间或吃了PacMan时运行
其他的
done=false;//确保鬼魂从不吃奶酪
对于(int b=0;b<6;b++)
如果(alienx[b]==alienx[a]&alieny[b]==alieny[a]&b!=a)
done=false;//防止重影彼此重叠
如果(!完成)
{
oldx=alienx[a];
oldy=alieny[a];
}
}而(!完成);
网格[alienx[a]][alieny[a]]=2;
}
类DrawingArea扩展了JPanel
{
公共组件(图形g)
{
超级组件(g);
显示板(g);
this.requestFocus();
}
公共空白显示板(图形g)
{
g、 setColor(颜色为灰色);
g、 fillRect(116464270,50);//电路板的灰色背景
g、 fillRect(30,10445445);//灰色背景表示“奶酪剩余”
g、 setColor(颜色为黄色);
g、 drawRect(116464270,50);//勾勒出“奶酪剩余”框
如果(grid[currentx][currenty]==1)//使奶酪在吃的时候消失
{
网格[currentx][currenty]=0;
切斯--;
}
if(grid[currentx][currenty]==2)//使吃豆人在击中鬼魂时消失
迷失=真实;
字体进度=新字体(“无衬线”,Font.PLAIN,25);
g、 setFont(进度);
如果(!lost&&chez>0)
g、 抽绳(“剩下的奶酪:+chez,130500);//显示出剩下的奶酪
else if(lost==true)
g、 抽绳(“你输了!:(”,175500);
else if(chez==0)
g、 抽绳(“你赢了!:)”,175500);
g、 setColor(Color.white);
对于(int row=0;row<Html>
<Head>
<Title>Pac Man Applet</Title>
</Head>
<Body>
<Applet Code="MyApplet.class" width=200 Height=100>
</Applet>
</Body>
</Html>