SpringBoot输出中国五星红旗java代码

package com.yanek.controller;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.IOException;
@Controller
public class FiveStarFlagController {
    @RequestMapping("/fivestar")
    public void getFiveStarFlag(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires", 0);
        int width =600;
        int height = width/3*2;
        double maxR = 0.15, minR = 0.05;
        double maxX = 0.50, maxY = 0.50;
        double[] minX = {0.75, 0.85, 0.85, 0.75};
        double[] minY = {0.35, 0.45, 0.60, 0.70};
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        java.awt.Graphics2D g=(java.awt.Graphics2D)image.createGraphics();
        g.clearRect(0, 0,width,height);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //画棋面
        g.setColor(java.awt.Color.red);
        g.fillRect(0, 0, width, height);
        //g.fillRect(50, 50, width, height);
        g.setColor(java.awt.Color.yellow);
        //画大五角星
        double ox = height*maxX, oy = height*maxY;
        g.fill(createPentacle(ox,oy,height*maxR,-Math.PI/2));
        //画小五角星
        for(int idx =0;idx < 4;idx ++){
             double sx = minX[idx]*height, sy = minY[idx]*height;
             double theta = Math.atan2(oy-sy,ox-sx);
             g.fill(createPentacle(sx,sy,height*minR,theta));
        }
        g.dispose();
        ServletOutputStream outStream = response.getOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);
        encoder.encode(image);
        outStream.close();
    }

    public static java.awt.Shape createPentacle(double sx, double sy, double radius,double theta) {
        final double arc = Math.PI / 5;
        final double rad = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10);
        GeneralPath path = new GeneralPath();
        path.moveTo(1, 0);
        for (int idx = 0; idx < 5; idx++) {
            path.lineTo(rad * Math.cos((1+2*idx)*arc),rad*Math.sin((1+2* idx)*arc));
            path.lineTo(Math.cos(2*(idx+1)*arc),Math.sin(2 *(idx + 1)*arc));
        }
        path.closePath();
        AffineTransform atf = AffineTransform.getScaleInstance(radius, radius);
        atf.translate(sx/radius, sy/radius);
        atf.rotate(theta);
        return atf.createTransformedShape(path);
    }



}

访问:http://192.168.10.205:9999/fivestar 效果

 

 

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值