Calcolare la Lunghezza in Pixel di una Stringa
Mostreremo quattro modi per fare questo:
Primo modo: una sola classe che non ha costruttore, ha un metodo getStrWidth che calcola lalunghezza della stringa in pixel
Classe: StrPixLgh1 |

|
1 package strpixlgh1;
2
3 import java.awt.FontMetrics;
4 import java.awt.Graphics;
5 import java.awt.image.BufferedImage;
6
7 public class StrPixLgh1 {
8
9 public static void main(String[] args) {
10 String lblStr = "Questo è un semplice test";
11 int sWpx = getStrWidth(lblStr);
12
13 System.out.println("Larghezza della stringa: " + sWpx + " pixel");
14 }
15
16 public static int getStrWidth(String str) {
17 // Crea un'immagine vuota (1x1 pixel)
18 BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
19 Graphics g = img.createGraphics();
20
21 // Ottieni le metriche del font
22 FontMetrics fm = g.getFontMetrics();
23
24 // Calcola la larghezza della stringa
25 int sWpx = fm.stringWidth(str);
26 return sWpx;
27 }
28 } |
Secondo metodo: una sola classe con costruttore che accetta una stringa come argomento, ed il metodo getStrWidth().
Classe: StrPixLgh2 |

|
1 package strpixlgh2;
2
3 import java.awt.FontMetrics;
4 import java.awt.Graphics;
5 import java.awt.image.BufferedImage;
6
7 public class StrPixLgh2 {
8 private String str;
9
10 public StrPixLgh2(String str) {
11 this.str = str;
12 }
13
14 public int getStrWidth() {
15 // Crea un'immagine vuota (1x1 pixel)
16 BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
17 Graphics g = img.createGraphics();
18
19 // Ottieni le metriche del font
20 FontMetrics fm = g.getFontMetrics();
21
22 // Calcola la larghezza della stringa
23 int sWpx = fm.stringWidth(str);
24 return sWpx;
25 }
26
27 public static void main(String[] args) {
28 String lblStr = "Questo è un semplice test";
29 StrPixLgh2 strPx = new StrPixLgh2(lblStr);
30 int sWpx = strPx.getStrWidth();
31
32 System.out.println("Larghezza della stringa: " + sWpx + " pixel");
33 }
34 } |
Terzo metodo: una sola classe, un costruttore con arg String, un metodo che calcola e uno che legge
Classe: StrPixLgh3 |

|
1 package strpixlgh3;
2
3 import java.awt.FontMetrics;
4 import java.awt.Graphics;
5 import java.awt.image.BufferedImage;
6
7 public class StrPixLgh3 {
8 private final int strWidth;
9
10 public StrPixLgh3(String str) {
11
12 this.strWidth = calculateStrWidth(str);
13 }
14
15 private int calculateStrWidth(String str) {
16
17 BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
18 Graphics g = img.createGraphics();
19
20
21 FontMetrics fm = g.getFontMetrics();
22
23
24 return fm.stringWidth(str);
25 }
26
27 public int getStrWidth() {
28 return strWidth;
29 }
30
31 public static void main(String[] args) {
32 String lblStr = "Questo è un semplice test";
33 StrPixLgh3 strPx = new StrPixLgh3(lblStr);
34 int sWpx = strPx.getStrWidth();
35
36 System.out.println("Larghezza della stringa: " + sWpx + " pixel");
37 }
38 } |
Quarto modo: con due classi separate. Una classe con il metodo main e un'altra classe che calcola la lunghezza della stringa.
Classe: StrPixLgh4 |

|
1 package strpixlgh4;
2
3 import java.awt.FontMetrics;
4 import java.awt.Graphics;
5 import java.awt.image.BufferedImage;
6
7 public class StrPixLgh4 {
8 public static void main(String[] args) {
9 String lblStr = "Questo è un semplice test";
10 StrPxLghCalculator strPx = new StrPxLghCalculator(lblStr);
11 int sWpx = strPx.calculateStrWidth();
12
13 System.out.println("Larghezza della stringa: " + sWpx + " pixel");
14 }
15 }
16
17 class StrPxLghCalculator {
18 private final String str;
19
20 public StrPxLghCalculator(String str) {
21 this.str = str;
22 }
23
24 public int calculateStrWidth() {
25
26 BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
27 Graphics g = img.createGraphics();
28
29
30 FontMetrics fm = g.getFontMetrics();
31
32
33 return fm.stringWidth(str);
34 }
35 } |
La considerazione finale è che:
I costruttori non possono avere un tipo di ritorno esplicito, nemmeno void. Quindi non è possibile restituire direttamente il valore della larghezza della stringa dal costruttore.
|