package com.poly; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.GeneralPath; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Random; public class LoadImageApp { BufferedImage img; int segmentSize=5; int segmentWidth = 400; int rowSize=20; int rowHeight = 90; int titleHeight = 110; int noWidth = 100;//序号长度 int timeAddedWidth = 300;//时间增量长度 int middleWidth = segmentWidth*(segmentSize+1);//中间部分的长度 int briefWidth = 400;//摘要长度 int headDown = 65; // int padding = 20; // int halfPadding = padding/2; public LoadImageApp() { try { img = new BufferedImage(noWidth+timeAddedWidth+middleWidth+briefWidth,rowHeight*rowSize,BufferedImage.TYPE_INT_ARGB); } catch (Exception e) { e.printStackTrace(); } int imgWidth = img.getWidth(); int imgHeight = img.getHeight(); Color green = new Color(55,105,86); Color grey = new Color(221,221,221); Color deepGrey = new Color(221,227,231); Color moreGrey = new Color(242,247,250); Font font = new Font("微软雅黑", Font.BOLD, 36); Graphics2D g2 = img.createGraphics(); //背景 g2.setBackground(Color.WHITE); g2.clearRect(0,0,img.getWidth(),img.getHeight()); //head线 g2.setStroke(createStroke(6)); g2.setColor(green); g2.drawLine(0,titleHeight,imgWidth,titleHeight); //head填充 g2.setColor(deepGrey); g2.fillRect(0,0,imgWidth,titleHeight); g2.setStroke(createStroke(4)); // g2.setColor(grey); //偶数行填充深灰色 for(int i=1;i<rowSize;i++){ // g2.drawLine(halfPadding,(halfPadding+titleHeight)+rowHeight*i,imgWidth-halfPadding,(halfPadding+titleHeight)+rowHeight*i); //偶数行不同颜色 if(i%2==0){ g2.setColor(moreGrey); g2.fillRect(0,titleHeight+rowHeight*(i-1),imgWidth,rowHeight); } g2.setFont(font); g2.setColor(Color.BLACK); //序号 g2.drawString(String.valueOf(i),49,titleHeight+52+rowHeight*(i-1)); //时间增量 NumberFormat numberFormat = new DecimalFormat("0.000000000"); g2.drawString(numberFormat.format(0.000000001*i*10),140,titleHeight+52+rowHeight*(i-1)); for(int j=0;j<segmentSize+1;j++){ //箭头 drawAL(noWidth+timeAddedWidth+segmentWidth/2+(segmentWidth*i),titleHeight+72+rowHeight*(i-1),noWidth+timeAddedWidth+segmentWidth/2+(segmentWidth*(i+1)),titleHeight+72+rowHeight*(i-1),g2); } } //序号和时间增量后的白线 g2.setColor(Color.WHITE); g2.setStroke(createStroke(4)); g2.drawLine(noWidth+timeAddedWidth,0,noWidth+timeAddedWidth,imgHeight); g2.setColor(Color.BLACK); g2.setStroke(createStroke(4)); g2.setFont(font); g2.drawString("序号",40,headDown); g2.drawString("时间增量",170,headDown); for(int i=0;i<segmentSize+1;i++){ //headIP g2.setFont(font); g2.setColor(Color.BLACK); g2.drawString("192.169.0.1",noWidth+timeAddedWidth+100+segmentWidth*i,headDown); //竖线 g2.setColor(green); int x = noWidth+timeAddedWidth+(segmentWidth)/2+segmentWidth*i; g2.drawLine(x,titleHeight,x,imgHeight); } // } public Stroke createStroke(int width){ BasicStroke stroke = new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); return stroke; } private void drawAL(int sx, int sy, int ex, int ey, Graphics2D g2) { double H = 16; // 箭头高度 double L = 10; // 底边的一半 int x3 = 0; int y3 = 0; int x4 = 0; int y4 = 0; double awrad = Math.atan(L / H); // 箭头角度 double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度 double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len); double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len); double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点 double y_3 = ey - arrXY_1[1]; double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点 double y_4 = ey - arrXY_2[1]; Double X3 = new Double(x_3); x3 = X3.intValue(); Double Y3 = new Double(y_3); y3 = Y3.intValue(); Double X4 = new Double(x_4); x4 = X4.intValue(); Double Y4 = new Double(y_4); y4 = Y4.intValue(); //起始线 g2.setStroke(createStroke(4)); g2.setColor(Color.BLACK); g2.drawLine(sx, sy, ex, ey); //箭头 // g2.drawLine(ex, ey, x3, y3); // g2.drawLine(ex, ey, x4, y4); // 三角形箭头 GeneralPath triangle = new GeneralPath(); triangle.moveTo(ex, ey); triangle.lineTo(x3, y3); triangle.lineTo(x4, y4); triangle.closePath(); // 实心箭头 g2.fill(triangle); //非实心箭头 //g2.draw(triangle); } private static double[] rotateVec(int px, int py, double ang, boolean isChLen, double newLen) { double mathstr[] = new double[2]; // 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度 double vx = px * Math.cos(ang) - py * Math.sin(ang); double vy = px * Math.sin(ang) + py * Math.cos(ang); if (isChLen) { double d = Math.sqrt(vx * vx + vy * vy); vx = vx / d * newLen; vy = vy / d * newLen; mathstr[0] = vx; mathstr[1] = vy; } return mathstr; } public static void main(String[] args) throws IOException { LoadImageApp a = new LoadImageApp(); ImageIO.write(a.img,"png",new File("/home/Desktop/331.png")); } }