从调用子类AWT框架方法 [英] Calling awt Frame methods from subclass

查看:314
本文介绍了从调用子类AWT框架方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是关于框架,Java和处理

This question is about Frames, Java and Processing.

这听起来问题错综复杂pretty但它真的不。我会尽量保持这一个简单的最低限度。我在迷宫游戏创建一个小球让我的头周围的物理和渲染。这是一个很好的经验,到目前为止,但我已经打了一下砖墙。结果
总体布局,我决定上是一个AWT框架内包含PApplets并具有帧关闭。这样做的原因是因为有人告诉我,你应该只有一个Papplet实例在同一时间。

This questions sounds pretty convoluted but its really not. I'll try keep this to a simple minimum. I'm creating a small ball in a maze game to get my head around physics and rendering. It's been a good experience so far but I've hit a bit of a brick wall.
The general layout I decided on was to contain PApplets within a AWT Frame and have the Frame close. The reason for this is because I was told that you should only have on instance of a Papplet at a time.

PApplet Applet的类处理,渲染库。

PApplet is the Applet class in Processing, a rendering library.

我有3个班在这里包括主

I have 3 classes here including the main

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类

the exampleFrame class

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();
    }
}

主要

public class Main
{
    public static void main(String[] args) 
    {
        ExampleFrame x = new ExampleFrame(new Menu());
    }
}

需要什么鼠标时pressed == ebtn在框架所有的东西将被删除,一榜屏幕将被装载的情况发生。高分是几乎相同的菜单。没有必要张贴code有足够的在这里。

What needs to happen when mousePressed == ebtn is all the stuff in the Frame will be removed and a Highscores Screen will be loaded. highscores is almost the same as menu. There is no need to post code as there is enough here.

第二类是充当框架和保持PApplet所述一个

The second class is the one which acts as a frame and holds the PApplet

底线,有没有人有任何想法如何调用从PApplet框架方法或另一种方式来消除所有PApplets内容并加载另一个PApplet吗?

Bottom line, has anyone have any idea how to call the Frame methods from the PApplet or another way to remove all PApplets contents and load another PApplet in?

推荐答案

为了回答如何调用从PApplet帧的方法呢?,我修改了code段裸露最低限度。在这个修改后的版本,当用户点击鼠标按键的System.out 被激发。

In order to answer How to call the Frame methods from the PApplet?, I have modified your code snippet to bare minimum. In this modified version when the user click mouse button a System.out is fired.

现在有在其中您可以访问您的框架对象有两种方法。但在此之前,让我说出这两点:

Now there are two ways in which you can access your Frame object. But before that let me state these two points:


  • 当你创建了新ExampleFrame一个PApplet(新菜单()); ,并在其添加到您的JFrame 像这样的添加(应用程序,BorderLayout.CENTER); 则窗口的复杂层次/创建面板。

  • When you create a PApplet like new ExampleFrame(new Menu()); and add it in your JFrame like this add(app, BorderLayout.CENTER); then a complex hierarchy of windows/panels are created.

这样的:

javax.swing.JPanel
    javax.swing.JLayeredPane
        javax.swing.JRootPane
            test.ExampleFrame


  • PApplet 提供公共场设置和访问你的框架对象。而令人惊奇的是名为 :)。您可以致电 app.init()之前设置;

    • PApplet provides a public field for setting and accessing your frame object. And amazingly it is called frame :). You can set it before calling app.init();
    • >> code

      **检出在code中的意见**

      ** Checkout the comments in the code**

      修改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 !!");
          }
      }
      

      修改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';
          }
      }
      

      检出pssed的公共无效鼠标$ P $()方法。

      有关完整性,我还包括Main.java我。

      For completeness, I am also including Main.java.

      public class Main {
          public static void main(String[] args){
              new ExampleFrame(new Menu());
          }
      }
      

      现在回答删除所有PApplets内容和加载另一个PApplet

      好吧,我没有测试它。但是你可以添加的JP​​anel 你的 JApplet的,做你的上即创建子控件的所有图纸等。当感觉像重绘然后调用 JPanel.removeAll()。其中按javadoc的:

      Well I have not tested it. But you can add a JPanel to your JApplet and do all your drawing on that i.e creating child controls etc. When feel like redrawing then call JPanel.removeAll(). Which as per javadoc:

      移除此所有组件
        容器。此方法还通知
        布局管理器以除去
        从这个容器的组件
        ,通过removeLayoutComponent布局
        方法。

      Removes all the components from this container. This method also notifies the layout manager to remove the components from this container's layout via the removeLayoutComponent method.

      在此调用后重绘的JP​​anel 。试试吧,它可能工作。)

      After this call repaint on the JPanel. Try it out, it might work :).

      这篇关于从调用子类AWT框架方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆