|
这个问题是关于框架,Java和处理的。
这个问题听起来很复杂,但实际上并非如此。我将尝试将其保持在最低限度。我正在迷宫游戏中创建一个小球,以使我了解物理和渲染。到目前为止,这是一个很好的经验,但是我碰到了一些砖墙。
我决定的总体布局是在AWT框架中包含PApplets,并关闭框架。这样做的原因是因为有人告诉我您一次只能使用一个Papplet实例。
PApplet
是Processing(渲染库)中的
Applet
类。
我这里有3节课,包括主要
public class Menu extends PApplet
{
//images and buttons
PImage background, playbtn1, playbtn2, hsbtn1, hsbtn2, abbtn1, abbtn2, exbtn1, exbtn2;
FBox pBtn, hBtn, eBtn;
FWorld menu;
//simple constructor
public Menu()
{
}
public void setup()
{
size(600, 400);
smooth();
Fisica.init(this);
menu = new FWorld();
//loading and placing images
background = loadImage(\"MenuAlt.jpg\");
System.out.println(background);
playbtn1 = loadImage(\"play1.gif\");
playbtn2 = loadImage(\"play2.gif\");
hsbtn1 = loadImage(\"high1.gif\");
hsbtn2 = loadImage(\"high2.gif\");
exbtn1 = loadImage(\"exit1.gif\");
exbtn2 = loadImage(\"exit2.gif\");
//loading and placing buttons
pBtn = new FBox(120, 150);
pBtn.setPosition(135, 215);
pBtn.setDrawable(false);
hBtn = new FBox(120, 150);
hBtn.setPosition(295, 215);
hBtn.setDrawable(false);
eBtn = new FBox(120, 150);
eBtn.setPosition(455, 215);
eBtn.setDrawable(false);
//add item to world
menu.add(pBtn);
menu.add(hBtn);
menu.add(eBtn);
}
public void draw()
{
image(background, 0, 0);
image(playbtn1, 80, 140);
image(hsbtn1, 237, 135);
image(exbtn1, 400, 140);
mouseOver();
menu.draw();
}
//close this frame an open a new level, high score or exit
//depending on what the use clicks
public void mousePressed()
{
FBody pressed = menu.getBody(mouseX, mouseY);
if (pressed == pBtn)
{
System.out.println(\"play game\");
this.getParent().getParent().getParent().getParent().setVisible(false);
ExampleFrame x = new ExampleFrame(new Level(\"level1.txt\"));
x.setLocation(this.getParent().getParent().getParent().getParent().getLocation());
}
if (pressed == hBtn)
{
System.out.println(\"high scores\");
this.getParent().getParent().getParent().getParent().setVisible(false);
/* these are just for finding the parent
System.out.println(this.getName());
System.out.println(this.getParent().getName());
System.out.println(this.getParent().getParent().getName());
System.out.println(this.getParent().getParent().getParent().getName());
System.out.println(this.getParent().getParent().getParent().getParent().getName());
*/
ExampleFrame x = new ExampleFrame(new HighScores()); //for testing, you can change this to new menu()
x.setLocation(this.getParent().getParent().getParent().getParent().getLocation());
}
if (pressed == eBtn)
{
System.out.println(\"exit\");
System.exit(0);
}
}
exampleFrame类
public class ExampleFrame extends JFrame
{
PApplet app;
public ExampleFrame(PApplet emApp)
{
super(\"Ball Maze Game\");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);
app = emApp;
setSize(615,438);
setVisible(true);
setLayout(new BorderLayout());
add(app, www.sxzhongrui.com);
app.init();
}
}
主要的
public class Main
{
public static void main(String[] args)
{
ExampleFrame x = new ExampleFrame(new Menu());
}
}
当mousePressed == ebtn发生时,框架中的所有内容都将被删除,并且将加载高分屏幕。高分与菜单几乎相同。无需发布代码,因为这里足够了。
第二类是充当框架并持有PApplet的类
最重要的是,有谁知道如何从PApplet调用Frame方法,或以另一种方式删除所有PApplets内容并加载另一个PApplet的方法?