java--Digital Image processing (Java graphics and its API introduction)

Source: Internet
Author: User
Tags dashed line

1. Create a Graphics object
BufferedImage bi = new BufferedImage (120,120, BUFFEREDIMAGE.TYPE_INT_ARGB);
Graphics2D g2d = Bi.creategraphics ();
2. Control graphics Edge Anti-aliasing
Graphics2D. Setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on);
3. Segment/polyline

Two-point draw segment
G2d.drawline (50,50,200,50);
Multipoint draw line Point (50, 100), point (100, 130), point (150, 70), point (200, 100)
int[] xpoints = new int[]{50,100,150,200};
int[] ypoints = new int[]{100,130,70,100};
int npoints=4;
G2d.drawpolyline (xpoints,ypoints,npoints);
G2d.dispose ();
Two-point draw segment (set line width to 5px): Point (50, 150), point (200, 150)
Basicstroke BS1 = new Basicstroke (5);
G2d.setstroke (BS1);
G2d.drawline (50,150,200,150);
Draw dashed line: divides the dashed line into several segments (both the real and the blank segments are considered to be a paragraph), and the real and blank segments are plotted alternately,
The length of each segment drawn (including real and blank segments) is taken from the dash dashed pattern array (from the first
The element begins to loop the value), the following array indicates the length of each segment: 5px, 10px, 5px, 10px, ...
Float[] Dash = new float[]{5,10};
Basicstroke bs2=new Basicstroke (1,basicstroke.cap_square,basicstroke.join_miter,10.0f,dash,0.0f);
G2d.setstroke (BS2);
G2d.drawline (50,200,200,200);

G2d.dispose ();

4. Rectangle

1. Draw a rectangle: starting point (30, 20), width 80, height 100
G2d.drawrect (30,20,80,100);

2. Fills a rectangle
G2d.fillrect (30,20,80,100);

3. Draw a rounded rectangle: starting point (30, 150), width 80, height 100, fillet width 30, fillet height 30
G2d.drawroundrect (30,150,80,100,30,30);

4. Draw a polygon (close link): Point (140, 150), point (180, 250), point (220, 200)
int[] xpoints = new int[]{140,180,220};
int[] ypoints = new int[]{150,250,200};
int npoints = 3;
G2d.drawpolygon (xpoints,ypoints,npoints);

G2d.dispose ();

5, ARC, fan

1. Draw an arc: the upper-left corner of the ellipse's bounding rectangle has coordinates (0, 0), Width 100, height 100,
ARC has a starting angle of 0 degrees, the number of angles to be drawn is-90 degrees,
Ellipse right horizontal line is 0 degrees, counterclockwise is positive angle, clockwise is negative angle
G2d.drawarc (0,0,100,100,0,-90);
2. Draw a circle: the upper-left corner coordinates of the circle's bounding rectangle are (120, 20) and the width height is 100
G2d.drawarc (120,20,100,100,0,360);
3. Fills a fan
G2d.fillarc (80, 150, 100, 100, 90, 270);

G2d.dispose ();

6. Ellipse

1. Draw a circle: the upper-left corner coordinates of the circle's bounding rectangle are (0, 0) and the width height is 100
G2d.drawoval (0,0,100,100);
2. Fills an ellipse
G2d.filloval (120, 100, 100, 150);
G2d.dispose ();

7. Pictures

String filepath = "c:/users/lenovo/desktop/demo.jpg";
Image image = Toolkit.getdefaulttoolkit (). GetImage (filepath);
G2d.drawimage (Image,50,50,image.getwidth (this), Image.getheight (this), this);

G2d.dispose ();

8. Text

Set font style, null means use default font, Font.plain is Normal style, size is 25px
G2d.setfont (New Font (null,font.plain,25));

Draws text, where the coordinate parameter refers to the position of the lower-left corner of the text after it is drawn
Initial drawing requires initialization of fonts and may take a longer time
G2d.drawstring ("Hello world!", 20,60);
G2d.drawstring ("Hello, world!") ", 20,120);

9. Draw Taiji Graphics

private void Painttaiji (Graphics g) {
Mf.settitle ("Taiji");
Graphics2D g2d = (graphics2d) g.create ();

G2d.setrenderinghint (renderinghints.key_antialiasing,renderinghints.value_antialias_on);

Shape lefthalfcircle = new ellipse2d.double (10,10,300,300);
Shape righthalfcircle = new ellipse2d.double (10,10,300,300);
Shape innerCircle1 = new ellipse2d.double (85,10,150,150);
Shape innerCircle2 = new ellipse2d.double (85,160,150,150);

Shape Rectangel1 = new rectangle2d.double (160,10,150,300);
Shape rectangle2 = new rectangle2d.double (10,10,150,300);

Area left = new Area (lefthalfcircle);
Area right = new Area (righthalfcircle);

Area area1 = new Area (RECTANGEL1);
Area area2 = new Area (rectangle2);
Left semicircle White, right semicircle black
Left.subtract (AREA1);
Right.subtract (AREA2);

Area Inner1 = new Area (INNERCIRCLE1);
Area Inner2 = new Area (innerCircle2);

Left.add (Inner1);
Right.add (INNER2);

Right.subtract (Inner1);
Small white Circle
Shape minorwhitecircle = new ellipse2d.double (150,70,20,20);
Small black Circle
Shape minorbalckcircle = new ellipse2d.double (150,230,20,20);

//
G2d.setpaint (Color.White);
G2d.fill (left);
G2d.setpaint (Color.Black);
G2d.fill (right);

G2d.fill (minorwhitecircle);
G2d.setpaint (Color.White);
G2d.fill (minorbalckcircle);


}

10. Single Color fill

private void Singlecolor (Graphics g) {
Mf.settitle ("8. Brush color Settings");
Graphics2D g2d = (graphics2d) g.create ();

G2d.setpaint (Color.Blue);

G2d.fillrect (0,0,this.getwidth (), This.getheight ());
}

11. Setting the gradient color

private void Setgradient (Graphics g) {
Mf.settitle ("9. Gradient fill");
Graphics2D g2d = (graphics2d) g.create ();

Color Secondcolor = new color (99,153,255);
Fill horizontally
Gradientpaint hlinepaint = new Gradientpaint (0,0,color.black,this.getwidth (), 0,secondcolor);
Fill in vertical direction
Gradientpaint hlinepaint = new Gradientpaint (0,0,color.black,0,this.getheight (), secondcolor);
G2d.setpaint (Hlinepaint);

G2d.fillrect (0,0,this.getwidth (), This.getheight ());

}

12, circular radial gradient color fill

private void Radialgradientpaint (Graphics g) {
Mf.settitle ("10. Radial gradient color fill");
Graphics2D g2d = (graphics2d) g.create ();

float cx = this.getwidth ()/2;
float cy = this.getheight ()/2;//(Cx,cy is the center coordinate)
float radius = math.min (cx,cy);//Circle radius
float[] Fractions = new float[]{0.1f,0.5f,1.0f};//color Ramp keyframe Position
color[] colors = new color[]{color.red,color.green,color.black};//color array
Radialgradientpaint RGP = new Radialgradientpaint (cx,cy,radius,fractions,colors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
G2d.setpaint (RGP);

G2d.fillrect (0,0,this.getwidth (), This.getheight ());
}



java--Digital Image processing (Java graphics and its API introduction)

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.