51CTO第一篇博文:001绘制复杂图形并输出图片-Java实现

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
package com.inspur.graphics;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class DrawGraphics {

BufferedImage buffImg = null ;

void createImage(String fileLocation) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(buffImg);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 绘制Board面板
*/
private void drawPanel() {
// 1.1.设置基本的属性
int imgWidth = 985 ;
int imgHeight = 0 ; // 根据插框排列的层数确定所需高度

// 1.2.插框层数和所需面板高度数值的计算
int rackSize = 6 ; // 插框数量
int rackSizeInLayer = 3 ; // 每层插框数量

int rackLayerSize = 0 ; // 插框排列层数
if (rackSize % rackSizeInLayer == 0) {
rackLayerSize = rackSize / rackSizeInLayer ;
} else {
rackLayerSize = rackSize / rackSizeInLayer + 1 ;
}
imgHeight = rackLayerSize * 181 + (rackLayerSize + 1) * 40 ;

// 1.3.设置画布参数,绘制画布
buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_BGR) ;
Graphics2D grahpics2D = (Graphics2D) buffImg.getGraphics() ;
// 消除线条锯齿
grahpics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// font set
Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 10) ;
grahpics2D.setFont(font) ;
// backgroundColor
grahpics2D.setColor(new Color(238, 238, 238)) ;
// 绘制Board面板
grahpics2D.fillRect(0, 0, imgWidth, imgHeight) ;

// 2.绘制插框
for (int i = 0; i < rackLayerSize; i++) {
int rackYOffset = 40 ;
rackYOffset += i * (40 + 181) ;
if (i == i - 1) {
rackSizeInLayer = rackSize % 3 ;
}
for (int j = 0; j < rackSizeInLayer; j++) {
int rackXOffSet = 32 ;
rackXOffSet += j * (36 + 283) ;
drawRack(grahpics2D, rackXOffSet, rackYOffset);
}
}

// 4.释放此图形的上下文并释放它所使用的所有系统资源,调用函数输出图片
grahpics2D.dispose();
createImage("D:\\works\\graphics\\test.png");
}

/**
* 在面板上添加插框
* @param grahpics2D
* @param rackXOffset 插框相对于Panel横向偏移量
* @param rackYOffset 插框相对于Panel竖向偏移量
*/
private void drawRack(Graphics2D grahpics2D, int rackXOffset, int rackYOffset) {
String rackUrl = "D:\\works\\graphics\\rack.png" ;
// 1.1.在画布上绘制插框
BufferedImage rackImg = null ;
try {
rackImg = ImageIO.read(new File(rackUrl));
} catch (IOException e) {
e.printStackTrace();
}
grahpics2D.drawImage(rackImg, rackXOffset, rackYOffset, null);
// 1.2.Rack title
grahpics2D.setColor(Color.BLACK);
grahpics2D.drawString("SUBRACK-0", rackXOffset + rackImg.getWidth() / 2 - 36, rackYOffset + 28) ;
// 2.为插框绘制插槽
drawSlot(grahpics2D, 28, rackXOffset, rackYOffset);
}

/**
* 在插框上绘制插槽
* @param grahpics2D
* @param slotNum 插框插槽数量
* @param baseX 插框插槽横向偏移基量(插槽跟着插框走)
* @param baseY 插框插槽竖向偏移基量
*/
private void drawSlot(Graphics2D grahpics2D, int slotNum, int baseX, int baseY) {
String slotContainerBgUrl = "D:\\works\\graphics\\slot_container_bg.png" ;
// 1.基础数据确定
int rackLeftWidth = 16 ; // 根据使用的rackImg确定的值
int slotWidth = 18 ; // 卡槽宽度,根据示例图确定的数值
int slotHeight = 48 ; // 卡槽高度,根据示例图确定的数值

int topSlotContainerOffset = 32 ; // 上部插槽编号容器相对于插框顶部的偏移量
int bottomSlotContainerOffset = 144 ; // 下部插槽编号容器相对于插框顶部的偏移量

int topSlotYOffset = 48 ; // 上部插槽相对于插框顶部的偏移量
int bottomSlotYOffset = 96 ; // 下部插槽相对于插框顶部的偏移量
// 2.绘制插槽编号容器
BufferedImage slotContainerBgImg = null ;
try {
slotContainerBgImg = ImageIO.read(new File(slotContainerBgUrl)) ;
} catch (IOException e) {
e.printStackTrace();
}
// 2.1.top slot conatiner
grahpics2D.drawImage(slotContainerBgImg, baseX + rackLeftWidth, baseY + topSlotContainerOffset, null) ;
// 2.2.bottom slot conatiner
grahpics2D.drawImage(slotContainerBgImg, baseX + rackLeftWidth, baseY + bottomSlotContainerOffset, null) ;

// 3.绘制插槽(分两层)
int layerSlotNum = slotNum / 2 ;
if (slotNum % 2 != 0) { // 插槽数量为奇数
layerSlotNum += 1 ;
}
// 3.1.solt-bottom
for (int i = 0; i < layerSlotNum; i++) {
// 绘制插槽矩形
grahpics2D.setColor(Color.LIGHT_GRAY);
int slotXOffset = rackLeftWidth ; // 插槽相对于插框左侧的偏移量
slotXOffset += i * slotWidth ;
grahpics2D.drawRect(baseX + slotXOffset, baseY + bottomSlotYOffset, slotWidth, slotHeight) ;
// 为插槽插卡(显示名称 + 颜色)
if (i != 4 && i != 5) {
insertCard(grahpics2D, baseX + slotXOffset, baseY + bottomSlotYOffset, i);
}
drawSlotNo(grahpics2D, i, baseX + slotXOffset, baseY + bottomSlotContainerOffset + 12); // 12为卡槽编号在其卡槽中的y偏移量
}
// 3.2.solt-top
for (int i = layerSlotNum; i < slotNum; i++) {
// 绘制插槽矩形
grahpics2D.setColor(Color.LIGHT_GRAY);
int slotXOffset = rackLeftWidth ;
slotXOffset += (i - layerSlotNum) * slotWidth ;
grahpics2D.drawRect(baseX + slotXOffset, baseY + topSlotYOffset, slotWidth, slotHeight) ;
// 为插槽插卡(颜色+显示名称)
if (i != 16 && i != 17) {
insertCard(grahpics2D, baseX + slotXOffset, baseY + topSlotYOffset, i);
}
drawSlotNo(grahpics2D, i, baseX + slotXOffset, baseY + topSlotContainerOffset + 12); // 12为卡槽编号在其卡槽中的y偏移量
}
}

/**
* 在卡槽容器内绘制卡槽编号
* @param grahpics2D
* @param i 第i个卡槽编号(0开始)
* @param slotXOffset 卡槽相对于其所在插框左侧偏移量
* @param slotYOffset 卡槽相对于其所在插框顶部偏移量
*/
private void drawSlotNo(Graphics2D grahpics2D, int i, int slotXOffset, int slotYOffset) {
String code = null ;
// 在slot bottom container下写插槽编码
grahpics2D.setColor(Color.BLACK);
code = i + "" ;
if (i < 10) {
code = "0" + i ;
}
grahpics2D.drawString(code, slotXOffset + 3, slotYOffset);
}

/**
* 向插槽中插卡的模拟实现
* @param grahpics2D 画布实例
* @param baseX 基础X坐标
* @param baseY 基础Y坐标
* @param slotIndex 卡槽索引(第几个由0开始)
*/
private void insertCard(Graphics2D grahpics2D, int baseX, int baseY, int slotIndex) {
// TODO:在该方法内模拟取出对应的Card
baseX += 2 ;// 输出图片效果调整
baseY += 2 ;
int cardWidth = 17 ; // 板卡宽度(稍微比卡槽小)
int cardHeight = 46 ; // 板卡高度(稍微比插槽小)
int fontOffset = 5 ; // 板卡上文字正常情况
if (slotIndex == 24 || slotIndex == 26) { // temp:两卡在一插槽内
cardWidth = cardWidth * 2 + 1 ;
fontOffset = cardWidth / 2 - 5 / 2 ; // 5 字体宽度
}
if (slotIndex != 25 && slotIndex != 27) {
String cardSpecNO = "SPUA" ;
if (baseY < 95) { // 95 temp
if (slotIndex == 24 || slotIndex == 26) {
grahpics2D.setColor(Color.WHITE) ;
} else {
grahpics2D.setColor(Color.GREEN);
}
} else {
grahpics2D.setColor(Color.PINK);
cardSpecNO = "SPUB" ;
}
grahpics2D.fillRect(baseX, baseY, cardWidth, cardHeight) ;

grahpics2D.setColor(Color.BLACK);
int y = 0 ;
for (int j = 0; j < cardSpecNO.length(); j++) {
y = (j + 1) * 11 ;
String outChar = cardSpecNO.substring(j, j + 1) ;
grahpics2D.drawString(outChar, (baseX - 2) + fontOffset, baseY + y); // baseX - 2为输出图片效果需要
}
}
}

public static void main(String[] args) {
DrawGraphics dg = new DrawGraphics() ;
dg.drawPanel() ;
System.out.println("ok");
}

}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java

“51CTO第一篇博文:001绘制复杂图形并输出图片-Java实现” 的相关文章