OVE*_*ONE 37 java processing awt
这个问题是关于框架,Java和处理.
这个问题听起来很复杂,但实际上并非如此.我会尝试将其保持在最低限度.我正在迷宫游戏中创建一个小球,以便了解物理和渲染.到目前为止,这是一次很好的体验,但我打了一个砖墙.
我决定的总体布局是在AWT框架中包含PApplet并使框架关闭.之所以这样,是因为我被告知你一次只能拥有一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
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, BorderLayout.CENTER);
app.init();
}
}
Run Code Online (Sandbox Code Playgroud)
主要的
public class Main
{
public static void main(String[] args)
{
ExampleFrame x = new ExampleFrame(new Menu());
}
}
Run Code Online (Sandbox Code Playgroud)
当mousePressed == ebtn时,需要发生的事情是Frame中的所有内容都将被删除,并且将加载一个Highscores Screen.高分与菜单几乎相同.没有必要发布代码,因为这里有足够的.
第二类是充当框架并保存PApplet的类
最重要的是,有没有人知道如何从PApplet调用Frame方法或以其他方式删除所有PApplet内容并加载另一个PApplet?
And*_*son 19
mousePressed == ebtn时需要发生的事情是Frame中的所有内容都将被删除,并且将加载一个Highscores Screen
演示.嵌套下面CardLayout添加一个ActionListener而不是一个MouseListener.它对鼠标和键盘输入都有反应.
在同一屏幕空间中包含多个GUI元素还有很多其他方法.关闭我的头顶,JTabbedPane,JSplitPane,JDesktopPane/ JInternalFrame,啪的高分JDialog或JOptionPane..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CardLayoutDemo {
public static void main(String[] args) {
Runnable r = new Runnable () {
public void run() {
final JRadioButton game = new JRadioButton("Game", true);
JRadioButton highScores = new JRadioButton("High Scores");
ButtonGroup bg = new ButtonGroup();
bg.add( game );
bg.add( highScores );
JPanel buttons = new JPanel(new
FlowLayout(FlowLayout.CENTER, 5, 5));
buttons.add( game );
buttons.add( highScores );
JPanel gui = new JPanel(new BorderLayout(5,5));
gui.add(buttons, BorderLayout.SOUTH);
final CardLayout cl = new CardLayout();
final JPanel cards = new JPanel(cl);
gui.add(cards);
cards.add(new JLabel("Level 1"), "game");
cards.add(new JLabel("High Scores"), "scores");
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent ae) {
if (game.isSelected()) {
cl.show(cards, "game");
} else {
cl.show(cards, "scores");
}
}
};
game.addActionListener(al);
highScores.addActionListener(al);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
Run Code Online (Sandbox Code Playgroud)
为了回答如何从PApplet调用Frame方法?,我已将您的代码段修改为最低限度.在此修改版本中,当用户单击鼠标按钮时,将System.out触发a.
现在有两种方法可以访问您的Frame对象.但在此之前,让我说出这两点:
new ExampleFrame(new Menu());并将其添加到您的JFrame喜欢中时,会创建一个add(app, BorderLayout.CENTER);复杂的窗口/面板层次结构.像这样:
javax.swing.JPanel
javax.swing.JLayeredPane
javax.swing.JRootPane
test.ExampleFrame
Run Code Online (Sandbox Code Playgroud)
PApplet提供用于设置和访问框架对象的公共字段.令人惊讶的是它被称为frame:).您可以在致电之前进行设置app.init();>>代码
**查看代码中的注释**
修改了ExampleFrame.java
import java.awt.BorderLayout;
import javax.swing.JFrame;
import processing.core.PApplet;
public class ExampleFrame extends JFrame
{
private static final long serialVersionUID = 4792534036194728580L;
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, BorderLayout.CENTER);
// Setting my frame object
app.frame = this;
app.init();
}
// Sample Method
public void sampleMethod(String msg)
{
System.out.println("I think '"+ msg +"' called me !!");
}
}
Run Code Online (Sandbox Code Playgroud)
修改了Menu.java
import java.awt.Container;
import processing.core.PApplet;
import processing.core.PImage;
public class Menu extends PApplet
{
private static final long serialVersionUID = -6557167654705489372L;
PImage background;
static String tab = "";
//simple constructor
public Menu()
{
}
public void setup()
{
size(600, 400);
smooth();
background = loadImage("C:/temp/background.jpg");
}
public void draw()
{
image(background, 0, 0);
}
public void mousePressed()
{
Container p = getParent();
tab = "";
// FIRST WAY OF ACCESSING PARENT FRAME
while(p != null)
{
//printParentTree(p);
if(p instanceof ExampleFrame)
{
ExampleFrame myframe = (ExampleFrame)p;
myframe.sampleMethod("First Way");
break;
}
p = p.getParent();
}
// SECOND WAY OF ACCESSING PARENT FRAME
if(frame != null && (frame instanceof ExampleFrame))
{
ExampleFrame myframe = (ExampleFrame)p;
myframe.sampleMethod("Second Way");
}
}
void printParentTree(Container p)
{
System.out.println(tab+p.getClass().getName());
tab +='\t';
}
}
Run Code Online (Sandbox Code Playgroud)
检查public void mousePressed()方法.
为了完整起见,我还包括Main.java.
public class Main {
public static void main(String[] args){
new ExampleFrame(new Menu());
}
}
Run Code Online (Sandbox Code Playgroud)
现在回答删除所有PApplet内容并加载另一个PApplet
好吧,我还没有测试过.但你可以添加一个JPanel你的JApplet所有绘图,即创建子控件等.当感觉像重绘然后调用JPanel.removeAll().根据javadoc:
从此容器中删除所有组件.此方法还通知布局管理器通过removeLayoutComponent方法从此容器的布局中删除组件.
这个调用之后repaint的JPanel.尝试一下,它可能会工作:).
| 归档时间: |
|
| 查看次数: |
19537 次 |
| 最近记录: |