-3

I made a simple Java game that consisted of 2 classes (that uses a JFrame). I tried to export it to a Runnable jar but for some reason it never works when I try to run it. Thanks!

Here is my code:

Main Class

    RussianRoulette game = new RussianRoulette();
    game.setVisible(true);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setSize(220, 200);
    game.setResizable(false);

}
}

Other Class

int lives = 5;
JTextField livesDisplay;
JButton gunButton;
ImageIcon gunCold;
ImageIcon gunActive;
JOptionPane endPane = new JOptionPane();
int endProgram;
int clicked = 0;
JTextField display;


public RussianRoulette() {
    super("Russian Roulette");
    Font font = new Font("Western", 20, 20);

    gunCold = new ImageIcon(getClass().getResource("guncold.gif"));
    gunActive = new ImageIcon(getClass().getResource("gunactive.gif"));

    JPanel buttonPanel = new JPanel();
    JPanel livesPanel = new JPanel();
    JPanel displayPanel = new JPanel();

    display = new JTextField("  Click Gun To Play!", 15);

    livesDisplay = new JTextField("Lives: " + lives, 8);
    livesDisplay.setEditable(false);
    livesDisplay.setFont(font);
    livesDisplay.setBackground(Color.BLACK);
    livesDisplay.setForeground(Color.WHITE);

    livesPanel.add(livesDisplay);
    livesPanel.setBackground(Color.BLACK);

    displayPanel.add(display);
    displayPanel.setBackground(Color.BLACK);
    display.setFont(font);
    display.setEditable(false);
    display.setBackground(Color.BLACK);
    display.setForeground(Color.WHITE);

    LayoutManager overlay = new OverlayLayout(buttonPanel);
    buttonPanel.setLayout(overlay);
    buttonPanel.setBackground(Color.BLACK);
    gunButton = new JButton(gunCold);
    gunButton.setToolTipText("Click To Pull Trigger");
    gunButton.setAlignmentX(0.4f);
    gunButton.setAlignmentY(0.0f);
    buttonPanel.add(gunButton);

    add(buttonPanel, BorderLayout.CENTER);
    add(livesPanel, BorderLayout.NORTH);
    add(displayPanel, BorderLayout.SOUTH);

    gunButton.addActionListener(new ActionListener() {
        @SuppressWarnings("static-access")
        public void actionPerformed(ActionEvent event) {
            Random r = new Random();
            int rand = r.nextInt(6);
            clicked++;
            switch (rand) {
            case 0:
                display.setText(" Bang! You lost a life.");
                lives--;
                livesDisplay.setText("Lives: " + lives);
                break;
            case 1:
                display.setText("      You're alive!");
                break;
            case 2:
                display.setText("      You're alive!");
                break;
            case 3:
                display.setText("      You're alive!");
                break;
            case 4:
                display.setText("      You're alive!");
                break;
            case 5:
                display.setText("      You're alive!");
                break;
            }

            Timer imageSwitch = new Timer(984, new TimerListener());
            imageSwitch.setRepeats(false);
            if (rand == 0) {
                imageSwitch.start();
                gunButton.setIcon(gunActive);
            } else {
                gunButton.setIcon(gunCold);
            }

            if (lives == 0) {
                JFrame end = new JFrame();
                endPane = new JOptionPane();
                end.add(endPane);
                endProgram = endPane.showOptionDialog(null, "You survived "
                        + clicked + " trigger pulls!", "Game Over",
                        endPane.OK_CANCEL_OPTION,
                        endPane.INFORMATION_MESSAGE, null, null, null);

                if (endProgram == endPane.OK_OPTION) {
                    System.exit(0);
                } else if (endProgram == endPane.CANCEL_OPTION) {
                    System.exit(0);
                } else if (endProgram == endPane.CLOSED_OPTION) {
                    System.exit(0);
                }
            }

        }

    });
}

private class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        gunButton.setIcon(gunCold);

    }

    {
        if (lives == 0) {
            display.setText("  Bang! You're dead.");
        }
    }

}

}

  • did you set main class ??what happen when you double click it?nothing? – Madhawa Priyashantha Jul 02 '15 at 19:51
  • @FastSnail I exported both the main class and the sub class, when I double click it, after a few seconds I get an error that says "The Java jar file could not be launched". – Ethan Cotler Jul 02 '15 at 19:56
  • [Java Swing: unable to load image using getResource](http://stackoverflow.com/q/22733881) – Tom Jul 02 '15 at 21:27
  • @Tom I just tried that and it didn't work. It did this weird thing where every once in a while the gun button became white and shrank to like a third of it's size – Ethan Cotler Jul 02 '15 at 23:00
  • Can you run your applicaton from the console, please? Just open a console, switch to the location of the jar file using `cd path/to/file` and then run `java -jar YourJarFile.jar`. I guess you will get a `NullPointerException` if you run it. – Tom Jul 03 '15 at 16:40
  • @Tom I just did. You're right I got a NullPointerException. What should I do cause I tried to use getResourceAsStream, it works but it freezes the GIF – Ethan Cotler Jul 03 '15 at 23:19

1 Answers1

1

I figured it out. I had to set the path of the images to a package in my project, which contained my pictures.

gunCold = new ImageIcon(getClass().getClassLoader().getResource("images/guncold.gif"));
gunActive = new ImageIcon(getClass().getClassLoader().getResource("images/gunactive.gif"));
  • @Tom Yeah I know. After that I did one final thing and I created a package called "images" in my project and put the images in there. I then set the path of both the image icons to the pictures in the package. – Ethan Cotler Jul 04 '15 at 16:05
  • @Tom I did getClass().getClassLoader().getResource("images/guncold.gif"); Then I did the same thing for the other picture – Ethan Cotler Jul 04 '15 at 16:11
  • @Tom Just did. Thanks for all you're help though! I'm going to accept my answer in 2 hours, because it says I have to wait. – Ethan Cotler Jul 04 '15 at 17:16
  • Yes, you have to avoid some time before you can accept your own answer (as far as I know this time depends on the posting date of the question). Btw I removed my comments under your answer to clean up a bit :D. – Tom Jul 04 '15 at 17:24