Graphical interface of Java Engineering Development (first lesson)

Source: Internet
Author: User
Tags in degrees

Let's start with the code:

1  Packagea case of a graphical small application;2 ImportJavax.swing.JApplet;3 ImportJava.awt.Graphics;4 5  Public classDraw a graphicextendsjapplet{6      Public voidPaint (Graphics canvas) {7Canvas.drawoval (100,50,200,200);8Canvas.filloval (155, 100, 10, 20);9Canvas.filloval (230,100,10,20);TenCanvas.drawarc (150, 160, 100, 50, 180, 180); One     } A}

First it uses the class JApplet in the Swing Library (package) just like all the other small applications.

At the same time, it also uses the class graphics from the AWT library. In addition to the classes in the Swing library, small applications often use classes from the AWT library.

Next Statement line PUBILC class HappyFace extends JApplet

Starting with the class definition for the applet, it is named HappyFace. The word extends JApplet indicates that we are defining a small program (inheritance) instead of other types of classes.

This small application contains a method--paint, which defines the following statement to begin with:

public void paint (Graphics canvas)

The Paint method specifies what graphics are drawn in this small application. The Paint method is called automatically when the applet is run.

Now let's explain:

Canvas.drawoval (100,50,200,200);

It is used to form the face graphics. The first two digits indicate where the circle is drawn on the screen. The second two digits are the lengths of the long and short axes used to draw the ellipse. The units of these numbers are defined in pixels.

Canvas.filloval (155, N, ten,);
Canvas.filloval (230,100,10,20);
These two words are used to draw the eyes. Fillover means that a filled ellipse is called.
The last sentence is of course used to draw the mouth.

Here is a brief explanation of the basics of drawing ellipses and circles:

Canvas.drawoval (100,50,90,50) is drawn, the first two numbers are coordinates, and the last two digits are the short and long half axes. That is, DrawOval and Fillover two methods.

Draw arc: Canvas.drawarc (150,160,100,50,180,180);

The first two parameters are given a position of the invisible rectangle. The upper-left corner of the rectangle is on the point (150,160). The next two parameters specify the size of the rectangle: the rectangle has a width of 100 and a height of 50. Then inside the invisible rectangle, imagine an invisible ellipse with the same width and height as the invisible rectangle. The latter two parameters specify the visible portion of the invisible ellipse.

The DRAWARC second parameter specifies the starting angle, in degrees. The last parameter specifies how many degrees the arc of an ellipse will continue to be visible. The endpoint at the right end of the elliptical horizon is 0 degrees. As you move the ellipse edge counterclockwise, the degrees increase gradually.

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.