0

I'm making a 2D runner game, I made my frames with

public class MainMenu2 {
    MainMenu2() throws IOException{
        JFrame Main_Menu = new JFrame("Main Menu");
        Main_Menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final int widthScreen = screenSize.width;
        final int heightScreen = screenSize.height;

every time I created a class I made the JFrames like that every single time, without re-calling the class or whatever. I completed the menu with buttons that connect to other classes but I need to make a level now. It consists on a player moving right,left and up, collisions with platforms and enemies. Kinda like super mario. I'm a beginner in Java so I haven't found any examples that follow my structure, since they all base off from 1 JFrame. I don't know how to make a tile and connect it to a class' JFrame. I'm lost I need help, thank you.

earandap
  • 1,446
  • 1
  • 13
  • 22
DennyS
  • 1
  • 1
  • 1
    All the examples you've seen have 1 `JFrame` for a reason - you *should* only have 1 `JFrame`. You're not going to have your player run through different application windows, correct? As a rule of thumb, have as many `JPanels` as you like - but there are only a few use cases I can think of where multiple `JFrame`s would make sense - this is not one of them. – sleepToken Jan 09 '20 at 12:36
  • 1
    Have you considered using _JavaFX_ which is supposed to be a successor to _Swing_? Perhaps have a look at [Introduction to JavaFX for Game Development](https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835) – Abra Jan 09 '20 at 13:51
  • `Main_Menu` should be either a `JDialog` or a `JPanel` in a `CardLayout`. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jan 09 '20 at 14:23
  • See examples of switching between `JPanel`s using `CardLayout` : [1](https://stackoverflow.com/a/46013230/3992939) and [2](https://stackoverflow.com/a/46870789/3992939) – c0der Jan 10 '20 at 15:40

1 Answers1

-1

Sound like you're confused in swing hierarchy.

You can use Jpanel in which You can set All tiles and then set Jpanel into Jframe.
In short from child to parent :: Tiles --> Jpanel --> Jframe. (Refer the one simple example : https://coderanch.com/t/633503/java/Tiled-Based-Map-Game-Java).