Font Metrics

Font Metrics

Font properties that affect the rendering of strings are represented by the FontMetrics class. You can determine the FontMetrics of the current font in a graphics context with Graphics.getFontMetrics(). If all you want to know is the estimated width of a string in pixels, FontMetrics provides a stringWidth() method for just this purpose. However, a more general method is also provided, called getStringBounds(). This method will return a Rectangle2D instance representing the bounding box of a string when drawn in the given Font. The following program shows how to use this information to draw a rectangle around a string:

 import java.awt.*;import java.awt.event.*;import java.awt.geom.*;public class AWTStringSize extends Frame {  private String __string;  public void paint(Graphics g) {    FontMetrics metrics = g.getFontMetrics();    Rectangle2D rect;    int x, y;    // Get dimensions of string    rect = metrics.getStringBounds(__string, g);    x = (getWidth() - (int)rect.getWidth())/2;    y = (getHeight() - (int)rect.getHeight())/2;    g.drawString(__string, x, y);    // Draw a box slightly outside of the bounds of the string    g.drawRect(x - 1, y - (int)rect.getHeight() - 1,               (int)rect.getWidth() + 2,               (int)rect.getHeight() + 2);                   }  public AWTStringSize(String string) {    __string = string;  }  public static final void main(String[] args) {    AWTStringSize frame;    if(args.length != 1) {      System.err.println("Usage: AWTStringSize string");      return;    }    frame = new AWTStringSize(args[0]);    frame.addWindowListener(new WindowAdapter() {      public void windowClosing(WindowEvent e) {        Window window = e.getWindow();        window.setVisible(false);        window.dispose();        System.exit(0);      }      });    frame.setSize(400, 400);    frame.setVisible(true);  }}
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as