1

I used to run my program as an applet, and it worked fine then, but I have decided to try to make it run in a JFrame. I recently had a problem with overriding, but I think I finally have that problem solved. This is a game I have been working on recently, its basically a recreation of Flappy Bird. I don't intend to sell my finished version, as I believe that the credit does not belong to me. Right now I am looking to make a pipe go across the screen and I plan on making a max of 3 sets of pipes on the screen at any given time.

FIXED Error with frame not opening New error: Undesired output

Here is my main class (Game)

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;

    public class Game {

        Pipes panel = new Pipes();

        public Game() {
            JFrame f = new JFrame();

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(panel);
            f.setTitle("Pipe Game");
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            f.setResizable(false);

            Timer timer = new Timer(10, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.move();
                    panel.repaint();
                }
            });
            timer.start();
        }

        public static void main(String args[]) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    new Game();
                }
            });
        }
}

And here is my class that draws (Pipes)

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Pipes extends JPanel {
    //Declare and initialiaze variables
    int height = setHeight();
    int x1 = 754;   //xVal start
    int x2 = 75;    //pipe width
    int y1 = -1;    //yVal start
    int y2;         //pipe height
    int vel;        //pipe velocity
    int gap = 130;  //gap height

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        y2 = height;
        g.clearRect(0,0,750,500);                       //Clear screen
        g.drawRect(x1,y1,x2,y2);                        //Draw part 1
        g.drawRect(x1-3,y2-1,x2+6,25);                  //Draw part 2
        g.drawRect(x1-3,y2+25+gap,x2+6,25);             //Draw part 3
        g.drawRect(x1,y2+25+gap+25,x2,500-y2-49-gap);   //Draw part 4
    }

    public void move() {
        x1--;
    }

    public int getY() {
        return y2+25;
    }

    public int getX() {
        return x1-3;
    }

    public int setHeight() {
        int num = (int)(9*Math.random() + 1);
        int val = 0;
        if (num == 9)
        {
            val = 295;
        }
        else if (num == 8)
        {
            val = 246;
        }
        else if (num == 7)
        {
            val = 216;
        }
        else if (num == 6)
        {
            val = 185;
        }
        else if (num == 5)
        {
            val = 156;
        }
        else if (num == 4)
        {
            val = 125;
        }
        else if (num == 3)
        {
            val = 96;
        }
        else if (num == 2)
        {
            val = 66;
        }
        else
        {
            val = 25;
        }
        return val;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(751, 501);
    }
}
4

1 回答 1

2

我想你想要

public void run() {
   new Game();
}

代替

public void run() {
   new Pipes();
}
于 2014-02-04T23:39:21.277 回答