Java basic drawing technology. The Java drawing coordinate system of tank wars (I.)

Source: Internet
Author: User

Introduction to coordinate system

Illustrates the Java coordinate system. The coordinate origin is in the upper-left corner, in pixels, which is the smallest display unit on the computer screen. In the Java coordinate system, the first is the x-coordinate, which indicates that the current position is horizontal, the origin of the coordinates is x pixels, and the second is the y-coordinate, which indicates that the current position is in the vertical direction, and y pixels from the origin of the coordinates.

      

The drawing also has to figure out a very important concept, pixel:

What the computer displays on the screen is made up of every pixel on the screen. For example, the resolution of a computer monitor is 800*600, which means that each line on the computer screen consists of 800 points, a total of 600 lines, and a total of 480 000 pixels across the computer screen. Now the computer can support a higher resolution, that is, more pixels can be displayed on the screen. Therefore, pixels are a density unit, and centimeters are unit of length, and they cannot be compared.

Case: Draw a circle first

Example code:

  

1  Packagecom.test;2 /*3 * Function: Java drawing principle4  * */5 Importjava.awt.*;6 Importjavax.swing.*;7  Public classDemo1extendsjframe{8 9     /**Ten      * @paramargs One      */ AMypanel MP =NULL; -      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub theDemo1 de =Newdemo1 (); -     } -  -      Publicdemo1 () +     { -MP =NewMypanel (); +          A          This. Add (MP); at          This. setSize (400, 300); -          This. Setdefaultcloseoperation (jframe.exit_on_close); -          This. setvisible (true); -     } - } -  in //Define a Mypanel (my own panel, suitable for drawing and implementing areas of the drawing) - classMypanelextendsJPanel to { +     //Paint method that covers JPanel -     //Graphics is an important class of drawing, you can interpret it as a brush the      Public voidPaint (Graphics g) *     { $         //1. Call the parent class function to complete the initialization taskPanax Notoginseng         //that's not a few words . -         Super. Paint (g); the         //Draw a circle first +G.drawoval (10,10,30,30); A          the          +     } -}
View Code

Principle of drawing (1)

The component class provides two important methods for drawing-related:

1. Paint (Graphics g) The appearance of the drawing component

2. Repaint () refresh the appearance of the component

When the component is first displayed on the screen, the program automatically calls the paint () method to draw the component

  

Principle of Drawing (2)

The paint () will be called in the following situations:

1. Minimize the window, then maximize

2. The size of the window has changed

3. Repaint function is called

Graphics class

The graphics class, which you can understand is a paintbrush, provides us with a variety of ways to draw graphics: "Strongly recommended reference to JDK help documentation"

1, Draw straight line drawLine (int x1,int y1,int x2,int y2);

2, Draw rectangle border drawrect (int x,int y,int width,int height);

3, Draw oval border DrawOval (int x,int y,int width,int height);

4. Fill rectangle fillRect (int x,int y,int width,int height);

5, filled oval filloval (int x,int y,int width,int height);

6, drawing film DrawImage (Image img.int x,int y,..);

7, Draw string drawstring (string Str,int x,int y);

8, set the brush font setFont (font font);

9, set the color of the brush setcolor (color c);

Java basic drawing technology. The Java drawing coordinate system of tank wars (I.)

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.