知道的麻烦告诉一下,不胜感激,最好能给点代码看看!
14 个解决方案
#1
image应该有保存功能吧。
#2
@Override
public boolean exportOtherShapesImage(File f, DrawPanel panel) {
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
#3
#4
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.FlowLayout;
public class Application extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JPanel picturePanel;
private JLabel pictureLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Application frame = new Application();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Application() {
setTitle("\u56FE\u7247\u6D4F\u89C8\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 458, 304);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
{
JToolBar toolBar = new JToolBar();
contentPane.add(toolBar, BorderLayout.NORTH);
{
JButton openButton = new JButton("\u6253\u5F00");
openButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);//设置过滤器
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile()
.getAbsolutePath());
pictureLabel.setIcon(new ImageIcon(
chooser.getSelectedFile().getAbsolutePath()));
}
}
});
toolBar.add(openButton);
}
}
{
picturePanel = new JPanel();
contentPane.add(picturePanel, BorderLayout.CENTER);
picturePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
{
pictureLabel = new JLabel("");
picturePanel.add(pictureLabel);
}
}
}
}
#5
2楼的正确又有问题,如果你那样用panel.paint(g);来设置panel的画笔,到最后你所保存的图片就是所画上去的内容,并不能够将Jpanel上添加的组建画上去,
运行下就知道保存的只是用g.drawLine方法的图片
将添加组建画上去貌似还没有什么方案能行,既然要保存成图片,应该这个JPanel内容都应该用画笔将其画上去
public class PanelImage extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(10, 10, 50, 50);
}
public boolean exportOtherShapesImage(File f, JPanel panel)
{
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try
{
ImageIO.write(image, "png", f);
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
public static void main(String[] args)
{
PanelImage p = new PanelImage();
p.add(new JLabel("this is my Label"));
p.setSize(500, 500);
p.setVisible(true);
p.exportOtherShapesImage(new File("d:/1.png"), p);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 500, 500);
f.add(p);
f.setVisible(true);
}
}
运行下就知道保存的只是用g.drawLine方法的图片
将添加组建画上去貌似还没有什么方案能行,既然要保存成图片,应该这个JPanel内容都应该用画笔将其画上去
#6
初来乍到顶一个
#7
我就是用2楼的办法,也能将JPanel上的组件保存到图片中
#8
public boolean exportOtherShapesImage(File f, DrawPanel panel) {
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
#9
mark..
#10
那就怪事了,我怎么的都不能够将组建保存,保存的只是画上去的东西
#11
这些个人怎么都这么牛的
#12
纯属接风~~~~~~~~O(∩_∩)O~~
#13
me too
#14
private JMenuItem getJMenuItemSaveAs() {
if (jMenuItemSaveAs == null) {
jMenuItemSaveAs = new JMenuItem("另存为");
jMenuItemSaveAs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fileChooser=new JFileChooser();
fileChooser.setSelectedFile(new File("default.png"));
int returnVal = fileChooser.showSaveDialog(jMenuItemSaveAs);
if(returnVal==JFileChooser.APPROVE_OPTION){
Dimension imageSize = jPanelTopologyDiscovery.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = image.createGraphics();
jPanelTopologyDiscovery.paint(g);
g.dispose();
File topologyImage=fileChooser.getSelectedFile();
try {
if(!topologyImage.exists()){
ImageIO.write(image, "png", topologyImage);
JOptionPane notice=new JOptionPane ();
notice.showMessageDialog(null, "恭喜,图片保存成功!","保存成功",JOptionPane.INFORMATION_MESSAGE);
}else{
int returnValue=0;
JOptionPane notice=new JOptionPane ();
returnValue=notice.showConfirmDialog(fileChooser, "文件"+topologyImage.getName()+"已经存在,是否覆盖?", "保存文件",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(returnValue==JOptionPane.YES_OPTION){
ImageIO.write(image, "png", topologyImage);
//JOptionPane notice=new JOptionPane ();
notice.showMessageDialog(null, "恭喜,图片保存成功!","保存成功",JOptionPane.INFORMATION_MESSAGE);
}else{
actionPerformed(e);
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
}
return jMenuItemSaveAs;
}
#1
image应该有保存功能吧。
#2
@Override
public boolean exportOtherShapesImage(File f, DrawPanel panel) {
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
#3
#4
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.FlowLayout;
public class Application extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JPanel picturePanel;
private JLabel pictureLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Application frame = new Application();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Application() {
setTitle("\u56FE\u7247\u6D4F\u89C8\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 458, 304);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
{
JToolBar toolBar = new JToolBar();
contentPane.add(toolBar, BorderLayout.NORTH);
{
JButton openButton = new JButton("\u6253\u5F00");
openButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);//设置过滤器
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile()
.getAbsolutePath());
pictureLabel.setIcon(new ImageIcon(
chooser.getSelectedFile().getAbsolutePath()));
}
}
});
toolBar.add(openButton);
}
}
{
picturePanel = new JPanel();
contentPane.add(picturePanel, BorderLayout.CENTER);
picturePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
{
pictureLabel = new JLabel("");
picturePanel.add(pictureLabel);
}
}
}
}
#5
2楼的正确又有问题,如果你那样用panel.paint(g);来设置panel的画笔,到最后你所保存的图片就是所画上去的内容,并不能够将Jpanel上添加的组建画上去,
运行下就知道保存的只是用g.drawLine方法的图片
将添加组建画上去貌似还没有什么方案能行,既然要保存成图片,应该这个JPanel内容都应该用画笔将其画上去
public class PanelImage extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(10, 10, 50, 50);
}
public boolean exportOtherShapesImage(File f, JPanel panel)
{
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try
{
ImageIO.write(image, "png", f);
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
public static void main(String[] args)
{
PanelImage p = new PanelImage();
p.add(new JLabel("this is my Label"));
p.setSize(500, 500);
p.setVisible(true);
p.exportOtherShapesImage(new File("d:/1.png"), p);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 500, 500);
f.add(p);
f.setVisible(true);
}
}
运行下就知道保存的只是用g.drawLine方法的图片
将添加组建画上去貌似还没有什么方案能行,既然要保存成图片,应该这个JPanel内容都应该用画笔将其画上去
#6
初来乍到顶一个
#7
我就是用2楼的办法,也能将JPanel上的组件保存到图片中
#8
public boolean exportOtherShapesImage(File f, DrawPanel panel) {
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
g.dispose();
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("export Image -->" + f.getAbsoluteFile());
return true;
}
#9
mark..
#10
那就怪事了,我怎么的都不能够将组建保存,保存的只是画上去的东西
#11
这些个人怎么都这么牛的
#12
纯属接风~~~~~~~~O(∩_∩)O~~
#13
me too
#14
private JMenuItem getJMenuItemSaveAs() {
if (jMenuItemSaveAs == null) {
jMenuItemSaveAs = new JMenuItem("另存为");
jMenuItemSaveAs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fileChooser=new JFileChooser();
fileChooser.setSelectedFile(new File("default.png"));
int returnVal = fileChooser.showSaveDialog(jMenuItemSaveAs);
if(returnVal==JFileChooser.APPROVE_OPTION){
Dimension imageSize = jPanelTopologyDiscovery.getSize();
BufferedImage image = new BufferedImage(imageSize.width,
imageSize.height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = image.createGraphics();
jPanelTopologyDiscovery.paint(g);
g.dispose();
File topologyImage=fileChooser.getSelectedFile();
try {
if(!topologyImage.exists()){
ImageIO.write(image, "png", topologyImage);
JOptionPane notice=new JOptionPane ();
notice.showMessageDialog(null, "恭喜,图片保存成功!","保存成功",JOptionPane.INFORMATION_MESSAGE);
}else{
int returnValue=0;
JOptionPane notice=new JOptionPane ();
returnValue=notice.showConfirmDialog(fileChooser, "文件"+topologyImage.getName()+"已经存在,是否覆盖?", "保存文件",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(returnValue==JOptionPane.YES_OPTION){
ImageIO.write(image, "png", topologyImage);
//JOptionPane notice=new JOptionPane ();
notice.showMessageDialog(null, "恭喜,图片保存成功!","保存成功",JOptionPane.INFORMATION_MESSAGE);
}else{
actionPerformed(e);
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
}
return jMenuItemSaveAs;
}