【问题标题】:Calling one class from another when action performed执行操作时从另一个类调用一个类
【发布时间】:2015-06-08 04:08:39
【问题描述】:

我有一个 flappy Bird 示例的游戏/测试类,并且我有一个登录屏幕,单击此屏幕类中的 JButton“登录”后,我想在下面调用 flappyBird 类。

我尝试过这样的事情,但没有运气

if(event == jButtonLogin){
this.dispose();
FlappyBird bird = new FlappyBird();
bird.setVisible();

//因为它有 Jframe,虽然这个逻辑可以工作。

如果有人可以指出正确的方法,请

 public class FlappyBird implements ActionListener, MouseListener, KeyListener {

    //private static final long serialVersionUID = 1L;

    public static FlappyBird flappyBird; //creating a static flappybird so it can be acessed within main

    public final int WIDTH = 800, HEIGHT = 600;   //scren size of the game
    public int highScore = 0;

    public Renderer renderer;
    public Random rand;

    public Rectangle bird;

    public int ticks, yMotion, score; //bird movement
    public boolean gameOver, started;

    public ArrayList<Rectangle> columns;    //arrrayList of Recatangles names column


    public FlappyBird()
    {
        JFrame jframe = new JFrame();    //main frame of game

        Timer timer = new Timer(20,this);

        renderer = new Renderer();
        rand = new Random();

        jframe.setSize(WIDTH, HEIGHT);
        jframe.setLocationRelativeTo(null);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //  jframe.setResizable(false);
        jframe.setVisible(true);
        jframe.addMouseListener(this);
        jframe.addKeyListener(this);
        jframe.setTitle("FlappyTest");
        jframe.add(renderer);    //need to create what were rendering

        bird = new Rectangle(50 - 10,HEIGHT/2 - 10,20,20);
        columns = new ArrayList<Rectangle>();

        addColumn(true);
        addColumn(true);
        addColumn(true);
        addColumn(true);

        timer.start();
    }

    public void addColumn(boolean start){
        int space = 350;
        int width = 100;
        int height = 50 + rand.nextInt(300);   // height of column //minum = 50 // and talles being random at 300

        if (start)  //if first pipes
        {
            columns.add(new Rectangle(WIDTH + width + columns.size() * 200, HEIGHT - height - 100, width, height));  //top pipe
            columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 200, 0, width, HEIGHT - height - space));   // bot pipe 
        }
        else
        {
            columns.add(new Rectangle(columns.get(columns.size() - 1).x + 300, HEIGHT - height - 100, width, height)); //top pipe
            columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));  //bot pipe
        }
    }

    public void paintColumn(Graphics g, Rectangle column)    //passing theses two vriable on the method
    {
        g.setColor(Color.green); //darker()
        g.fillRect(column.x, column.y, column.width, column.height);
    }

    public void jump()
    {
        if(gameOver)
        {
            bird = new Rectangle(50 - 10,HEIGHT/2 - 10,20,20);
            columns.clear();
            yMotion = 0;
            score = 0;

            addColumn(true);
            addColumn(true);
            addColumn(true);
            addColumn(true);

            gameOver = false;
        }

        if(!started)
        {
            started = true;
        }
        else if(!gameOver)
        {
            if (yMotion > 0)
            {
                yMotion = 0;
            }
            yMotion -=15;
        }

    }

    public void actionPerformed(ActionEvent arg0)    //action being performed on the timer (20,THIS)
    {
        int speed = 10; //columns speed

        ticks++; //keeping count of each tick of the bird

        if(started)
        {

            for(int i = 0;i < columns.size(); i++)
            {
                Rectangle column = columns.get(i);

                column.x -= speed;
            }
            if(ticks % 2 == 0 && yMotion < 15)   
            {
                yMotion += 2;
            }

            for(int i = 0;i < columns.size(); i++)  // for the whole columns ArrayList
            {
                Rectangle column = columns.get(i); // grab column at whichever i postion

                if(column.x + column.y < 0)   // column x.y less than 0
                {
                    columns.remove(column);   //remove that current column
                    if(column.y == 0)   //if the column.y == 0 when it turns 0 and you have no more starting columns
                    {
                        addColumn(false);   // you add the other columns false // infinite loop
                    }
                }
            }

            bird.y += yMotion;

            for(Rectangle column : columns)
            { // FOR EACH RECTANGLE column IN COLUMNS
                if(column.y == 0 && bird.x  + bird.width / 2 > column.x + column.width / 2 - 10 && bird.x  + bird.width / 2 < column.x + column.width / 2 + 10)
                {
                    score++;
                }
                if(column.intersects(bird))
                { //colision detection with pipes
                    gameOver = true;
                    if(bird.x < column.x)
                    {
                        bird.x  = column.x - bird.width;
                    }
                    else
                    {
                        if(column.y != 0){   //if its not the top column
                            bird.y = column.y - bird.height;
                        }
                        else if(bird.y < column.height)
                        {
                            bird.y = column.height;
                        }
                    }
                }
            }
            if(bird.y > HEIGHT - 100)  // if the Y becomes greater then the ground or if its less than 0
            {
                gameOver = true;
            }

            if(bird.y + yMotion >= HEIGHT - 120)
                bird.y = HEIGHT - 100 - bird.height;
            }
        renderer.repaint();   //EVERY 20 WE CALL REPAINT USING THE RENDERER TO UPDATE IT+
    }

    public void repaint(Graphics g) //MAIN SCREEN REPAINT
    {
        //System.out.println("Teste");
        System.out.println(bird.y);

        g.setColor(Color.BLACK);     //bground of the screen
        g.fillRect(0,0,WIDTH,HEIGHT);

        g.setColor(Color.BLUE);     //blue line
        g.drawLine(0, HEIGHT-100, WIDTH, HEIGHT-100);

        g.setColor(Color.WHITE);    //White Screen
        g.fillRect(0,HEIGHT-98,WIDTH, HEIGHT-100);

        g.setColor(Color.WHITE);     //adding bird component
        g.fillRect(bird.x,bird.y,bird.width,bird.height);

        for (Rectangle column : columns)
        {
            paintColumn(g, column);
        }

        g.setColor(Color.RED);
        g.setFont(new Font("Arial",1,100));

        if(!started)
        {
            g.drawString("Click to Start!", 75, HEIGHT / 2 - 50);
        }

        if(gameOver)
        {
            g.drawString("GAME OVER!", 75, HEIGHT / 2 - 50);
            if(score > highScore){
                highScore = score;
            }
        }
        if(!gameOver && started)
        {
            g.drawString(String.valueOf(score),WIDTH/2-25, 100);
        }

        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial",1,20));
        g.drawString("High Score : " + highScore, 10, 50);
    }

    public static void main(String[] args) 
    {
        flappyBird = new FlappyBird(); // creating a new instance of flappybird
    }

    @Override
    public void mouseClicked(MouseEvent arg0) 
    {
        jump();
    }

    @Override
    public void mouseEntered(MouseEvent arg0)
    {
    }

    @Override
    public void mouseExited(MouseEvent arg0)
    {
    }

    @Override
    public void mousePressed(MouseEvent arg0)
    {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) 
    {
    }

    @Override
    public void keyPressed(KeyEvent e) 
    {
        if(e.getKeyCode() == KeyEvent.VK_SPACE)
        {
            jump();
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0)
    {

    }

    @Override
    public void keyTyped(KeyEvent arg0) 
    {

    }

}

【问题讨论】:

  • 考虑使用 CardLayout 在视图之间切换,并根据需要使用某种模型来共享数据
  • @MadProgrammer 我很抱歉,但你能更具体一点
  • 考虑使用CardLayout,这将允许您更改在单个框架内对用户可见的组件。您可以通过创建单个实例并将其传递给所有需要它的视图来使用包含要在它们之间共享的属性的模型(对象),例如m-v-c
  • 当你点击登录时,你想显示另一个 JFrame - 对吗?
  • @MadProgrammer 感谢您抽出宝贵时间,我会调查一下,再次感谢

标签: java swing class jframe jbutton


【解决方案1】:

考虑研究布局管理器以实现基于状态的游戏的行为。

例如:在您的 main 方法中,与其调用 new FlappyBird(),不如调用类似 new Login() 的方法。

在创建 Login 对象时要小心——您不一定要通过“打开”并创建第二个 JFrame 来重新发明轮子(可以通过隐藏 JFrame 来完成不受欢迎的创可贴解决方案,但事实并非如此你想要什么)。

相反,正如 MadProgrammer 所暗示的 - 布局管理器可以立即允许您的应用程序交换环境。一些重组是为了完成这个设计,但总的来说它更加模块化、可扩展(如果你想包括一个选项页面),并且更加面向 OOP。

最终,从 Login 对象中,用户将能够单击一个按钮,将 Layout 切换到 Play Layout(这是您所有 FlappyBird 东西所在的位置)。

【讨论】:

  • 谢谢,这确实有帮助。如果我能得到一些关于如何处理代码的例子并指出我正确的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 2014-02-07
  • 2018-06-09
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
相关资源
最近更新 更多