Java 2D API-2. Getting Started with Graphics

Source: Internet
Author: User

Java 2D API powerful and complex, but most of the time we just use Java.awt.Graphcis part of the function of the class. The following content will cover most common applications.

Graphics the methods in the class can be broadly divided into two categories:

    • Draw and fill method for drawing basic graphics, text, and images;
    • property setting method that controls the effect of drawing and padding. methods such as SetFont() and setcolor() belong to such methods.

Show the correspondence between these methods and images:

1. DrawString ()

Used to draw text:

g.DrawString ("Hello", 10, 10);

The parameters were:

    • String to be drawn
    • starting point x and the y coordinates

Import Java.applet.Applet;

Import Java.awt.Graphics;

Public class graphics_drawstring extends Applet {

@Override

Public void paint (Graphics g) {

g . DrawString ("Hello Graphics", page, +);

}

}

The output interface is as follows:

2. DrawImage

To draw an image:

Import Java.applet.Applet;

Import Java.awt.Graphics;

Import Java.awt.Image;

Import Java.awt.Toolkit;

Public class graphics_drawimage extends Applet {

@Override

Public void paint (Graphics g) {

Image img = Toolkit. Getdefaulttoolkit (). GetImage (this. GetClass (). GetResource ("Th.png"));

g . DrawImage (img, 0, 0, N, a, this);

}

}

Th.png I randomly selected a picture, the output interface is as follows:

3. DrawLine, DrawArc, DrawRect, DrawOval, DrawPolygon

These methods are used to draw different shapes.

Depending on the requirements, you can choose from the following recommendations Graphics method of:

    • whether to render the picture at its original size, or according to the specified Rectangle zoom picture;
    • The transparent area of the picture, whether it is filled or not.

2D Graphics each of the points is required (x, y) coordinates are determined, so the drawing and filling methods require location information. For example, draw a line:

Graphics.drawline (int x1, int y1, int x2, int y2);

among them, (x1, y1) is the beginning of the line, (x2, y2) is the end of the line.

Java 2D API-2. Getting Started with Graphics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.