0

I have just exported a Java game I made to a Runnable JAR.

The game has an opening screen (a class named OpeningScreen extending JPanel). When you press ENTER, it's supposed to go from the opening screen to the game itself (create a new Board instance and add it to the JPanel.) It works fine inside Eclipse.

An instance of OpeningScreen is created in the class with main(), named Starter.

When exporting, I set the Starter class as the "Launch configuration", and set "Library handling" to "Extract required libararies into generated JAR".

After exporting, I get a window saying that it exported with compile errors, but doesn't tell me which errors exactly.

When starting the program from the generated JAR, the opening screen does show, but pressing ENTER won't start the game, although it does work inside Eclipse.

Here's the code for OpeningScreen:

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class OpeningScreen extends JPanel implements KeyListener{

    private static final long serialVersionUID = 1L;
    public OpeningScreen(){
        setFocusable(true);
        setVisible(true);
        addKeyListener(this);
    }

    public void paint(Graphics g){

        super.paint(g);
        setBackground(Color.BLACK);

        Graphics2D g2d = (Graphics2D) g;

        // A lot of drawing Strings.

    }


    public void startGame(){

        JFrame frame = new JFrame("Pong Battle");
        frame.setSize(500,500);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Board board = new Board();
        frame.add(board);

        frame.setVisible(true);
    }

    @Override
    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        if(key==KeyEvent.VK_ENTER)startGame();

    }

    public void keyReleased(KeyEvent arg0) {
    }
    public void keyTyped(KeyEvent arg0) {
    }

}

EDIT: Starter class:

import javax.swing.*;
import java.awt.*;

public class Starter extends JFrame {

    public Starter(){

        setSize(500,500);
        setResizable(false);
        setTitle("Pong Battle");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        OpeningScreen openingS = new OpeningScreen();
        add(openingS);

        setVisible(true);

    }

    public static void main(String[]args){
        Starter starter = new Starter();
    }

}

What could be the problem? Thanks

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
Josh
  • 95
  • 1
  • 9
  • 1) For Swing, typically use key bindings over the AWT based, lower level, `KeyListener`. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for details on how to use them. 2) For Swing, it is `paintComponent(Graphics)` rather that `paint(..)` 3) The problem is that the panel is neither focusable nor has focus. 4) For better help sooner, post an [SSCCE](http://sscce.org/). 5) These same problems come up once or twice a day. Do you actually *search* the group before asking? – Andrew Thompson Dec 31 '13 at 13:29
  • What do you mean the panel is neither focusable nor has focus? I called setFocusable(true). – Josh Dec 31 '13 at 13:39
  • Oh, my bad. Again, a little louder this time ***For better help sooner, post an [SSCCE](http://sscce.org/).*** – Andrew Thompson Dec 31 '13 at 13:40
  • Sure, but since your already here could you help me with this? – Josh Dec 31 '13 at 13:42
  • Could you post something that actually bloody here? That collection of rubbish does not, **due to a missing class.** – Andrew Thompson Dec 31 '13 at 13:43
  • BTW - *"I get a window saying that it exported with compile errors"* That is something you should fix at compile time. When your 'magic' IDE compiles it, it makes changes to the code so that it can spit out a Jar that will most likely fail! Totally pointless.. – Andrew Thompson Dec 31 '13 at 13:46
  • You mean I need to add Starter? No problem – Josh Dec 31 '13 at 13:46
  • I'd fix the compile errors, but I have no idea what they are. – Josh Dec 31 '13 at 13:47
  • No, I did not. To find the missing class, why don't you copy/paste what appears here and try to compile it as a new project? The compiler will tell you *exactly* what is missing. – Andrew Thompson Dec 31 '13 at 13:48
  • *"but I have no idea what they are."* You are 'programming by magic'. Change that, or you'll fail completely. – Andrew Thompson Dec 31 '13 at 13:49
  • Sorry but I don't understand what you're saying. I think you're saying that I'm not aware enough of potential compile errors while I code. How could I change this? I have no idea what could cause these compile error and how to avoide them. – Josh Dec 31 '13 at 13:51
  • *"How could I change this?"* Your IDE will show lists of compile errors when you go to compile the code! What IDE do you use? – Andrew Thompson Dec 31 '13 at 13:53
  • I use Eclipse. Where is the 'compile code' in Eclipse? – Josh Dec 31 '13 at 13:56
  • I get no compile errors with code posted, minus the Board class at least. I just deleted that code and works fine. – Paul Samsotha Dec 31 '13 at 13:58
  • If you mean the standard console, I got nothing there after exporting. – Josh Dec 31 '13 at 13:58
  • *"I get no compile errors with code posted, minus the Board class at least"* And, do you *really think* I have a `Board` class conveniently lying around, just to help you with your code? If you are seeing the problems with the lack of the `Board` class, then **that is compile errors.** BTW - I don't know how to compile in Eclipse, I don't use it often. But I know it has ways to compile code, and can show the errors. – Andrew Thompson Dec 31 '13 at 14:01
  • @AndrewThompson, that was _my_ comment haha – Paul Samsotha Dec 31 '13 at 14:06
  • @peeskillet Oh boy. Is my face red! I should pay closer attention. OP - My bad. – Andrew Thompson Dec 31 '13 at 14:08
  • `but since your already here could you help me with this?` you have already been told that custom painting should be done in the paintComponent() method and you even accepted that answer: http://stackoverflow.com/questions/20830382/jpanel-wont-display. Why should we continue to help when you just ignore our advice??? – camickr Dec 31 '13 at 16:30

2 Answers2

0

Your opening screen needs to be added inside another container and subsequently another Jframe

  1. Create another JFrame.
  2. Make it undecorated. f.setUndecorated(true)
  3. Make opening screen panel as content panel f.setContentPane(new OpeningPanel())
  4. Display this frame.
  5. Remove the opening screen frame once Enter key is pressed. f.dispose()
  6. and then call startGame()
vahid abdi
  • 9,636
  • 4
  • 29
  • 35
Sorter
  • 9,704
  • 6
  • 64
  • 74
0

Don't use a KeyListener. You should be using Key Bindings.

See Motion Using the Keyboard for the reasons why and a working example.

camickr
  • 321,443
  • 19
  • 166
  • 288