圖片自己去找!
public class Picture {
private BufferedImageimage;
? ? private int CANVAS_W =1136; //底圖寬度
? ? private int CANVAS_H =640; //底圖寬度
? ? private int BOX_X =80; //信息框X坐標(biāo)
? ? private int BOX_Y =145; /信息框Y坐標(biāo)
//設(shè)置圖片素材地址
? ? public static String$bg_path ="./static/picture/bg.png"; //背景圖片路徑
? ? public static String$player_default_box_path ="./static/picture/empty_box.png"; //玩家默認(rèn)信息框圖路徑
? ? /**
* 獲取戰(zhàn)績背景圖? ---素材轉(zhuǎn)化成圖片
*/
? ? public void setMaterial(){
//獲取底圖
? ? ? ? BufferedImage bufferedImage = getImage($bg_path);
? ? ? ? BufferedImage bufferedImagePlayer = getImage($player_default_box_path);
? ? ? ? //創(chuàng)建畫布
? ? ? ? image =new BufferedImage(CANVAS_W, CANVAS_H, BufferedImage.TYPE_INT_RGB);
? ? ? ? //開啟畫圖
? ? ? ? Graphics2D graphics =image.createGraphics();
? ? ? ? graphics.drawImage(bufferedImage,0,0,bufferedImage.getWidth(),bufferedImage.getHeight(),null);
? ? ? ? graphics.drawImage(bufferedImagePlayer,BOX_X,BOX_Y,bufferedImagePlayer.getWidth(),bufferedImagePlayer.getHeight(),null);
? ? }
/**
* 獲取圖片資源
*/
? ? public BufferedImagegetImage(String path){
try {
//讀入文件
? ? ? ? ? ? URL url = ClassLoader.getSystemResource(path);
? ? ? ? ? ? File file =new File(url.getPath());
? ? ? ? ? ? //構(gòu)造Image對象
? ? ? ? ? ? BufferedImage image = ImageIO.read(file);
? ? ? ? ? ? return image;
? ? ? ? }catch (IOException e) {
throw new RuntimeException("獲取圖片資源異常",e);
? ? ? ? }
}
/**
* //生成圖片文件
? ? * @param fileLocation
? ? */
? ? 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){//關(guān)閉輸出流
? ? ? ? ? ? ? ? ? ? try {
bos.close();
? ? ? ? ? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
}
}
}
}
/**
* 圖片合成
*/
? ? public void graphicsGeneration(){
setMaterial();
? ? ? ? createImage("c:\\haha.jpg");
? ? }
public static void main(String[] args) {
Picture cg =new Picture();
? ? ? ? try {
cg.graphicsGeneration();
? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? }
}
}