【java/graphics2d】类化的在笛卡尔坐标系绘制图片代码


先上图:

代码:

package graphics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

/**
 * PNG图片生成器
 * @author ufo
 * 2022年1月31日
 */
public class PngMaker {
    // 图片宽度
    private int width;
    
    // 图片高度
    private int height;
    
    // img对象
    private BufferedImage img;
    
    private Graphics2D g2d;
    
    // 垂直方向起始点
    private int yStart;
    
    public PngMaker(int width,int height,int yStart){
        this.width=width;
        this.height=height;
        this.img=new BufferedImage(this.width,this.height,BufferedImage.TYPE_INT_RGB);
        this.g2d=(Graphics2D)img.getGraphics();
        
        this.yStart=yStart;
        
        resetCoodinate();
        draw();
    }
    
    // 重置屏幕坐标系为笛卡尔坐标系
    private void resetCoodinate() {
        AffineTransform trans = new AffineTransform();
        trans.translate(0,this.height-this.yStart);
        trans.rotate(getRad(180.0),0,0);
        trans.scale(-1,1);        
        this.g2d.setTransform(trans);
    }
    
    // 绘制图案
    private void draw() {
        // 填充矩形
        g2d.setColor(new Color(135,206,235));
        g2d.fillRect(0, -this.yStart, this.width, this.height);
        
        // 绘直线
        int x1=200,y1=500;
        g2d.setColor(Color.white);
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.drawLine(0, 0, x1, y1);
        // 绘文字
        g2d.setFont(new Font("宋体",Font.BOLD,36));
        putString(g2d,"测试文字1",x1,y1);
                
        // 绘直线
        int x2=400,y2=300;
        g2d.setColor(Color.red);
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.drawLine(0, 0, x2, y2);
        // 绘文字
        g2d.setFont(new Font("宋体",Font.BOLD,24));
        putString(g2d,"测试文字2",x2,y2);
        
        // 绘直线
        int x3=800,y3=200;
        g2d.setColor(Color.yellow);
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.drawLine(0, 0, x3, y3);
        // 绘文字
        g2d.setFont(new Font("宋体",Font.BOLD,12));
        putString(g2d,"测试文字3",x3,y3);
                
        g2d.dispose();// g2d使命完成
    }
    
    // 写入图片
    public void write2File(String path) {
        try {
            ImageIO.write(img, "PNG", new FileOutputStream(path));
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    
    // 写文字
    private void putString(Graphics2D g2d,String text,int x,int y) {
        AffineTransform previousTrans = g2d.getTransform();
        
        AffineTransform newtrans = new AffineTransform();

        FontMetrics fm2=g2d.getFontMetrics();
        int textWidth=fm2.stringWidth(text);
        
        newtrans.translate(x-textWidth/2, (this.height-this.yStart)-y);
        
        g2d.setTransform(newtrans);
        g2d.drawString(text,0,0);
        
        g2d.setTransform(previousTrans);
    }
    
    // 传入度数,返回弧度
    private static double getRad(double degree) {
        return degree*Math.PI/180.0f;
    }
    
    public static void main(String[] args) {
        PngMaker pm=new PngMaker(1200,960,50);
        pm.write2File("c:\\hy\\3.png");
        System.out.println("图片输出完了");
    }
}

再接下来就可以绘制直方图了。

END

相关