• 【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

  • 相关阅读:
    【Xamarin挖墙脚系列:IOS-关于手机支持的屏幕方向】
    【Xamarin挖墙脚系列:Xamarin 上台讲述PPT呵呵呵】
    Java文件下载
    javaWeb学习总结(2)- http协议
    javaWeb学习总结(1)- JavaWeb开发入门
    java中几种获取项目路径方式
    XSS研究1-来自外部的XSS攻击
    Session Cookie的HttpOnly和secure属性
    关于Cookie安全性设置的那些事
    Hibernate 所有缓存机制详解
  • 原文地址:https://www.cnblogs.com/heyang78/p/15858319.html
Copyright © 2020-2023  润新知