简体   繁体   中英

Trouble calling a JAVA file from a JAVA File

I'm fairly new to java and am having trouble calling a mini tennis game I coded, following a tutorial, into another java file. Essentially a button in the Main File will call my game and display it in another JFrame . I haven't been able to get my game frame to open and I know it has something to do with the layout of my code for the game but I have tried rearranging and I haven't been able to call it because the main function throws an interrupted exception because there is a thread in there that keeps the game looping. What would I put in my main program to call it and how can I modify my mini tennis game to be able to be called? Basically, how can I modify the code below to be able to be called using a Game2 game = new Game2(); in another program? Or is there another method?

@SuppressWarnings("serial")
public class Game2 extends JPanel {
    Ball ball = new Ball(this);
    Racquet racquet = new Racquet(this);
    int speed = 1;

    private int getScore() {
        return speed - 1;
    }

    public Game2() {
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                racquet.keyReleased(e);
            }

            @Override
            public void keyPressed(KeyEvent e) {
                racquet.keyPressed(e);
            }
        });
        setFocusable(true);
        Sound.BACK.loop();
    }

    private void move() {
        ball.move();
        racquet.move();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        ball.paint(g2d);
        racquet.paint(g2d);

        g2d.setColor(Color.GREEN);
        g2d.setFont(new Font("Verdana", Font.BOLD, 30));
        g2d.drawString(String.valueOf(getScore()), 10, 30);
    }

    public void gameOver() {
        Sound.BACK.stop();
        Sound.GAMEOVER.play();
        JOptionPane.showMessageDialog(this,
                                      "You caught it this many times " + getScore(),
                                      "Game Over",
                                      JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Catch the Snowball");
        Game2 game = new Game2();
        frame.add(game);
        frame.setSize(300, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        while (true) {
            game.move();
            game.repaint();
            Thread.sleep(10);
        }
    }
}

I am not sure if i understand you correctly, but if you want to access Game2 game = new Game2(); from another class you can create static object game.

    public class Game2 extends JPanel {
        public static Game2 game;
        ...

Then you need to initialize it.

public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame("Catch the Snowball");
    game = new Game2();

and then you can access it by

Game2.game.something();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM