如何像. 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, 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。
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, "png", outputfile);
} catch (IOException e) {
...
}
来源
如果您想了解更多信息,请阅读此文档:https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html