Java: reset button for your game 🎮

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • #Java #reset #button
    Java reset button game tutorial explained
    //--------------------------------------------------------------------------------------------
    public class Main {
    public static void main(String[] args) {
    new GameFrame();
    }
    }
    //--------------------------------------------------------------------------------------------
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    public class GameFrame extends JFrame implements ActionListener{
    Game game;
    JButton resetButton;
    GameFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 500);
    this.setLayout(null);
    resetButton = new JButton();
    resetButton.setText("Reset");
    resetButton.setSize(100, 50);
    resetButton.setLocation(0, 200);
    resetButton.addActionListener(this);
    game = new Game();
    this.add(resetButton);
    this.add(game);
    this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==resetButton) {
    this.remove(game);
    game = new Game();
    this.add(game);
    SwingUtilities.updateComponentTreeUI(this);
    }
    }
    }
    //--------------------------------------------------------------------------------------------
    import java.awt.Color;
    import java.util.Random;
    import javax.swing.JPanel;
    public class Game extends JPanel{
    Random random;
    Game(){
    // code for game goes here :D
    random = new Random();
    int r = random.nextInt(256);
    int g = random.nextInt(256);
    int b = random.nextInt(256);
    this.setSize(500, 500);
    this.setLocation(100,0);
    this.setBackground(new Color(r,g,b));
    }
    }
    //--------------------------------------------------------------------------------------------
  • Věda a technologie

Komentáře • 49

  • @BroCodez
    @BroCodez  Před 3 lety +6

    //--------------------------------------------------------------------------------------------
    public class Main {
    public static void main(String[] args) {

    new GameFrame();

    }
    }
    //--------------------------------------------------------------------------------------------
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    public class GameFrame extends JFrame implements ActionListener{
    Game game;
    JButton resetButton;

    GameFrame(){

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 500);
    this.setLayout(null);

    resetButton = new JButton();
    resetButton.setText("Reset");
    resetButton.setSize(100, 50);
    resetButton.setLocation(0, 200);
    resetButton.addActionListener(this);

    game = new Game();

    this.add(resetButton);
    this.add(game);
    this.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==resetButton) {
    this.remove(game);
    game = new Game();
    this.add(game);
    Game.requestFocusInWindow(); #You may need to add this line. When you click the reset button, it may take focus away from the game.
    SwingUtilities.updateComponentTreeUI(this);
    }
    }
    }
    //--------------------------------------------------------------------------------------------
    import java.awt.Color;
    import java.util.Random;
    import javax.swing.JPanel;
    public class Game extends JPanel{

    Random random;

    Game(){
    // code for game goes here :D

    random = new Random();

    int r = random.nextInt(256);
    int g = random.nextInt(256);
    int b = random.nextInt(256);

    this.setSize(500, 500);
    this.setLocation(100,0);
    this.setBackground(new Color(r,g,b));

    }
    }
    //--------------------------------------------------------------------------------------------

    • @andromydous
      @andromydous Před 2 lety

      Game.requestFocusInWindow(); should be game.requestFocusInWindow(); Otherwise, you'll get an error line. Also, for those copy/pasting the code (specifically the previous mentioned line of code, the commented line should have "//" in front. Not the "#". It's a downside to working with different programming languages.

  • @lubodimoff9176
    @lubodimoff9176 Před 2 lety

    You are amazing!
    Keep these Java videos going!!!

  • @stephanieezat-panah7750
    @stephanieezat-panah7750 Před 2 lety +2

    fantastic. I have been wrestling with your TicTacToe game, trying to create a Reset button and an Exit button. The reset was giving me fits. now, I can go back. thank you much!

  • @deki4n
    @deki4n Před 3 lety

    Thanks bro! I discovered your channel some time ago and watched your JButton video to create something like a reset button, but this is way better !!

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 Před 3 lety +1

    very useful as always bro

  • @bubu2723
    @bubu2723 Před rokem +1

    Just created my first game with your help (it was like i watched your java tutorials and said in my mind "ok i will do something ez - rolling the dice with computer till someone get 30 points" but that took me 4 days after get back from my real job) i went through many tutorials of yours, and you helped me a lot, when i done this - i felt excited and satisfy! big thanks for this Bro!

  • @Garrison86
    @Garrison86 Před rokem

    This was extremely helpful! Thanks again

  • @alicias.h.4480
    @alicias.h.4480 Před 3 lety +1

    Very useful!
    Thank you Bro Code :) 👍

  • @rzayevmehemmedeli
    @rzayevmehemmedeli Před 3 lety +1

    mükəmməl

  • @noah77
    @noah77 Před 3 lety +2

    Your Java series is doing very well!
    Wholesome;

    • @BroCodez
      @BroCodez  Před 3 lety +3

      w00t!

    • @noah77
      @noah77 Před 3 lety +2

      @@BroCodez lol, can you make a tut on how to change the colours of a menu bar?
      Cuz I want a menu bar to have a black background and white text.
      I can't figure it out, how to do it?

  • @Bush98
    @Bush98 Před 2 lety +1

    Thank you bro!

  • @UnderArea51
    @UnderArea51 Před 2 lety

    Awesomeness!!!

  • @kornel001026
    @kornel001026 Před rokem

    thanks, it helped

  • @ad6612
    @ad6612 Před rokem +1

    What is the use of SwingUtilities.updateComponentTreeUI(this); ??
    Btw, LOVE your tutorials

  • @cheeese1412
    @cheeese1412 Před 2 lety +1

    I searched for revalidate()/repaint() because my frame does not update to the newly added component after removing the previous one (probably because I don't know how to use it).
    ... and SwingUtilities.updateComponentTreeUI(frame); worked very well.

  • @rzayevmehemmedeli
    @rzayevmehemmedeli Před 3 lety

    ƏLA

  • @marwayassine6022
    @marwayassine6022 Před 3 lety +10

    @Bro Code how can we add this to the snake game?

  • @Abhi-pq6qs
    @Abhi-pq6qs Před 2 lety

    I wanted to add a restart Button to the snake game you made a tutorial on how would i do that

  • @shadikaes
    @shadikaes Před 2 lety +1

    I tried to create this reset button for the game snake, but it doesn appear me... I have created a new class called "Reset" where I puted your "GameFrame" code, because if I put that on the GameFrame of the Snake Game one, its bugs...
    How I create a reset button for that game. Thanks for advance

  • @vitz8572
    @vitz8572 Před 2 lety

    can you make the same button to calculate and reset ?

  • @janil9357
    @janil9357 Před 3 lety +3

    Thanks bro
    tho I have a problem where the KeyAdapters stop working when I click the restart button
    any suggestions?

    • @BroCodez
      @BroCodez  Před 3 lety +6

      Java Swing's focus issues are kind of a pain. Basically the reset button is taking focus away from the game when you click on it. Try adding this line to within the GameFrame's actionPerformed() method after you add the panel: panel.requestFocusInWindow();
      EXAMPLE:
      @Override
      public void actionPerformed(ActionEvent e) {
      if(e.getSource() == startButton) {
      this.remove(panel);
      panel = new GamePanel();
      this.add(panel);
      panel.requestFocusInWindow();
      SwingUtilities.updateComponentTreeUI(this);
      }
      }

    • @janil9357
      @janil9357 Před 3 lety +1

      @@BroCodez Wow.. Bro you are the Best.. Thank you Thumbs up !!

  • @RyandracusChapman
    @RyandracusChapman Před 2 lety

    How do we get the JButton to take up only the space required for the size of the button? Why does it cover up all the rest of graphics that I already have and the button itself is the correct size? Does it come with a whole screen along with a button? If so, how do I get rid of that extra room?

    • @creativedogeMC
      @creativedogeMC Před rokem

      If you mean that the button covers the full screen, you need to write, in the place you make the JFrame,jframename.setLayout(null);

    • @RyandracusChapman
      @RyandracusChapman Před rokem +1

      @@creativedogeMC While I appreciate the reply, this is around 8 months late a response lol. I've long figured it out, but I understand the concern.

  • @madeariartha2546
    @madeariartha2546 Před 3 lety +1

    have to admit that java is much faster than python in game programming

    • @BroCodez
      @BroCodez  Před 3 lety +1

      Python is easy but slow af lol

    • @Mizu2023
      @Mizu2023 Před rokem

      Java is indeed faster
      It compiles 😎

  • @MrLegendGaming
    @MrLegendGaming Před 3 lety +2

    The code works fine for me except after I click it the KeyAdapaters stop working, can you tell me why?

    • @janil9357
      @janil9357 Před 3 lety +1

      me too donno what is wrong

    • @BroCodez
      @BroCodez  Před 3 lety +2

      The reset button currently has focus after you press it. You would need to give the focus back to your class that contains the game. You can add this line of code after you add your game to your container:
      Game.requestFocusInWindow();

    • @MrLegendGaming
      @MrLegendGaming Před 2 lety +1

      @@BroCodez Worked, thanks!!

    • @ismaildz5795
      @ismaildz5795 Před rokem +1

      ​@@BroCodez Thank you

  • @sheldor58
    @sheldor58 Před 3 lety +2

    I need this for snake. Please

    • @ashishpurohit4352
      @ashishpurohit4352 Před 3 lety +1

      GameFrame Class
      public class GameFrame extends JFrame implements ActionListener{
      GamePanel gamepanel;
      JButton resetButton;

      GameFrame(){
      this.setTitle("Snake");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setResizable(true);
      this.setSize(600,500);


      resetButton = new JButton();
      resetButton.setText("Reset");
      resetButton.setSize(100,50);
      resetButton.setLocation(0,200);
      resetButton.addActionListener(this);

      gamepanel = new GamePanel();
      this.add(resetButton);
      this.add(gamepanel);


      this.pack();
      this.setVisible(true);
      this.setLocationRelativeTo(null);


      }
      @Override
      public void actionPerformed(ActionEvent e) {
      if(e.getSource()==resetButton) {
      this.remove(gamepanel);
      gamepanel = new GamePanel();
      this.add(gamepanel);
      gamepanel.requestFocusInWindow();
      SwingUtilities.updateComponentTreeUI(this);

      }


      }
      }
      GamePanel
      below random
      random = new Random();

      int r = random.nextInt(256);
      int g = random.nextInt(256);
      int b = random.nextInt(256);
      this.setSize(500,500);
      this.setLocation(100,0);
      this.setBackground(new Color(r,g,b));
      this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));

      this.setFocusable(true);
      this.addKeyListener(new MyKeyAdapter());
      startGame();


      }

    • @shamsarwar1070
      @shamsarwar1070 Před 3 lety

      @@ashishpurohit4352 thanks bro! I spent a good while trying to figure that out

    • @ashishpurohit4352
      @ashishpurohit4352 Před 3 lety

      @@shamsarwar1070 welcome brother🤘

  • @niteshkuriyal3270
    @niteshkuriyal3270 Před 3 lety +1

    How can I create reset button for that TICTACTOE game?

    • @BroCodez
      @BroCodez  Před 3 lety +2

      you would just have the button call the constructor again for the tictactoe game

    • @Xanderci__
      @Xanderci__ Před 2 lety

      @@BroCodez how im struggling :< i cant think possibly to work the program

    • @stephanieezat-panah7750
      @stephanieezat-panah7750 Před 2 lety

      Hey Nitesh, I was working on that TicTacToe game as well. good question

    • @stephanieezat-panah7750
      @stephanieezat-panah7750 Před 2 lety

      I hit 'reply' before I finished my comment. When I tried to have the button call the constructor, and searched internet, I encountered, "No, you cannot call a constructor from a method"
      Still working on this.....