2 Frames nacheinander aufrufen

Jackii

Mitglied
Hallo.

Wichtige Infos mal gleich zu anfang 😉
Ich Programmiere Java mit Netbeans 7.1

Ich bin grade dabei ein Startbildschirm für ein 2D Spiel zu programmieren.
Zuerst öffnet sich das Startbild und durch ein Button Klick soll sich das eigendliche Spiel öffenen.

Das Spiel wird im einem Fullscreen Frame ausgegeben. Das Startbild ist kein Fullscreen.

Zu zeit bekomme ich folgende Fehlermeldung:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

Mir wurde gesagt das es warscheinlich etwas mit den zwei Frames zutun hat deshalb meine frage ob meine Idee überhaubt möglich sei??

MfG
Jackii
 
klar ist das möglich und die Fehlermeldung sagt, dass irgendwas NULL ist.
Entweder einer deiner beiden Frames, oder irgendwelche Komponenten die sich darin befinden.
Ohne Code nichts los...also zeig den Quellcode, oder -wenn du viel Glück hast- hat einer heute seine Kristallkugel dabei und kann dir sagen, wo du an welcher Stelle etwas ändern musst.
 
Ich hatte auch schon mal was geplant und etwas hat nicht funktioniert.
Ging das was ich geplant hatte überhaupt?

:noe:

Was erwartest du mit so einer Frage? Naja ich kann nicht helfen, meine Kistallkugel hat Urlaub.

PS: In der NullPointer Exception (Der Fehler kommt ja wahrscheinlich in deiner IDE, welche wir auch nicht kennen) steht in welcher Zeiler welcher Datei der Fehler auftritt. Guckmal genau nach. Da ist irgendwas nicht initialisiert worden!
 
Da wird irgendeine Variable/Objekt null sein also sowas wie
Siehe hier

Und ja, es ist möglich.

Ich vermute mal nach der Aussage:
Zu zeit bekomme ich folgende Fehlermeldung:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

Mir wurde gesagt das es warscheinlich etwas mit den zwei Frames zutun hat deshalb meine frage ob meine Idee überhaubt möglich sei??

Ist dein Java wissensstand noch am Anfang und deshalb würde ich dir auch mal empfehlen die Grundlagen dir anzueignen.
Galileo Computing :: Java ist auch eine Insel - index

Um dir bei der Fehlersuche zu helfen, bräuchte man schon ein wenig Code und den gesamten Stacktrace.
 
Sorry das ich vergessen habe den Code anzufügen -.-

Dies ist der Code für StartBild.java:

Java:
package Menu;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;

public class StartBild {
    
    public static void main(String[] args) {
            new StartBild().los();
    }
    
    public void los() {
        ImagePanel panel = new ImagePanel(new ImageIcon("images/hinter.jpg").getImage());
        final JFrame frame = new JFrame();
        JButton startBut = new JButton("Starte Spiel");
        JButton highBut = new JButton("Highscoreliste");
        JButton endeBut = new JButton("Spiel beenden");
        
        panel.setLayout(new java.awt.GridBagLayout());
    
        panel.add(startBut, new java.awt.GridBagConstraints());
        panel.add(highBut, new java.awt.GridBagConstraints());
        panel.add(endeBut, new java.awt.GridBagConstraints());
    
        frame.getContentPane().add(panel);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    
        
        //alle ActionListener
        
        //ActionListener fuer den Start Button
        startBut.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                
                SwingUtilities.invokeLater(new Runnable() { 
                    @Override
                    public void run() {                       
                        frame.setVisible(false);
                        //frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                        frame.dispose();
                        System.out.println("Start Javio");
                        KeyConfig.start();
                    }
                });
            }
        });
        
        //ActionListener fuer den Highscore Button
        highBut.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
//                try {
//                        Main hscore = new Main(); //Highscore starten
//                    } catch (Exception ex) {
//                        Logger.getLogger(StartBild.class.getName()).log(Level.SEVERE, null, ex);
//                    }
                    System.out.println("Start Highscore");
            }
        });
    
        //ActionListener fuer den Ende Button
        endeBut.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.exit( 0 ); //Programm schliessen
            }
        });
    }
}

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }
  
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }
}


Hier ist der Code von der KeyConfig.java:

Java:
package Menu;

import input.GameAction;
import input.InputManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.Border;

/**
    The KeyConfigTest class extends the MenuTest demo to add
    a dialog to configure the keyboard keys.
*/
public class KeyConfig extends Menu {

    public static void main(String[] args) {
        new StartBild().los();
        //new KeyConfig().run();
    }

    private static final String INSTRUCTIONS =
        "<html><font color=\"white\">Konfiguration</font></html>";

    private JPanel dialog,   // Main Panel für die Tasten-Konfiguration
                   selection;
   
    private JButton okButton,
                    selectButton,
                    selectButton2;
 
    private List inputs;

    @Override
    public void init() {
        super.init();

        inputs = new ArrayList();

        // create the list of GameActions and mapped keys
        JPanel configPanel = new JPanel(new GridLayout(5,2,2,2));
        configPanel.setBackground(Color.white);
        addActionConfig(configPanel, moveLeft);
        addActionConfig(configPanel, moveRight);
        addActionConfig(configPanel, jump);
        addActionConfig(configPanel, pause);
        addActionConfig(configPanel, exit);

        // create the panel containing the OK button
        JPanel bottomPanel = new JPanel(new FlowLayout());
        bottomPanel.setBackground(Color.black);
        okButton = new JButton("Uebernehmen");
        okButton.setFocusable(false);
        okButton.addActionListener(this);
        bottomPanel.add(okButton);

        // create the panel containing the instructions.
        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(new JLabel(INSTRUCTIONS));
        topPanel.setBackground(Color.black);

        // create the dialog border
        Border border = BorderFactory.createLineBorder(Color.black);
        Border border2 = BorderFactory.createLineBorder(Color.black);

        selection = new JPanel(new BorderLayout());
        selection.setBackground(Color.black);
        selection.setBorder(border2);
        selection.setVisible(false);
        selection.setSize(selection.getPreferredSize());
        // center the dialog
        selection.setLocation(
            (screen.getWidth() - selection.getWidth()) / 2,
            (screen.getHeight() - selection.getHeight()) / 2);

        // add the dialog to the "modal dialog" layer of the
        // screen's layered pane.
        screen.getFullScreenWindow().getLayeredPane().add(selection,JLayeredPane.MODAL_LAYER);
        // create the config dialog.
        dialog = new JPanel(new BorderLayout());
      
        dialog.add(topPanel, BorderLayout.NORTH);
        dialog.add(configPanel, BorderLayout.CENTER);
        dialog.add(bottomPanel, BorderLayout.SOUTH);
        dialog.setBorder(border);
        dialog.setVisible(false);
        dialog.setSize(dialog.getPreferredSize());

        // center the dialog
        dialog.setLocation(
            (screen.getWidth() - dialog.getWidth()) / 2,
            (screen.getHeight() - dialog.getHeight()) / 2);

        // add the dialog to the "modal dialog" layer of the
        // screen's layered pane.
        screen.getFullScreenWindow().getLayeredPane().add(dialog,JLayeredPane.MODAL_LAYER);
        System.out.println("Alles Erstellt");
    }

    /**
        Adds a label containing the name of the GameAction and an
        InputComponent used for changing the mapped keys.
    */
    private void addActionConfig(JPanel configPanel, GameAction action)
    {
        JLabel label = new JLabel(action.getName(), JLabel.RIGHT);
        InputComponent input = new InputComponent(action);
        configPanel.add(label);
        configPanel.add(input);
        inputs.add(input);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        super.actionPerformed(e);
        if (e.getSource() == okButton) {
            // hides the config dialog
            configAction.tap();
        }
    }

    @Override
    public void checkSystemInput() {
        super.checkSystemInput();
             if (configAction.isPressed()) {
            // hide or show the config dialog
            boolean show = !dialog.isVisible();
            dialog.setVisible(show);
            setPaused(show);
        }
    }

    /**
        Resets the text displayed in each InputComponent, which
        is the names of the mapped keys.
    */
    private void resetInputs() {
        for (int i=0; i<inputs.size(); i++) {
            ((InputComponent)inputs.get(i)).setText();
        }
    }

    /**
        The InputComponent class displays the keys mapped to a
        particular action and allows the user to change the mapped
        keys. The user selects an InputComponent by clicking it,
        then can press any key or mouse button (including the
        mouse wheel) to change the mapped value.
    */
    class InputComponent extends JTextField  {

        private GameAction action;

        /**
            Creates a new InputComponent for the specified
            GameAction.
        */
        public InputComponent(GameAction action) {
            this.action = action;
            setText();
            enableEvents(KeyEvent.KEY_EVENT_MASK |MouseEvent.MOUSE_EVENT_MASK |
                         MouseEvent.MOUSE_MOTION_EVENT_MASK |MouseEvent.MOUSE_WHEEL_EVENT_MASK);
        }

        /**
            Sets the displayed text of this InputComponent to the
            names of the mapped keys.
        */
        private void setText() {
            String text = "";
            List list = inputManager.getMaps(action);
            if (list.size() > 0) {
                for (int i=0; i<list.size(); i++) {
                    text+=(String)list.get(i) + ", ";
                }
                // remove the last comma
                text = text.substring(0, text.length() - 2);
            }

            // make sure we don't get deadlock
            synchronized (getTreeLock()) {
                setText(text);
            }
        }


        /**
            Maps the GameAction for this InputComponent to the
            specified key or mouse action.
        */
        private void mapGameAction(int code, boolean isMouseMap) {
            if (inputManager.getMaps(action).size() >= 1) {
                inputManager.clearMap(action);
            }
            if (isMouseMap) {
                inputManager.mapToMouse(action, code);
            }
            else {
                inputManager.mapToKey(action, code);
            }
            resetInputs();
            screen.getFullScreenWindow().requestFocus();
        }

        // alternative way to intercept key events
        @Override
        protected void processKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                // if backspace is pressed, clear the map
                if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE &&
                    inputManager.getMaps(action).size() > 0)
                {
                    inputManager.clearMap(action);
                    setText("");
                    screen.getFullScreenWindow().requestFocus();
                }
                else {
                    mapGameAction(e.getKeyCode(), false);
                }
            }
            e.consume();
        }


        // alternative way to intercept mouse events
        @Override
        protected void processMouseEvent(MouseEvent e) {
            if (e.getID() == MouseEvent.MOUSE_PRESSED) {
                if (hasFocus()) {
                    int code = InputManager.getMouseButtonCode(e);
                    mapGameAction(code, true);
                }
                else {
                    requestFocus();
                }
            }
            e.consume();
        }

        // alternative way to intercept mouse events
        @Override
        protected void processMouseMotionEvent(MouseEvent e) {
            e.consume();
        }

        // alternative way to intercept mouse events
        @Override
        protected void processMouseWheelEvent(MouseWheelEvent e) {
            if (hasFocus()) {
                int code = InputManager.MOUSE_WHEEL_DOWN;
                if (e.getWheelRotation() < 0) {
                    code = InputManager.MOUSE_WHEEL_UP;
                }
                mapGameAction(code, true);
            }
            e.consume();
        }
    }
    
    public static void start() {
        new KeyConfig().run();
    }
}

Wenn ich die KeyConfig.java Klasse ausführe funktioniert das Spiel...
 
Mach doch beides in einem Frame, am Anfang beinhaltet der Frame ein JPanel mit den Startbildschirminhalt, danach per Buttonklick wird das Startbildpanel ersetzt und der Frame auf fullscreen gesetzt.
 
dies hab ich auch schon versucht....leider klappte dies nicht so wie erhofft.....

Beim Starten der KeyConfig Klasse wird das Frame auch erstellt aber kurz bevor ich es sehe stürzt das Programm ab -.-

Das Spiel wird im einem Fullscreen Frame ausgegeben. Das Startbild ist kein Fullscreen.
Das Startbild ist jetzt auch Fullscreen!!!

Im Anhang habe ich ein Screenshot von dem Code teil wo das Programm rausfliegt.
Ich hoffe es hilft....
 

Neue Themen


Zurück
Oben