Java anonymous objects 🃏

Поделиться
HTML-код
  • Опубликовано: 29 дек 2024

Комментарии • 54

  • @BroCodez
    @BroCodez  4 года назад +14

    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class Main {

    public static void main(String[] args) {

    JFrame frame = new JFrame();
    ArrayList deck = new ArrayList();

    /*
    ImageIcon AC = new ImageIcon("src\\cards\\1.png");
    JLabel AClabel = new JLabel(AC);
    deck.add(AClabel);

    ImageIcon TwoC = new ImageIcon("src\\cards\\2.png");
    JLabel TwoClabel = new JLabel(TwoC);
    deck.add(TwoClabel);

    ... repeat for all 52 cards :(
    */

    for(int i =1;i

    • @joyceasante8292
      @joyceasante8292 Год назад

      "Hard"way to add objects
      import java.awt.*;
      import java.util.*;
      import javax.swing.*;
      public class Main
      {
      public static void main (String[]args)
      {
      JFrame frame = new JFrame ();
      ArrayList < JLabel > deck = new ArrayList < JLabel > ();
      ImageIcon AC = new ImageIcon ("src\\cards\\1.png");
      JLabel AClabel = new JLabel (AC);
      deck.add (AClabel);
      ImageIcon twoC = new ImageIcon ("src\\cards\\2.png");
      JLabel twoClabel = new JLabel (twoC);
      deck.add (twoClabel);
      frame.add (deck.get (0));
      frame.add (deck.get (1));
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.setSize (380, 380);
      frame.setLayout (new FlowLayout ());
      frame.setVisible (true);
      }
      }
      ______________________________
      SIMPLE WAY
      import java.awt.*;
      import java.util.*;
      import javax.swing.*;
      public class Main
      {
      public static void main (String[]args)
      {
      JFrame frame = new JFrame ();
      ArrayList < JLabel > deck = new ArrayList < JLabel > ();
      for (int i = 1; i

  • @angelcastineira2561
    @angelcastineira2561 4 года назад +12

    creating the array is so clever! at first i thought it was a little redundant since you could this and save one step
    frame.add(new JLabel(new ImageIcon("src\\cards\\"+i+".png")));
    but then I realized that creating and filling the array, you can use the array positions as "names" for the objects so you can correct a specific Jlabel later.
    beautiful!

  • @ChrisMusicRSA
    @ChrisMusicRSA 3 года назад +2

    This may just be me... but I feel like this is like watching a really entertaining gaming video. It might be the colors, idk.
    Thx for the great explanation! :P

  • @DNA912
    @DNA912 4 года назад +1

    Not what I was looking for. But something I never thought of doing and glad to know now.

  • @owenagnel4139
    @owenagnel4139 4 года назад +2

    Great vid as usual!

  • @girl6994
    @girl6994 4 года назад +1

    Line NO.10. We don’t need to type two times JLabel in if you we are using JDK 8 or above.

  • @anthonydeluca9832
    @anthonydeluca9832 Год назад +1

    This is also likely more efficient in memory as you don't have a object in multiple places in memory I.E.
    Bunny bunny = new Bunny(); --- adds object to memory
    Pet pet = new Pet(bunny); --- creates another object in memory and stores the location of bunny as well (I think?)
    vs
    Pet pet = new Pet(new Bunny()); -- the Bunny no longer exists afterwards and doesn't need a pointer to it so all the info is in pet and is still accessible
    PLEASE DO NOT TAKE THIS AS PROFESSIONAL ADVISE -- this is my personal interpretation of the benefits and I could be WRONG, if so please make me aware. Either way it still saves lines of code and effort.

  • @yenegew-tech
    @yenegew-tech 2 года назад

    No words to tell How much u'r helping me

  • @khiemdo5527
    @khiemdo5527 3 года назад

    Another great video. Thanks a lot. Happy New Year and wishing you all the best.

  • @kemann3815
    @kemann3815 3 года назад

    Best of the best

  • @tharinduperera8758
    @tharinduperera8758 2 года назад

    i think using this for swing is not the best idea as naming the object has flexibilty to use them for styling and other action and mouse listners etc.. but while programing this can be used here and there to remove the redundancy and unnecessory codes.

  • @usamanadeem145
    @usamanadeem145 3 года назад

    Great

  • @percivalgebashe4376
    @percivalgebashe4376 Год назад

    Nice

  • @cll2598
    @cll2598 Год назад

    Legend as always

  • @Dygit
    @Dygit 4 года назад +1

    Very cool video :) Now that I think about it, I probably could have been more efficient by using this method in one of my prior projects

    • @BroCodez
      @BroCodez  4 года назад +6

      Get used to that. As you gain new skills, you find more shortcuts to things you have coded previously lol

  • @davecone2063
    @davecone2063 3 года назад

    Coolies! Thank you for the lesson.

  • @mindreader860
    @mindreader860 3 года назад

    Great♥

  • @wolanus
    @wolanus 4 года назад

    great tutorial

  • @eugenezuev7349
    @eugenezuev7349 6 месяцев назад

    helpful, thanks

  • @tasneemayham974
    @tasneemayham974 Год назад

    This is to defeat the EVIL RUclips ALGORITHM.
    GO, BRO!!

  • @daniyalahsen5707
    @daniyalahsen5707 4 года назад

    AOA, Man you are under-rated. You really should have more subs than you already have (by the quality of your work). Also, let's say I want to download your videos on MP4 quality, how may I so that? (Legally)

    • @BroCodez
      @BroCodez  4 года назад +1

      You're comment ended up in my spam folder lol. Thank you. However I am not sure how to download RUclips videos

    • @daniyalahsen5707
      @daniyalahsen5707 4 года назад

      @@BroCodez wow Google sure knows what is spam and what is not. Wondering where all the 200 IQ algorithm experts went. Also AOA stands for Assalam O Alaikum which is Arabic for may you live in peace (just if you were wondering). AOA again.

  • @vuchinh9989
    @vuchinh9989 3 года назад

    Cool!

  • @developerjunior446
    @developerjunior446 3 года назад +1

    Can you make video your whole day as Programmer ?

  • @MrJIsBack
    @MrJIsBack 3 года назад

    But isn't it considered bad practice to just skip naming variables to make readable code?
    I've been forcing myself to do so thinking it was a better idea, but I didn't know not naming objects was actually considered to be a valid method...

  • @nawfalnjm5699
    @nawfalnjm5699 3 года назад

    omg . Just 3 lines of code , thanks !

  • @nihadyaqublu2359
    @nihadyaqublu2359 2 года назад

    this is genius

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 года назад

    thanks bro

  • @sidof8065
    @sidof8065 4 года назад

    good tuto

    • @BroCodez
      @BroCodez  4 года назад

      thanks for watching Sidof

  • @Electronic__Project
    @Electronic__Project 2 года назад +1

    Where can i find pictures Like u have BRO ?

  • @desmiles1567
    @desmiles1567 3 года назад

    🤩

  • @thiagobrittosouza
    @thiagobrittosouza 3 года назад

    thanks again bro

  • @mahdib9361
    @mahdib9361 4 года назад +2

    jAVAFX TUTURIAL PLEASE

  • @KentHervey
    @KentHervey 4 года назад

    Thanks

  • @kingkock1
    @kingkock1 2 года назад

    We're cooking with petrol now!!

  • @noah77
    @noah77 4 года назад +1

    Can I know where are you from??
    (Not intending to be bad)
    For me, I am from 🇬🇧🇬🇧

    • @noah77
      @noah77 4 года назад +1

      @@BroCodez nice to meet you bud:)

    • @noah77
      @noah77 4 года назад +2

      @@BroCodez love your java tutorials:)

  • @manuelgonzalezpalafox2627
    @manuelgonzalezpalafox2627 2 года назад

    Ly bro 7

  • @pesterlig
    @pesterlig 4 года назад

    Danke

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Год назад

  • @girl6994
    @girl6994 4 года назад +1

    I don’t know how to download these pictures so I can’t type same project with you.

    • @BroCodez
      @BroCodez  4 года назад

      I made them myself. This is more of a demonstration, but any pictures or images could work

    • @BroCodez
      @BroCodez  4 года назад

      holy crap, that would be awesome. You could write a sub-program for each card for each card's effects. I'm now imagining all the work that went into a game like Magic The Gathering Online

    • @chzzh6634
      @chzzh6634 3 года назад

      @@VXFluff ayy bro. u made good music. naisu

  • @zari_723
    @zari_723 2 года назад

    hi :)

  • @chzzh6634
    @chzzh6634 3 года назад

    I'm so lazy to continue. HELP :(

  • @ashish8-v4g
    @ashish8-v4g Месяц назад

    import java.util.Scanner;
    class AE extends Exception {
    AE(String m) {
    super(m);
    }
    }
    class Main {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int age;
    age = s.nextInt();
    try {
    if (age < 18) {
    throw new AE("You are not 18");
    } else {
    System.out.println("You are 18+");
    }
    } catch (AE e) {
    System.out.println("An error occurred: " + e.getMessage());
    }
    }
    }
    other way around

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 года назад

    Thanks