Java: reset button for your game 🎮
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
//--------------------------------------------------------------------------------------------
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));
}
}
//--------------------------------------------------------------------------------------------
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.
You are amazing!
Keep these Java videos going!!!
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!
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 !!
very useful as always bro
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!
This was extremely helpful! Thanks again
Very useful!
Thank you Bro Code :) 👍
mükəmməl
Your Java series is doing very well!
Wholesome;
w00t!
@@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?
Thank you bro!
Awesomeness!!!
thanks, it helped
What is the use of SwingUtilities.updateComponentTreeUI(this); ??
Btw, LOVE your tutorials
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.
ƏLA
@Bro Code how can we add this to the snake game?
I tryed and didnt work in snake game
I wanted to add a restart Button to the snake game you made a tutorial on how would i do that
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
can you make the same button to calculate and reset ?
Thanks bro
tho I have a problem where the KeyAdapters stop working when I click the restart button
any suggestions?
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);
}
}
@@BroCodez Wow.. Bro you are the Best.. Thank you Thumbs up !!
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?
If you mean that the button covers the full screen, you need to write, in the place you make the JFrame,jframename.setLayout(null);
@@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.
have to admit that java is much faster than python in game programming
Python is easy but slow af lol
Java is indeed faster
It compiles 😎
The code works fine for me except after I click it the KeyAdapaters stop working, can you tell me why?
me too donno what is wrong
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();
@@BroCodez Worked, thanks!!
@@BroCodez Thank you
I need this for snake. Please
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();
}
@@ashishpurohit4352 thanks bro! I spent a good while trying to figure that out
@@shamsarwar1070 welcome brother🤘
How can I create reset button for that TICTACTOE game?
you would just have the button call the constructor again for the tictactoe game
@@BroCodez how im struggling :< i cant think possibly to work the program
Hey Nitesh, I was working on that TicTacToe game as well. good question
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.....