本来想在JPanel上画图,然后保存为图片,但是发现如果循环创建JPanel保存图片太快,图片容易重叠出错,解决办法就是在JLabel上画图,代码如下:
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
分享到:
相关推荐
界面设计类似简单的QQ界面,可以使用BorderLayout布局管理器,在上面(BorderLayou.NORTH)显示“好友列表”四个字,中间部分(BorderLayou.CENTER)显示自己的好友名单,名单可以用多个JLabel控件(JLabel中添加...
"微调器 YearMonth date " "月份面板 MonthPane mp " "按钮 JButton " "bclear,daoru,bsave,daochu " "gnew,gopen,gsave,gcopy,gcut,gpaste,ghelp " "标签 JLabel time1,time2 " "菜单栏 JMenuBar mnb " "菜单 JMenu...
"微调器 YearMonth date " "月份面板 MonthPane mp " "按钮 JButton " "bclear,daoru,bsave,daochu " "gnew,gopen,gsave,gcopy,gcut,gpaste,ghelp " "标签 JLabel time1,time2 " "菜单栏 JMenuBar mnb " "菜单 JMenu...
实例14 判断输入的年份是否为闰年 26 实例15 抽奖活动 27 3.2 for语句 28 实例16 小九九乘法表 28 实例17 如何列出素数 29 实例18 Java中的递归 31 实例19 男生女生各多少人 32 实例20 求...
实例14 判断输入的年份是否为闰年 26 实例15 抽奖活动 27 3.2 for语句 28 实例16 小九九乘法表 28 实例17 如何列出素数 29 实例18 Java中的递归 31 实例19 男生女生各多少人 32 实例20 求水仙花数 34 实例21 求任意...
实例14 判断输入的年份是否为闰年 26 实例15 抽奖活动 27 3.2 for语句 28 实例16 小九九乘法表 28 实例17 如何列出素数 29 实例18 Java中的递归 31 实例19 男生女生各多少人 32 实例...
实例14 判断输入的年份是否为闰年 26 实例15 抽奖活动 27 3.2 for语句 28 实例16 小九九乘法表 28 实例17 如何列出素数 29 实例18 Java中的递归 31 实例19 男生女生各多少人 32 实例20 求水仙花数 34 实例21 求任意...
**为纸牌的正面设置白色图片 */ public void run(){ boolean is = false; ImageIcon icon = new ImageIcon("images/white.gif"); for (int i = 0; i ; i++){ try{ Thread.sleep(200); } catch ...
实例14 判断输入的年份是否为闰年 26 实例15 抽奖活动 27 3.2 for语句 28 实例16 小九九乘法表 28 实例17 如何列出素数 29 实例18 Java中的递归 31 实例19 男生女生各多少人 32 实例20 求水仙花数 34 实例21...
14.8.1标签(Jlabel)使用示例488 14.8.2按钮(JButton)使用示例491 14.8.3文本框(JTextField)和密码框(JPasswordField)使用示例492 14.8.4文本区(JTextArea)使用示例496 14.8.5复选框(JcheckBox)使用...