JAVA---graphics processing

Source: Internet
Author: User

The Abstract Windowing Tool (AWT) provides API programming interfaces for graphical user interfaces, allowing Java to provide a better graphical user interface. AWT divides the graphics processing into two layers: one is to process the original graphics, to draw the interface directly in the form of a point line, and the other is to provide a large number of components for a customizable graphical user interface. The coordinate system in the Java programming language differs from the coordinate system in mathematics, the upper-left corner of the screen is (0,0), and the lower-right corner is the pixel that grows horizontally to the right and vertically down the screen.

Method:

1:public void Paint (Graphics g)

2:repaint:applet automatically calls the Paint method when the system is redrawn.

3:update method: public void Update (Graphics g) updates the container, sends a signal to repaint to flush the applet, and the default Update method clears the applets paint area and calls the paint method.

Graphics class

The graphics class is an abstract class for all graphics contexts, allowing applications to paint components on a variety of devices. Graphical objects encapsulate the state information of basic rendering operations supported by Java, including paint component objects, rendering area coordinates (coordinates), region (clip), color, font (font), paint mode, and so on. The graphics class provides methods for drawing various graphics, including lines, circles and ellipses, rectangles, polygons, images, and various fonts.

public abstract void ClipRect (int x,int y,int width,int height); Specifies the area division.

public abstract void DrawLine (int x1,int y1,int x2,int y2); Draw a line between (X1,y1) and (x2,y2) using the current color

public abstract void DrawOval (int x1,int y1,int width,int height); Draw an Ellipse

public abstract void Filloval (int x1,int y1,int width,int height); Draw a solid ellipse

public abstract void DrawPolygon (int[] xpoints,int[] ypoints,int npoints); Draw polygons defined by x and Y coordinates

public abstract void DrawRect (int x1,int y1,int width,int height); Draw a rectangle

public abstract void Filloval (int x,int y,int width,int height); Draw a solid rectangle

public abstract void Drawroundrect (int x1,int y1,int width,int height,int arcwidth,int archeight); Draw a rounded rectangle with the current color

public abstract void DrawString (string str,int x,int y), using the current font and color to draw the string str.

public abstract void SetColor (Color c); Sets the color of the graphics context.

public abstract void Setpaintmode (); Set drawing Mode

Public abstract Boolean drawImage (Image img,int X,int y,imageobserver observer); Draw a specific graph;

public abstract void SetFont (font font); Sets a specific font font; Public font (String name,int style,int size);

Eg:font f=new Font ("Timesname", font.bold+font.italic,12)

Example:

Import java.awt.*;

Import java.applet.*;

Import java.geom.*;

public class Simplegui extends applet{

Image Samimage;

public void init () {

Samimage=getimage (Getdocumentbase (), "sample.gif");

}

public void Paint (Graphics g) {

G.cliprect (50,50,180,180);

G.drawline (0,0,20,30);

g.DrawString ("Graphic display", 100,30);

Color C=new color (255,200,0);

G.setcolor (c);

Font f=new font ("Timesromn", font.bold+font.italic,24);

G.setfont (f);

g.DrawString ("Graphic display", 180,30);

G.drawline (20,20,100,50);

G.drawline (20,20,50,100);

G.drawrect (40,40,40,40);

G.fillrect (60,60,40,40);

G.setcolor (color.red);

3D Rectangle

G.draw3drect (80,80,40,40,true);

G.draw3drect (120,120,40,40,true);

G.filll3drect (120,210,40,40,true);

Elliptic

G.drawoval (150,150,30,40);

G.filloval (170,170,20,20);

G.setcolor (Color.Blue);

Rounded Rectangle

G.drawroundrect (180,180,40,40,20,20);

G.fillroundrect (200,200,40,40,20,20);

Polygon

int xc[]={242,260,254,297,242};

int yc[]={240,243,290,300,270};

G.drawpolygon (xc,yc,5);

Image

G.drawimage (Samimage,250,50,this);

}

}

Color class

The color class is used to encapsulate colors, and the simpler way of using color classes is to use the predefined colors provided by color, red: color.red, Orange: Color.orange, and more. You can also use the RGB color model to define, the so-called RGB is: Red,green,blue three colors for blending, three kinds of base color range is: 0~255;

Commonly used color RGB values and corresponding class-predefined parameters:

White: color.white; (255,255,255). Light grey: Color.lightgray; (192,192,192). Grey: Color.gray; (128,128,128). Dark grey: Color.drakgray; (64,64,64). Black: Color.drak; (0,0,0,).

Red: color.red; (255,0,0). Pink: Color.pink; (255,175,175). Orange: Color.orange; (255,200,0). Yellow: Color.yellow; (255,255,0). Green: Color.green; (0,255,0). Magenta: Color.magenta; (255,0,255). Dark blue: Color.cyan; (0,255,255). Blue: Color.Blue; (0,0,255).

Color can also construct a function, public Color (int red,int green,int blue,int Alpha); Alpha refers to transparency, 255 is completely opaque, can be displayed normally, and when the alpha component is 0 o'clock, the first three components do not work, and the value between 0~255 creates a layered effect with different colors.

eg

Import java.awt.*;

Import java.applet.*;

Import java.awt.geom.*;

public class Usecolor extends applet{

public void Paint (Graphics oldg) {

Graphics2D g= (graphics2d) oldg;

G.setcolor (Color.Blue);

G.fill (New Ellipse2d.float (50,50,150,150));

G.setcolor (New Color (255,0,0));

G.fill (New Ellipse2d.float (50,50,140,140));

G.setcolor (New Color (255,0,64));

G.fill (New Ellipse2d.float (50,50,130,130));

G.setcolor (New Color (155,0,0,128));

G.fill (New Ellipse2d.float (50,50,110,110));

G.setcolor (New Color (255,0,0,255));

G.fill (New Ellipse2d.float (50,50,90,90));

G.setcolor (New Color (255,200,0));

G.fill (New Ellipse2d.float (50,50,70,70));

}

}

JAVA---graphics processing

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.