What does this snippet mean?

0

Recently I came across a stone in the shoe, I was going to comment on a code, but I realized that I was filling sausage by commenting it, because I do not know what the actual use of the code is:

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
lblResolucao.setText(+d.width +" x " +d.height);

In case it is a Swing code to capture the resolution of the device, but I have no idea the real reason they are there (it's kind of confusing to even explain).

I would like you to explain the first 2 lines DETAILED to me so that I can really understand the code.

    
asked by anonymous 18.04.2017 / 03:52

2 answers

3

Reading the code:

In the Toolkit tk = Toolkit.getDefaultToolkit(); line you use the static method getDefaultToolkit() to return the Toolkit of the platform, with Toolkit being a superclass of all current implementations of Abstract Window Toolkit(AWT) , and saved in the reference tk , of type Toolkit .

In the Dimension d = tk.getScreenSize(); line you access the getScreenSize() method of its tk object, which returns an object of type Dimension , which encapsulates width and height values with screen width and height .

/*
 * Output:
Screen width = 1280
Screen height = 1024
 */

import java.awt.Dimension;
import java.awt.Toolkit;

public class MainClass {
  public static void main(String[] args) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    System.out.println("Screen width = " + d.width);
    System.out.println("Screen height = " + d.height);

  }
}
    
18.04.2017 / 12:11
3

Toolkit is a super class that contains lots of information about the most diverse types of windows and frames in Java. Understand it as if it contained a lot of raw information about the windows, as it is not advised to access this information directly you use other classes to get the refined information of the first.

In your case, you use the Dimension class to get dimension information from your device window. For more information, visit the documentation for class Toolkit .

link

    
18.04.2017 / 11:53