Java key bindings ⌨️

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ธ.ค. 2024

ความคิดเห็น • 69

  • @BroCodez
    @BroCodez  4 ปีที่แล้ว +31

    //***************************************************
    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());
    }
    }
    }
    //***************************************************

    • @gamingwithgavin1283
      @gamingwithgavin1283 4 ปีที่แล้ว +1

      Bro Code thank you!

    • @sohamdokhale8816
      @sohamdokhale8816 4 ปีที่แล้ว +2

      Thank u very much

    • @joyceasante8292
      @joyceasante8292 ปีที่แล้ว

      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 ());
      }
      }
      }

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 ปีที่แล้ว +3

    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

  • @burntskull6452
    @burntskull6452 2 ปีที่แล้ว +2

    One of the most favorite TH-camr of mine.

  • @noah77
    @noah77 4 ปีที่แล้ว +11

    I have taken a break in Java, currently doing Python.
    But it's a amazing tutorial and I will try it out.

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +4

      nice! I was planning on switching to Python soon once this playlist hits 100 videos

    • @noah77
      @noah77 4 ปีที่แล้ว +1

      @@BroCodez cool

    • @sohamdokhale8816
      @sohamdokhale8816 4 ปีที่แล้ว +3

      Woah nice

  • @amikoss5635
    @amikoss5635 11 หลายเดือนก่อน +1

    He put swing tutorials right after the core concepts just to keep it interesting :)

  • @ParikshitShrestha-o1z
    @ParikshitShrestha-o1z 3 หลายเดือนก่อน

    Its awsome BRO..

  • @kemann3815
    @kemann3815 3 ปีที่แล้ว +3

    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❤

  • @Monsta1291
    @Monsta1291 2 ปีที่แล้ว +1

    amazing

  • @hamidhkh109
    @hamidhkh109 3 ปีที่แล้ว +4

    Hi, Thanks for your great tutorial. would you know why for example UpAction should be a class and not a method?

  • @nawfalnjm5699
    @nawfalnjm5699 3 ปีที่แล้ว +1

    thanks . your playlist is amazing !

  • @WilliamNash-w5w
    @WilliamNash-w5w ปีที่แล้ว

    YO THANKS BRO HELPED A LOT WHEN GOOGLE COULDN'T

  • @elvastaudinger4991
    @elvastaudinger4991 3 ปีที่แล้ว +3

    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.

    • @adeldzaferi7519
      @adeldzaferi7519 3 ปีที่แล้ว +2

      I had the same problem, the problem is the imports. Use the same imports as BroCode and it should be fine.

    • @baactiba3039
      @baactiba3039 2 ปีที่แล้ว

      Late but my fix:
      error was on " g = new G();"
      I had to change it to "G g = new G();"

  • @matterb6049
    @matterb6049 2 ปีที่แล้ว

    Brilliant vids bro

  • @microsoftgamerx854
    @microsoftgamerx854 ปีที่แล้ว +1

    @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());
    }
    }
    }
    }

  • @DeepakMali-xj8kr
    @DeepakMali-xj8kr 10 หลายเดือนก่อน

    Awesome !!

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 4 หลายเดือนก่อน

    💙💥

  • @mindreader860
    @mindreader860 3 ปีที่แล้ว

    Great

  • @udaysaimanoj2117
    @udaysaimanoj2117 2 ปีที่แล้ว +1

    Commented only to help the channel...Haha🤣

  • @JazzInATinCan
    @JazzInATinCan 2 ปีที่แล้ว

    This is a supportive comment to help with tha algorithm

  • @accumulator4825
    @accumulator4825 2 ปีที่แล้ว

    Awesome, thanks 😎

  • @MrLoser-ks2xn
    @MrLoser-ks2xn ปีที่แล้ว

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 ปีที่แล้ว

    thanks bro

  • @maheshbh1
    @maheshbh1 3 ปีที่แล้ว +2

    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

    • @OneEgg42
      @OneEgg42 3 ปีที่แล้ว +1

      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.

    • @adeldzaferi7519
      @adeldzaferi7519 3 ปีที่แล้ว

      I had the same problem, the problem is the imports. Use the same imports as BroCode and it should be fine.

    • @tj9272
      @tj9272 3 ปีที่แล้ว +3

      instead of: import java.awt.Desktop.Action;
      Use: import javax.swing.Action;

    • @adrianrodriguez80
      @adrianrodriguez80 2 ปีที่แล้ว

      @@tj9272 thank you so much!, why does this solve the problem?

  • @guramee
    @guramee ปีที่แล้ว

    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");

  • @marioshusband3700
    @marioshusband3700 4 ปีที่แล้ว +1

    can you do more python tutorials

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      Once this Java playlist hits 100 videos I'm switching to Python. Only 4 videos left

    • @marioshusband3700
      @marioshusband3700 4 ปีที่แล้ว +1

      Bro Code thank you

  • @ahmadal-anzi7160
    @ahmadal-anzi7160 2 ปีที่แล้ว

    Thanks

  • @angelcastineira2561
    @angelcastineira2561 4 ปีที่แล้ว +1

    thanks!

  • @leviplays7466
    @leviplays7466 9 หลายเดือนก่อน

    you're the bro

  • @marioshusband3700
    @marioshusband3700 4 ปีที่แล้ว +2

    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!

  • @BRADLEYBREEN_
    @BRADLEYBREEN_ 4 ปีที่แล้ว

    Is there a way to make more than one key work at a time?

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +5

      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.

    • @joaqueim5201
      @joaqueim5201 8 หลายเดือนก่อน

      ​@@BroCodezputting this comment here so I can save it for later

  • @johnandrewsuertefelipe4126
    @johnandrewsuertefelipe4126 4 ปีที่แล้ว

    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.

    • @adamsalem-s
      @adamsalem-s 3 ปีที่แล้ว

      i recommend using the unity game engine because it have good 2D game support and its easier to do most things

    • @CSBMathewKJ
      @CSBMathewKJ 2 ปีที่แล้ว

      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

  • @stiki6972
    @stiki6972 4 ปีที่แล้ว

    I love you thanks

  • @smokerd1543
    @smokerd1543 2 ปีที่แล้ว

    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.

  • @antoinealam6531
    @antoinealam6531 2 หลายเดือนก่อน

    bro

  • @thermometerz3599
    @thermometerz3599 3 ปีที่แล้ว

    Tysm

  • @jozefs.8262
    @jozefs.8262 4 ปีที่แล้ว

    THANKS! A lot

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +1

      thanks for watching Jozef

  • @augischadiegils.5109
    @augischadiegils.5109 4 ปีที่แล้ว

    Thanks bro :)

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      thanks for watching chadie!

  • @eugenezuev7349
    @eugenezuev7349 6 หลายเดือนก่อน

    Bro bindings

  • @gamingwithgavin1283
    @gamingwithgavin1283 4 ปีที่แล้ว +3

    Bro this guy is catching up to you th-cam.com/users/nobelsushank! He’s a rip off of your channel!!

  • @sajeucettefoistunevaspasme
    @sajeucettefoistunevaspasme 3 ปีที่แล้ว +1

    1

  • @dilanzuluaga8924
    @dilanzuluaga8924 3 ปีที่แล้ว

    te amo
    uwu

  • @matterb6049
    @matterb6049 2 ปีที่แล้ว

    Brilliant vids bro

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 ปีที่แล้ว

    Thanks