• 【java/graphics2d】在笛卡尔坐标系绘制图片示例


    先上图:

    再上代码:

    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;
    
    public class PictureMaker2 {
        public static void main(String[] args) throws Exception{
            final int W=1000;
            final int H=800;
            BufferedImage img=new BufferedImage(W,H,BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d=(Graphics2D)img.getGraphics();
            
            // 进行坐标变换
            AffineTransform trans = new AffineTransform();
            trans.translate(0,H-50);
            trans.rotate(getRad(180.0),0,0);
            trans.scale(-1,1);        
            g2d.setTransform(trans);
            
            // 填充矩形
            g2d.setColor(Color.BLUE);
            g2d.fillRect(0, -50, W, H);
            
            // 绘直线
            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使命完成
            ImageIO.write(img, "PNG", new FileOutputStream("c:\\hy\\2.PNG"));
            System.out.println("图片做成");
        }
        
        // 传入度数,返回弧度
        private static double getRad(double degree) {
            return degree*3.1415927/180.0f;
        }
        
        private static 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, (800-50)-y);
            
            g2d.setTransform(newtrans);
            g2d.drawString(text,0,0);
            
            g2d.setTransform(previousTrans);
        }
    
    }

    接下来将其类化,再绘制直方图。

    END

  • 相关阅读:
    js判断空对象
    浅析css布局模型2
    Python 绘图
    我的第一个 Kaggle 比赛学习
    写代码 Log 也要认真点么?
    Python 线性回归(Linear Regression)
    Python
    Git
    算法4:插入排序和选择排序算法的比较
    《算法4》2.1
  • 原文地址:https://www.cnblogs.com/heyang78/p/15858319.html
Copyright © 2020-2023  润新知