问题描述
如何将我的绘图保存为 .jpeg .png 格式?我想捕获在面板上绘制的图像并在单击保存按钮时保存。 我尝试了缓冲图像方法,但无法运行。当我搜索缓冲图像方法时,它需要扩展到 JPanel,我的代码基于 JFrame。
应用程序的当前版本:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GridLayout;
public class Cizim extends JFrame implements MouseMotionListener,ActionListener {
private int x = -10,y = -10 ;
private Color col = Color.BLACK;
private JLabel mousePos;
public Cizim() {
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JPanel bottom = new JPanel();
bottom.setLayout(new BorderLayout());
mousePos = new JLabel("(,)");
bottom.add(mousePos,BorderLayout.WEST);
bottom.setVisible(true);
//JFrame
setTitle("Cizim");
setSize(800,600);
setDefaultCloSEOperation(EXIT_ON_CLOSE);
JPanel p= new JPanel();
p.setLayout(new GridLayout(15,1));
//Buton Renkleri
JButton red = new JButton("Kirmizi");
red.setBackground(Color.RED);
JButton green = new JButton("Yesil");
green.setBackground(Color.GREEN);
JButton magenta = new JButton("Mor");
magenta.setBackground(Color.magenta);
JButton cyan = new JButton("Turkuaz");
cyan.setBackground(Color.CYAN);
JButton orange = new JButton("Turuncu");
orange.setBackground(Color.ORANGE);
JButton yellow = new JButton("Sari");
yellow.setBackground(Color.YELLOW);
JButton pink = new JButton("Pembe");
pink.setBackground(Color.pink);
JButton blue = new JButton ("Mavi");
blue.setBackground(new Color (51,51,204));
JButton gray = new JButton("Gri");
gray.setBackground(Color.GRAY);
JButton black = new JButton("Siyah");
black.setBackground(Color.BLACK);
JButton white = new JButton("Silgi");
white.setBackground(Color.WHITE);
//
p.add(red);
p.add(green);
p.add(magenta);
p.add(cyan);
p.add(orange);
p.add(yellow);
p.add(pink);
p.add(blue);
p.add(gray);
p.add(black);
p.add(white);
red.addActionListener(this);
green.addActionListener(this);
magenta.addActionListener(this);
cyan.addActionListener(this);
orange.addActionListener(this);
yellow.addActionListener(this);
pink.addActionListener(this);
blue.addActionListener(this);
gray.addActionListener(this);
black.addActionListener(this);
white.addActionListener(this); //Beyaz rengi silgi olarak kullandım.
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
JLabel instructions = new JLabel("Cizmek icin fareyi hareket ettirin.",JLabel.RIGHT);
c.add(instructions,BorderLayout.soUTH);
c.add(p,BorderLayout.WEST);
c.addMouseMotionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String act = e.getActionCommand();
if(act.equals("Mavi"))
col = new Color(51,204);
else if(act.equals("Kirmizi"))
col = Color.RED;
else if(act.equals("Yesil"))
col = Color.GREEN;
else if(act.equals("Mor"))
col = Color.magenta;
else if(act.equals("Turkuaz"))
col = Color.CYAN;
else if(act.equals("Turuncu"))
col = Color.ORANGE;
else if(act.equals("Sari"))
col = Color.YELLOW;
else if(act.equals("Pembe"))
col = Color.PINK;
else if(act.equals("Gri"))
col = Color.GRAY;
else if(act.equals("Siyah"))
col = Color.BLACK;
else if(act.equals("Silgi"))
col = Color.WHITE;
else//bunlardan biri değilse renk siyah olsun
col = Color.BLACK;
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
x = e.getX(); y= e.getY();
repaint();// özel parametre ile çağrılan çizim metodu
}
public void paint(Graphics g) {
g.setColor(col);
g.fillRect(x,y,10,10);
}
public static void main (String args[]) {
Cizim p = new Cizim();
}
}
解决方法
说明
- 检索图像
- 创建文件以存储图像
- 将检索到的图像保存到新创建的文件中
检索
创建一个缓冲图像。
BufferedImage bi = getMyImage();
文件创建
创建一个 .png 文件。
File outputFile = new File("save.png");
书写图像
在 .png 文件上绘制缓冲图像。
ImageIO.write(bi,"png",outputfile);
完整代码
try {
BufferedImage bi = getMyImage();
File outputfile = new File("saved.png");
ImageIO.write(bi,outputfile);
} catch (IOException e) {
...
}
来源
如果您想了解更多信息,请阅读此文档: https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html