Use Java to display the picture builder

Source: Internet
Author: User
Tags abstract range

One, this picture generator has the following functional characteristics:

1, you can set the width of the picture, height, frame colors, background color;

2, you can set the picture font size, name, color;

3, you can set the format of the output picture, such as JPEG, GIF and so on;

4, can store the picture to a file or store to an output stream;

5, can add a number of interference lines for the picture (in the generation of random code pictures available this feature);

6, printed on the picture of the text to support the automatic line wrapping;

In addition, this picture builder also uses the template method pattern.

Two, the following list of related source code

1, the abstract class Abstractimagecreator source code

/** This code in http://www.bt285.cn http://www.5a520.cn has been used * *

Public abstract class Abstractimagecreator {private static Random rnd = new Random (new Date (). GetTime ());   
        
    Picture width private int width = 200;   
        
    Picture height private int height = 80;   
        
    Outer frame color private color rectcolor;   
        
    Background color private color bgcolor;   
        
    Number of interference lines private int linenum = 0;   
        
    Picture format private String FormatName = "JPEG";   
        
    Font Color private Color fontcolor = new Color (0, 0, 0);   
        
    Font name private String fontname = "Song Body";   
        
   
    Font size private int fontsize = 15; ##### here the Get, set method for omitting member faces #####/** * Draw interference Line/private void Drawrandomline (Graphics g Raph) {for (int i=0;i<linenum;i++) {//Line color Graph.setcolor (100,   
                
            155)); line coordinate value int x1 = Rnd.nextint (wIdth);   
                
            int y1 = rnd.nextint (height);   
            int x2 = rnd.nextint (width);   
                
            int y2 = rnd.nextint (height);    
        Draw lines Graph.drawline (x1, y1, x2, y2);   
        }/** * Randomly acquired color object/private color getrandomcolor (int base, int range) {   
            
        if (base + range) > 255) range = 255-base;   
        int red = base + rnd.nextint (range);   
        int green = base + rnd.nextint (range);   
            
        int blue = base + rnd.nextint (range);   
    return new Color (red, green, blue);   
        The template method mode public void DrawImage (String text) throws is applied within the method ioexception{   
            
        BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);   
        if (Rectcolor = = null) Rectcolor = new Color (0, 0, 0); if (bgcolor = = null) bgcolor = new Color (240, 251, 200);    
            
        Gets the canvas Graphics graph = image.getgraphics ();    
        Draw rectangular graph.setcolor (bgcolor);   
            
        Graph.fillrect (0, 0, width, height);    
        Outer frame Graph.setcolor (rectcolor);   
            
        Graph.drawrect (0, 0, width-1, height-1);    
            
        Draw Disturbing line drawrandomline (graph);    
            
        Draw string DrawString (graph, text);    
            
        Implementation of Graph.dispose ();    
    Output picture results saveimage (image);   
        
    protected abstract void drawstring (Graphics graph, String text);   
        
protected abstract void SaveImage (BufferedImage image) throws IOException; }

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.