Location>code7788 >text

Graphics2D Drawing Methods Summary

Popularity:358 ℃/2024-08-26 13:49:18

I. Introduction

In the development may encounter such a class of scenarios, business complexity is not too high, the technical difficulty is not too deep, but do it is very easy to make people whole broken defense, the damage is very high insulting very strong: drawing.

Drawing is most afraid of someone's nitpicking: a distortion here, an error there, a distortion of the whole picture.

Recently in dealing with such a scenario , using Java's Graphics2D class , drawing the business needs of the graphic template , and then in the specific process to fill the data , and will be stored in the graphic , the logic is not complex , because it involves the ToC and ToB end of the two sides of the interaction , it must be used to carve a bit of attitude .

Second, the font installation

When drawing specific graphics, you need to deal with local fonts first and use the fonts provided by the designer before you can replicate the desired effect on the picture; after installing the relevant font packages, use Java to read and verify them before using them directly.

public class Typeface {
    public static void main(String[] args) {
        List<String> fontNames = new <>();
        Font[] fonts = ().getAllFonts();
        for (Font font : fonts){
            (());
        }
        (::println);
    }
}

III. Graphics

In cartography, there are some simple graphic styles involved, such as lines, rectangles, arcs and so on, which can be used with theGraphics2Dsyntax is generated directly, the following program creates a 500x500 image, then draws some simple graphic styles in it, and finally saves it locally.

public class DrawDraft {
  public static void main(String[] args) throws Exception {
    // 1、Creating Picture Drawings
    BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = ();
    (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    (RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // 2、Fill background color
    ();
    (0, 0, 500, 500);

    // 3、Drawing Lines
    (new BasicStroke(3));
    ();
    (50, 50, 280, 50);
    ();
    (50, 50, 165, 200);
    ();
    (280, 50, 165, 200);

    // 4、plot
    (new BasicStroke(2));
    ();
    (200, 200, 80, 50);// rectangles

    ();
    (280, 280, 100, 100, 0, 180);//circular arc
    (300, 300, 100, 50, 0, -270);//circular arc弧度

    ();
    (350, 350, 100, 100, 0, 180);//circular arc
    (350, 350, 100, 100, 0, -270);//Fill the circle three-quarters of the way

    // 5、Write to Pictures
    (image, "png", new File("src/main/"));

    ();
    ();
  }
}

IV. Drawing text

In the regular business scenarios, generally the first drawing template graphics, and then fill in the template of the graphic data, you can also directly use the template file provided by the designer, so that you can avoid the data to fill in the layout of the problem, if there is a large amount of dynamic data content, you can use the template engine, which in the previous content has to write a similar case.

The following case, the use of the above template, the text on this template to add, draw the text is mainly some dynamic alignment and typography and other issues, the final charting in effect when you add the signature can be.

import ;
import ;
import .Graphics2D; import ;Graphics2D
import ;
import ;Graphics2D; import ;Graphics2D; import ;Graphics2D
import ;Graphics2D; import ;Graphics2D; import ;Graphics2D
import ;

public class DrawImage {

  public static void main(String[] args) throws Exception {
    // 1. Basic styles
    
    Font yhBoldFont = new Font("Microsoft Yahei UI Bold", , 25); Font tailFont = new Font("Microsoft Yahei UI Bold", , 25); // 1.
    Font tailFont = new Font("Microsoft Yahei UI Bold", , 12); Font

    // 2. Drawing based on the base image
    BufferedImage backImg = (new File("src/main/")); int canvasWidth = (); int canvasWidth = (); canvasWidth = ()
    int canvasWidth = (); int canvasHeight = ()
    int canvasHeight = ();

    // 3. Create the brush
    Graphics2D graphics = ();
    (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    (RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // 4. Draw the centered title
    (yhBoldFont).
    ();
    String title = "2D Drawing";
    int titleWidth = ().stringWidth(title);
    int titleX = canvasWidth/2-titleWidth/2;
    int titleY = 50; (title, titleX, titleY)
    (title, titleX, titleY);

    // 5. Draw long text, left-justified and line-breaking.
    (yhFont).
    ();
    String blackText = "\u3000 organizations need to revisit the core value proposition of their projects to better align goals and co-create with stakeholders." ;
    String[] textWord = ("") ;
    // Maximum text width and line height
    int textMaxWidth = 200; ; int textLineHeight = 18
    int textLineHeight = 18;; // Text MaxWidth and LineHeight.
    // Text character output start coordinates
    int textWordX = 20;; int textWordY = 350; // Text character output start coordinates.
    int textWordY = 350; // Control the length of a single line of text by calculating
    // Calculate the length of a single line of text
    StringBuilder textLine = new StringBuilder(); for (String word : textWord); // control the length of a single line of text by calculating it.
    for (String word : textWord){
      (word, textWordX, textWordY); if (().
      if (().stringWidth(textLine + word) <= textMaxWidth) {
        // without line breaks, record a single line, move the x-coordinate
        (word);
        textWordX = textWordX + ().stringWidth(word);
      } else {
        // If a line break is needed, reset the current line of text and move the X and Y coordinates.
        (0); textWordX = 20 ; textWordX + ().
        textWordX = 20 ;
        textWordY = textWordY + textLineHeight; }
      }
    }

    // 6. Draw the short text, right-aligned
    (tailFont).
    ();
    String author = "Charting party: can't sleep during the day";
    int authorWidth = ().stringWidth(author);
    (author, authorWidth, 180); String drawDate = "Drawing party: sleepless during the day"; int authorWidth = ().
    String drawDate = "Time: August 28, 2024";
    int drawDateWidth = ().stringWidth(drawDate);
    (drawDate, drawDateWidth, 200);

    // 7. Add the watermark image
    BufferedImage watermarkImg = (new File("src/main/")); (new File("src/main/")); // Add a watermark image.
    (watermarkImg, 350, 120, 120,120,120, null);

    // 8. Write to the image
    (backImg, "png", new File("src/main/"));;
    ().
    ().
    ();
  }
}

Eggs: hereblackTextText is to let the big model randomly written, just punch this output and flavor, we guess from which domestic big model, (water release prompt word: domestic). Finally, about the file management will not go into details, which file server is convenient, it is stored anywhere.

V. Source code reference

Documentation repository:
/cicadasmile/butte-java-note

Source code repository:
/cicadasmile/butte-spring-parent