//*************************************************** public class Main{
public static void main(String[] args ){ // Key Bindings = bind an Action to a KeyStroke // don't require you to click a component to give it focus // all Swing components use Key Bindings // increased flexibility compared to KeyListeners // can assign key strokes to individual Swing components // more difficult to utilize and set up :(
Game game = new Game(); } } //*************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Game { JFrame frame; JLabel label; Action upAction; Action downAction; Action leftAction; Action rightAction;
Game(){
frame = new JFrame("KeyBinding Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(420,420); frame.setLayout(null);
label = new JLabel(); label.setBackground(Color.red); label.setBounds(100, 100, 100, 100); label.setOpaque(true);
upAction = new UpAction(); downAction = new DownAction(); leftAction = new LeftAction(); rightAction = new RightAction();
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
That space invader type game idea that you mentioned in the video about the keylistener was great practicing project. Love the series, probably jumping to python after i finished every 100 videos on java. Thanks bro❤
i put Action upAction; Action downAction; Action letfAction;Action rightAction; in the global area. upAction=new upAction() and so on in the local are. console throws type mismatch:cannnot convert game.UpAction to Desktop.Action. btw i did extends AbstractAction in each action class.
Before the constructor, I did all the imports, and declared Action upAction; After that, when I try , inside Game() { upAction = new UpAction(); There are red marks
copy and paste Bro's Game class code into your code. I had the same issue and had no idea why it wasn't working, but copy pasting Bro's Game code solved it. I really have no clue what i did wrong.
If you want the keybinding to work properly make sure to add JComponent.WHEN_IN_FOCUSED_WINDOW in getInputMap body.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "rightActionKey");
wanna hear something? a couple weeks or months ago you subscribed to me and your a programming channel and before I heard of your channel I was learning python COINCIDENCE ?! I THINK NOT!
that's a little tricky. Let's say we have an image and we would like to use the w,s,a,d keys to move this image up,left,down, and right. You might want to write something like this: use boolean values for up,left,down,right. and initially set them to false when you press either w,s,a,d change the corresponding boolean value to true when you release a key, change the corresponding boolean back to false Everytime you draw you're icon or image, it should move a certain amound depnding on what booleans are true.
I recently started java like a week ago and I tried to make a 2D game but I wanted to know how you can go like top right by pressing w and d at the same time.
Define a velocity vector for the motion of your object. By default, when no key is pressed, the object stays still and the velocity vector is a null vector. make it such that pressing w adds vj to the velocity vector, pressing d adds vi to the velocity vector and so an (i and j are standard unit vectors and v is the unit of speed). Then make it such that the object always moves along the velocity vector (maybe using another thread? not sure). If you want to maintain a constant maximum speed in all directions, normalize the vector and then multiply by v
Hello. First of all, I want to thank you for all the great knowledge you've given me on java(and all the codes I've stolen from you muahahaah). It was amazing. Now for my question xd. I don't really get my keybindings to work unless I click on an object. To be more precise-I've added key bindings to my text field but as soon as I click a JButton the keys don't do anything until I press the text field again. Help would be appreciated. Cheers bro.
//***************************************************
public class Main{
public static void main(String[] args ){
// Key Bindings = bind an Action to a KeyStroke
// don't require you to click a component to give it focus
// all Swing components use Key Bindings
// increased flexibility compared to KeyListeners
// can assign key strokes to individual Swing components
// more difficult to utilize and set up :(
Game game = new Game();
}
}
//***************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game {
JFrame frame;
JLabel label;
Action upAction;
Action downAction;
Action leftAction;
Action rightAction;
Game(){
frame = new JFrame("KeyBinding Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
label = new JLabel();
label.setBackground(Color.red);
label.setBounds(100, 100, 100, 100);
label.setOpaque(true);
upAction = new UpAction();
downAction = new DownAction();
leftAction = new LeftAction();
rightAction = new RightAction();
label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
label.getActionMap().put("upAction", upAction);
label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
label.getActionMap().put("downAction", downAction);
label.getInputMap().put(KeyStroke.getKeyStroke('a'), "leftAction");
label.getActionMap().put("leftAction", leftAction);
label.getInputMap().put(KeyStroke.getKeyStroke('d'), "rightAction");
label.getActionMap().put("rightAction", rightAction);
frame.add(label);
frame.setVisible(true);
}
public class UpAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX(), label.getY()-10);
}
}
public class DownAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX(), label.getY()+10);
}
}
public class LeftAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX()-10, label.getY());
}
}
public class RightAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX()+10, label.getY());
}
}
}
//***************************************************
Bro Code thank you!
Thank u very much
Practicing....
public class Main{
public static void main(String[]args){
Game game = new Game();
}
}
*********
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game
{
JFrame frame;
JLabel label;
Action upAction;
Action downAction;
Action leftAction;
Action rightAction;
Game ()
{
frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (210, 210);
frame.setLayout (null);
label = new JLabel ();
label.setBackground (Color.black);
label.getInputMap ().put (KeyStroke.getKeyStroke ('w'), upAction);
label.getActionMap ().put (upAction, upAction);
label.getInputMap ().put (KeyStroke.getKeyStroke ('s'), upAction);
label.getActionMap ().put (downAction, downAction);
label.getInputMap ().put (KeyStroke.getKeyStroke ('a'), upAction);
label.getActionMap ().put (leftAction, leftAction);
label.getInputMap ().put (KeyStroke.getKeyStroke ('d'), upAction);
label.getActionMap ().put (rightAction, rightAction);
frame.add (label);
frame.setVisible (true);
}
public class upAction extends AbstractAction
{
@Override public void actionPerformed (ActionEvent e)
{
label.setLocation (label.getX (), label.getY () - 10);
}
}
public class downAction extends AbstractAction
{
@Override public void actionPerformed (ActionEvent e)
{
label.setLocation (label.getX (), label.getY () + 10);
}
}
public class leftAction extends AbstractAction
{
@Override public void actionPerformed (ActionEvent e)
{
label.setLocation (label.getX () - 10, label.getY ());
}
}
public class rightAction extends AbstractAction
{
@Override public void actionPerformed (ActionEvent e)
{
label.setLocation (label.getX () + 10, label.getY ());
}
}
}
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
One of the most favorite TH-camr of mine.
I have taken a break in Java, currently doing Python.
But it's a amazing tutorial and I will try it out.
nice! I was planning on switching to Python soon once this playlist hits 100 videos
@@BroCodez cool
Woah nice
He put swing tutorials right after the core concepts just to keep it interesting :)
Its awsome BRO..
That space invader type game idea that you mentioned in the video about the keylistener was great practicing project. Love the series, probably jumping to python after i finished every 100 videos on java. Thanks bro❤
amazing
Hi, Thanks for your great tutorial. would you know why for example UpAction should be a class and not a method?
thanks . your playlist is amazing !
YO THANKS BRO HELPED A LOT WHEN GOOGLE COULDN'T
i put Action upAction; Action downAction; Action letfAction;Action rightAction; in the global area. upAction=new upAction() and so on in the local are. console throws type mismatch:cannnot convert game.UpAction to Desktop.Action. btw i did extends AbstractAction in each action class.
I had the same problem, the problem is the imports. Use the same imports as BroCode and it should be fine.
Late but my fix:
error was on " g = new G();"
I had to change it to "G g = new G();"
Brilliant vids bro
@BroCodez
Can you help me solving the issue, the arrows key doesn't move the labe2?
package Practice.GUI.KeyBinding.P2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Game2 {
Action Up,Down,Left,Right,Up2,Down2,Left2,Right2;
JFrame frame;
JLabel label1,label2;
Game2(){
ImageIcon icon = new ImageIcon("rocket.png");
Image resize = icon.getImage().getScaledInstance(50,70,Image.SCALE_DEFAULT);
ImageIcon ric = new ImageIcon(resize);
ImageIcon icon2 = new ImageIcon("rocket.png");
Image resize2 = icon.getImage().getScaledInstance(50,70,Image.SCALE_DEFAULT);
ImageIcon ric2 = new ImageIcon(resize2);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(Color.black);
label1 = new JLabel();
label1.setBounds(0,300,100,100);
label1.setIcon(ric);
label2 = new JLabel();
label2.setBounds(430,300,100,100);
label2.setIcon(ric2);
Up = new UpAction();
Down = new DownAction();
Left = new LeftAction();
Right = new RightAction();
Up2 = new UpAction();
Down2 = new DownAction();
Left2 = new LeftAction();
Right2 = new RightAction();
label1.getInputMap().put(KeyStroke.getKeyStroke('w'),"upAction1");
label1.getActionMap().put("upAction1",Up);
label1.getInputMap().put(KeyStroke.getKeyStroke('s'),"downAction1");
label1.getActionMap().put("downAction",Down);
label1.getInputMap().put(KeyStroke.getKeyStroke('a'),"leftAction1");
label1.getActionMap().put("leftAction1",Left);
label1.getInputMap().put(KeyStroke.getKeyStroke('d'),"rightAction1");
label1.getActionMap().put("rightAction1",Right);
label2.getInputMap().put(KeyStroke.getKeyStroke("UP"), "upAction2");
label2.getActionMap().put("upAction2", Up2);
label2.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "downAction2");
label2.getActionMap().put("downAction2", Down2);
label2.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "leftAction2");
label2.getActionMap().put("leftAction2", Left2);
label2.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "rightAction2");
label2.getActionMap().put("rightAction2", Right2);
frame.add(label1);
frame.add(label2);
frame.setVisible(true);
}
public class UpAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == label1) {
label1.setLocation(label1.getX(), label1.getY() - 10);
} else if(e.getSource() == label2) {
label2.setLocation(label2.getX(), label2.getY() - 10);
}
}
}
public class DownAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == label1) {
label1.setLocation(label1.getX(), label1.getY() + 10);
} else if(e.getSource() == label2) {
label2.setLocation(label2.getX(), label2.getY() + 10);
}
}
}
public class LeftAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == label1) {
label1.setLocation(label1.getX()-10, label1.getY());
} else if(e.getSource() == label2) {
label2.setLocation(label2.getX()-10, label2.getY());
}
}
}
public class RightAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == label1) {
label1.setLocation(label1.getX()+10, label1.getY());
} else if(e.getSource() == label2) {
label2.setLocation(label2.getX()+10, label2.getY());
}
}
}
}
Awesome !!
💙💥
Great
Commented only to help the channel...Haha🤣
This is a supportive comment to help with tha algorithm
Awesome, thanks 😎
thanks bro
Before the constructor, I did all the imports, and declared
Action upAction;
After that, when I try ,
inside Game() {
upAction = new UpAction();
There are red marks
copy and paste Bro's Game class code into your code. I had the same issue and had no idea why it wasn't working, but copy pasting Bro's Game code solved it. I really have no clue what i did wrong.
I had the same problem, the problem is the imports. Use the same imports as BroCode and it should be fine.
instead of: import java.awt.Desktop.Action;
Use: import javax.swing.Action;
@@tj9272 thank you so much!, why does this solve the problem?
If you want the keybinding to work properly make sure to add JComponent.WHEN_IN_FOCUSED_WINDOW in getInputMap
body.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "rightActionKey");
can you do more python tutorials
Once this Java playlist hits 100 videos I'm switching to Python. Only 4 videos left
Bro Code thank you
Thanks
thanks!
you're the bro
wanna hear something? a couple weeks or months ago you subscribed to me and your a programming channel and before I heard of your channel I was learning python
COINCIDENCE ?! I THINK NOT!
Is there a way to make more than one key work at a time?
that's a little tricky. Let's say we have an image and we would like to use the w,s,a,d keys to move this image up,left,down, and right. You might want to write something like this:
use boolean values for up,left,down,right. and initially set them to false
when you press either w,s,a,d change the corresponding boolean value to true
when you release a key, change the corresponding boolean back to false
Everytime you draw you're icon or image, it should move a certain amound depnding on what booleans are true.
@@BroCodezputting this comment here so I can save it for later
I recently started java like a week ago and I tried to make a 2D game but I wanted to know how you can go like top right by pressing w and d at the same time.
i recommend using the unity game engine because it have good 2D game support and its easier to do most things
Define a velocity vector for the motion of your object. By default, when no key is pressed, the object stays still and the velocity vector is a null vector. make it such that pressing w adds vj to the velocity vector, pressing d adds vi to the velocity vector and so an (i and j are standard unit vectors and v is the unit of speed). Then make it such that the object always moves along the velocity vector (maybe using another thread? not sure). If you want to maintain a constant maximum speed in all directions, normalize the vector and then multiply by v
I love you thanks
Hello. First of all, I want to thank you for all the great knowledge you've given me on java(and all the codes I've stolen from you muahahaah). It was amazing. Now for my question xd. I don't really get my keybindings to work unless I click on an object. To be more precise-I've added key bindings to my text field but as soon as I click a JButton the keys don't do anything until I press the text field again. Help would be appreciated. Cheers bro.
bro
Tysm
THANKS! A lot
thanks for watching Jozef
Thanks bro :)
thanks for watching chadie!
Bro bindings
Bro this guy is catching up to you th-cam.com/users/nobelsushank! He’s a rip off of your channel!!
1
exact
not completely
te amo
uwu
Brilliant vids bro
Thanks