浏览 4105 次
锁定老帖子 主题:JLabel保存为图片
精华帖 (0) :: 良好帖 (0) :: 新手帖 (9) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-18
最后修改:2009-06-18
import javax.swing.*; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /** * Created by IntelliJ IDEA. * User: Administrator * Date: 2009-6-18 * Time: 16:16:45 * To change this template use File | Settings | File Templates. */ public class TestJLabel2Image { public static void main(String ds[]) { final String[] columnNames = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; final int[] columnValues = {3, 7, 2, 1, 0, 9, 4, 6, 5, 7}; final int colorType = 1; int image_width = 180; int image_height = 180; final int table_width = 150; final int word_width = 20; final int word_height = 15; final int line_width = 10; final int line_height = 5; final JFrame f = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel() { public void paintComponent(Graphics g) { int x = 0; int y = 0; for (int i = 0; i < columnNames.length; i++) { String s = columnNames[i]; int count = columnValues[i]; y = y + word_height; g.drawString(s, x, y); if (count != 0) { int lineLength = 0; if (count > 25) { lineLength = line_width * 25; } else { lineLength = line_width * count; } if (colorType == 1) { g.setColor(Color.RED); } else { g.setColor(Color.BLUE); } g.fillRect(x + word_width, y - 5, lineLength, line_height); g.setColor(Color.BLACK); g.drawString(String.valueOf(count), x + word_width + lineLength + 5, y); } else { //g.drawString(String.valueOf(count), x + word_width, y); } } g.dispose(); } }; panel.setPreferredSize(new Dimension(table_width, image_height)); panel.add(label, BorderLayout.CENTER); f.getContentPane().add(panel); f.setSize(table_width, image_height); //f.setVisible(true); f.pack(); BufferedImage image = createImage(panel); f.dispose(); //对图片进行压缩处理,输出到指定目录 reduceImg(image, "d:/test.png", image_width, image_height); } public static BufferedImage createImage(JPanel panel) { int totalWidth = panel.getPreferredSize().width; int totalHeight = panel.getPreferredSize().height; BufferedImage panelImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2D = (Graphics2D) panelImage.createGraphics(); g2D.setColor(Color.WHITE); g2D.fillRect(0, 0, totalWidth, totalHeight); g2D.translate(0, 0); panel.paint(g2D); g2D.dispose(); return panelImage; } public static void reduceImg(BufferedImage bufferedImage, String imgdist, int widthdist, int heightdist) { try { BufferedImage tag = new BufferedImage(widthdist, heightdist, BufferedImage.TYPE_INT_RGB); /* * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 * 优先级比速度高 生成的图片质量比较好 但速度慢 */ tag.getGraphics().drawImage( bufferedImage.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(tag, "png", new File(imgdist)); } catch (IOException e) { e.printStackTrace(); } } } 和我另一篇JTable to Image代码基本一致,希望对大家有帮助 http://www.iteye.com/topic/386716 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-06-18
图片压缩代码是借用JE上的
|
|
返回顶楼 | |