JPanel和Pop UP,在哪里插入弹出的实例

时间:2015-02-24 04:21:35

标签: java swing popup jpanel

我正在制作一款游戏,一旦采取某项行动,我希望有一个带有选项的弹出屏幕。我不知道在我的代码中将popup jpanel的实例放在何处。我不希望它在主要JPanel的构造函数中是游戏板,我试图在main方法中添加它,但这似乎不起作用。建议?

public class GameBoard extends JPanel {

    public final int EARTH_ORBIT_RADIUS = 200;
    public final int BOARD_WIDTH = 800;
    public final int BOARD_HEIGHT = 600;
    public final int INITIAL_EARTH_X = 700;
    public final int INITIAL_EARTH_Y = 300;
    public final int INITIAL_DELAY = 100;
    public final int PERIOD_DELAY = 30;
    public final static int SUN_COORDINATE_X = 500;
    public final static int SUN_COORDINATE_Y = 300;
    public final int INITIAL_ASTEROID_X = 500;
    public final int INITIAL_ASTEROID_Y = 100;
    public final int INITIAL_SHIP_X = 500;
    public final int INITIAL_SHIP_Y = 500;

    public static int earth_x, earth_y;
    public static int asteroid_x, asteroid_y;
    public static int ship_x, ship_y;
    public int t = 0;
    public int ship_t = 0;
    public int keyheard = 0;
    public int[] earthcoordinates;
    public int[] asteroidcoordinates;
    public int[] shipcoordinates;

    public static boolean isCollision = false;

    Image earth;
    Image sun;
    Image asteroid;
    Image background;
    Image ship, shipL, shipR, shipU, shipD;

    Timer timer;

    GameBoard() {

        initGameBoard();

        loadImage();

        earth_x = INITIAL_EARTH_X;
        earth_y = INITIAL_EARTH_Y;

        asteroid_x = INITIAL_ASTEROID_X;
        asteroid_y = INITIAL_ASTEROID_Y;

        ship_x = INITIAL_SHIP_X;
        ship_y = INITIAL_SHIP_Y;

        timer = new Timer();
        timer.scheduleAtFixedRate(new ScheduledTask(), INITIAL_DELAY,
                PERIOD_DELAY);
    }

    void initGameBoard(){

        setBackground(Color.BLACK);
        setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
        setDoubleBuffered(true);
        addKeyListener(new KeyListen());
        setFocusable(true);

    }

    void loadImage() {
        ImageIcon earth_image_icon = new ImageIcon("earth2.png");
        earth = earth_image_icon.getImage();

        ImageIcon sun_image_icon = new ImageIcon("sun.png");
        sun = sun_image_icon.getImage(); 

        ImageIcon asteroid_image_icon = new ImageIcon("asteroid.png");
        asteroid = asteroid_image_icon.getImage();

        ImageIcon bg_image_icon = new ImageIcon("bg_pr.png");
        background = bg_image_icon.getImage();

        ImageIcon ship_image_icon = new ImageIcon("ship.png");
        ship = ship_image_icon.getImage();

        ImageIcon shipL_image_icon = new ImageIcon("shipL.png");
        shipL = shipL_image_icon.getImage();

        ImageIcon shipR_image_icon = new ImageIcon("shipR.png");
        shipR = shipR_image_icon.getImage();

        ImageIcon shipU_image_icon = new ImageIcon("shipU.png");
        shipU = shipU_image_icon.getImage();

        ImageIcon shipD_image_icon = new ImageIcon("shipD.png");
        shipD = shipD_image_icon.getImage();


    }

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

        drawBackground(g);
        drawEarth(g);
        drawSun(g);
        drawAsteroid(g);
        drawShip(g);

    }

    void drawEarth(Graphics g) {

        g.drawImage(earth, earth_x, earth_y, this);
        Toolkit.getDefaultToolkit().sync();
    }

    void drawSun(Graphics g) {
        g.drawImage(sun, SUN_COORDINATE_X, SUN_COORDINATE_Y, this);
        Toolkit.getDefaultToolkit().sync();
    }

    void drawAsteroid(Graphics g) {
        g.drawImage(asteroid, asteroid_x, asteroid_y, this);
        Toolkit.getDefaultToolkit().sync();
    }

    void drawBackground(Graphics g) {
        g.drawImage(background, 0, 0, null);
    }

    void drawShip(Graphics g){
        if(keyheard == 0 || isCollision == true){g.drawImage(ship,ship_x, ship_y, this);}
        else if(keyheard == 1){g.drawImage(shipL,ship_x, ship_y, this);}
        else if(keyheard == 2){g.drawImage(shipR,ship_x, ship_y, this);}
        else if(keyheard == 3){g.drawImage(shipU,ship_x, ship_y, this);}
        else if(keyheard == 4){g.drawImage(shipD,ship_x, ship_y, this);}

    }
    public class ScheduledTask extends TimerTask {

        @Override
        public void run() {

            OrbitCalculation oe = new OrbitCalculation();
            earthcoordinates = oe.earthOrbitCalculation(t);
            earth_x = earthcoordinates[0];
            earth_y = earthcoordinates[1];

            OrbitCalculation oa = new OrbitCalculation();
            asteroidcoordinates = oa.asteroidOrbitCalculation(t);
            asteroid_x = asteroidcoordinates[0];
            asteroid_y = asteroidcoordinates[1];

            OrbitCalculation os = new OrbitCalculation();
            shipcoordinates = os.shipOrbitCalculation(ship_t,keyheard, ship_x, ship_y);
            ship_x = shipcoordinates[0];
            ship_y = shipcoordinates[1];

            CollisionDetector c = new CollisionDetector();  

            if(c.whoCollision() == "asteroid"){
                ship_x = asteroid_x;
                ship_y = asteroid_y;

                isCollision = true;


            }
            else if(c.whoCollision() == "earth"){
                ship_x = earth_x;
                ship_y = earth_y;

                isCollision = true;
            }
            else{ isCollision = false;}

            //if(GameBo 

            t = t + 1;
            ship_t = ship_t + 1;


            repaint();
        }
    }

    public class KeyListen extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e){
            int key = e.getKeyCode();

            if(key == KeyEvent.VK_LEFT){
                keyheard = 1;
            }

            if(key == KeyEvent.VK_RIGHT){
                keyheard = 2;
            }
            if(key == KeyEvent.VK_UP){
                keyheard = 3;
            }
            if(key == KeyEvent.VK_DOWN){
                keyheard = 4;
            }
        }
        /**public int[] lastTwoKeys(KeyEvent e){
            last_two_keys[1] = last_two_keys[0];
            last_two_keys[0] = keyheard;

            return last_two_keys;

        }*/

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用JOptionPane创建Popup。见:http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
无需在构造函数中声明此组件。