자바 그림 과 텍스트 에 따라 사용자 정의 그림 생 성
4326 단어 자바
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ChartGraphics {
private BufferedImage image;
private int imageWidth = 300; //
private int imageHeight = 500; //
//
@SuppressWarnings("restriction")
public void createImage(String fileLocation) {
BufferedOutputStream bos = null;
if(image != null){
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bos!=null){//
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void graphicsGeneration(String name, String id, String classname, String imgurl) {
int H_title = 30; //
int H_mainPic = 200; //
int H_tip = 60; //
int H_btn = 25; //
int tip_2_top = (H_title+H_mainPic);
int btns_2_top = tip_2_top+H_tip+20;
int btn1_2_top = btns_2_top+10;
int btn2_2_top = btn1_2_top+H_btn;
int shops_2_top = btn2_2_top+H_btn+20;
int W_btn = 280; //
image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
//
Graphics2D main = image.createGraphics();
main.setColor(Color.white);
main.fillRect(0, 0, imageWidth, imageHeight);
//***********************
Graphics title = image.createGraphics();
//
title.setColor(new Color(143, 0, 0));
//
title.fillRect(0, 0, imageWidth, H_title);
// , ,
title.setColor(Color.white);
//
Font titleFont = new Font(" ", Font.BOLD, 14);
title.setFont(titleFont);
title.drawString("my head", 100, (H_title)/2+5);
//***********************
Graphics mainPic = image.getGraphics();
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {}
if(bimg!=null){
mainPic.drawImage(bimg, 0, H_title, imageWidth, H_mainPic, null);
mainPic.dispose();
}
//***********************
Graphics2D tip = image.createGraphics();
//
tip.setColor(new Color(255, 120, 89));
//
tip.fillRect(0, tip_2_top, imageWidth, H_tip);
// , ,
tip.setColor(Color.white);
//
Font tipFont = new Font(" ", Font.PLAIN, 14);
tip.setFont(tipFont);
tip.drawString(" , 1 ", 60, tip_2_top+(H_tip)/2-10);
tip.drawString(" ", 100, tip_2_top+(H_tip)/2+10);
//***********************
// , ,
tip.setColor(Color.black);
tip.drawString(" :", 20, btns_2_top);
tip.drawString(" :", 20, shops_2_top);
//***********************
Font btnFont = new Font(" ", Font.BOLD, 14);
Graphics2D btn1 = image.createGraphics();
btn1.setColor(new Color(41,192 , 50));//#29C65A
btn1.fillRect(10, btn1_2_top, W_btn, H_btn);
btn1.setColor(Color.BLACK);
btn1.drawRect(10, btn1_2_top, W_btn, H_btn);
//btn1
btn1.setColor(Color.white);
btn1.setFont(btnFont);
btn1.drawString(" ", 120, btn1_2_top+(H_btn/2)+5);
Graphics2D btn2 = image.createGraphics();
btn2.setColor(new Color(141,120 , 22));//#29C65A
btn2.fillRect(10, btn2_2_top, W_btn, H_btn);
btn2.setColor(Color.BLACK);
btn2.drawRect(10, btn2_2_top, W_btn, H_btn);
//btn2
btn2.setColor(Color.white);
btn2.setFont(btnFont);
btn2.drawString(" ", 120, btn2_2_top+(H_btn/2)+5);
createImage("c:\\hehe.jpg");
}
public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
cg.graphicsGeneration("ewew", "1", "12", "E:\\work_folder\\picture\\big_pic\\1.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
다음으로 전송:https://www.cnblogs.com/qianna-daisy/p/4572221.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.