java swing 画图怎么保存?求详细代码
packagetest;importjava.awt.Color;importjava.awt.Graphics;importjavax.swing.JFrame;imp...
package test;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
class A {
JFrame frame;
public static void main(String[] args) {
A a = new A();
a.go();
}
public void go() {
MyDrawPanel drawPanel = new MyDrawPanel();
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
}
}
}
这是现在的代码 画完图我想保存为本地图片 求详细代码 本人小白 谢谢 展开
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
class A {
JFrame frame;
public static void main(String[] args) {
A a = new A();
a.go();
}
public void go() {
MyDrawPanel drawPanel = new MyDrawPanel();
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
}
}
}
这是现在的代码 画完图我想保存为本地图片 求详细代码 本人小白 谢谢 展开
1个回答
展开全部
package test;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class TestJLabel2Image {
public static void main(String ds[]) {
final JFrame f = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel() {
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
g.dispose();
}
};
panel.setPreferredSize(new Dimension(555, 555));
panel.add(label, BorderLayout.CENTER);
f.getContentPane().add(panel);
f.pack();
BufferedImage image = createImage(panel);
f.dispose();
try {
ImageIO.write(image, "png", new File("d:/test.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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;
}
}
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class TestJLabel2Image {
public static void main(String ds[]) {
final JFrame f = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel() {
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
g.dispose();
}
};
panel.setPreferredSize(new Dimension(555, 555));
panel.add(label, BorderLayout.CENTER);
f.getContentPane().add(panel);
f.pack();
BufferedImage image = createImage(panel);
f.dispose();
try {
ImageIO.write(image, "png", new File("d:/test.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询