直接上代碼
畫線的精髓在這 // 字體大小 int fontTitileSize = 12; // 橫線的行數(shù) int totalrow = 16; // 豎線的行數(shù) int totalcol = 10; // 圖片寬度 int imageWidth = 700; // 行高 int rowheight = 38; // 圖片高度 int imageHeight = totalrow*rowheight+41; // 起始高度 int startHeight = 70; // 起始寬度 int startWidth = 0; // 單元格寬度 int colwidth = 60;// 初始化畫筆 BufferedImage image = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);Graphics graphics = image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0,0, imageWidth, imageHeight); graphics.setColor(new Color(220,240,240)); int tmpWidth = 5; graphics.setColor(Color.black); graphics.drawLine(startWidth, startHeight, startWidth+10*colwidth, startHeight); //劃線是根據(jù)開始坐標(biāo)和結(jié)束坐標(biāo)兩點畫一條線的 。//縱坐標(biāo)不變畫的是一條橫線// 橫坐標(biāo)不變畫的是一條豎線,如下 graphics.drawLine(startWidth+10*colwidth, startHeight, startWidth+10*colwidth, startHeight+15*rowheight);
畫完線之后,如何寫上文本呢 //設(shè)置字體 Font font = new Font("微軟雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.drawString("文本內(nèi)容", 0, 40);// 什么意思呢,即 在(0,40)這個點的位置,寫文本
最后生成圖片調(diào)用 createImage(image, path);public static void createImage(BufferedImage image, String fileLocation) { FileOutputStream fos = null; BufferedOutputStream bos = null; fos = new FileOutputStream(fileLocation); bos = new BufferedOutputStream(fos);// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);// encoder.encode(image); ImageIO.write(image,"jpg",bos) }起初,需要用到
com.sum.image.codec.jpeg.JPEGImageEncoder這個類,但是這個是rt.jar包中的sun私有包,涉及到版權(quán)的問題,關(guān)鍵是一些大公司使用的是openjdk,這個openjdk簡單的理解就是開源精簡版,它是沒有sun這個包的,那么就會出現(xiàn)java.lang.NoclassDefFoundError這個錯誤,在openjdk下,建議用
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- 推薦3個畫圖神器 辦公圖片編輯軟件
- java做桌面應(yīng)用程序 javaee學(xué)習(xí)路線
- 圖像常見的格式推薦 常見的圖像格式有哪些
- 撓腳心漫畫撓癢癢
- java新手代碼大全 java怎么使用數(shù)組
- 碧璽名字原來是因為一位畫師?慈禧去世后還腳蹬碧璽蓮花!
- java編譯中文亂碼 java中文亂碼怎么解決
- 如何畫好拉丁舞的標(biāo)準(zhǔn)唇
- 眾人托舉窗簾接住墜樓男童 現(xiàn)場畫面曝光,為好心人點贊!
- java開發(fā)中常見中間件 java中間件技術(shù)有哪些
