无法将当前canvas数据转换为java中的图像

我有一个简单的应用程序,允许用户在canvas控件中绘制。

现在,我想要的是将canvas转换为图像。 所以这是我的代码。

public void paint(Graphics g) { //super.paint(g); Graphics2D draw = (Graphics2D) g; if(this.is_beginning || this.to_save) { draw.setColor(Color.white); draw.fillRect(0, 0, this.getWidth(), this.getHeight()); this.is_beginning= false; } if(this.m_alzada) { draw.setColor(Color.red); draw.drawLine(uX, uY, x, y); } } 

这是我保存图像的方法。

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { int w = canvas1.getWidth(); int h = canvas1.getHeight(); int type = BufferedImage.TYPE_INT_BGR; BufferedImage image = new BufferedImage(w,h,type); Graphics2D g2 = image.createGraphics(); canvas1.to_save = true; canvas1.paint(g2); try { ImageIO.write(image, "png", new File("C:/Users/Uriel/Desktop/ejemplo.png")); } catch (IOException ex) { Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex); } } 

所有这些都导致了一个空白的图像,我知道绘制方法是如何工作的,我意识到那就是我的问题所在。 但是我该如何绘制用户已经在paint方法中绘制的所有东西呢?

对不起,我的英语不好,我来自墨西哥。 谢谢你的方式。

无论如何,当我使用Canvas og HTML5并且你得到一个矩阵,其中包含canvas中每个像素的RGB信息时,我想知道。 是否可以使用JAVA中的canvas组件执行此操作?

除了确保组件的大小正确外,还可以使用JComponent#printJComponent#printAll方法。

这些将禁用双缓冲,并且当它希望打印到屏幕时会出现其他一些本机对等问题

更新

从示例应用程序…

在此处输入图像描述

我能够生产这个转储

在此处输入图像描述

使用此代码

 Container pane = frame.getContentPane(); BufferedImage img = new BufferedImage(pane.getWidth(), pane.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); pane.printAll(g2d); g2d.dispose(); try { ImageIO.write(img, "png", new File("save.png")); } catch (IOException ex) { ex.printStackTrace(); } 

更新

我不认为你画的是问题的根源。 尽管如此,它并不是那么干净。

首先,你的“绘图”表面是从java.awt.Canvas扩展的,你将它添加到JFrame ,混合重量和重量轻的组件从来都不是一个好主意。

 public class Dibujo extends Canvas ... 

你最好使用像JPanel这样的东西

 public class Dibujo extends JPanel ... 

永远不要这样做

 public void paint(Graphics g) { //super.paint(g); 

你必须调用super.paint还有更多内容,然后只需填充组件。 一旦开始使用像JPanel这样的东西,你就会想要覆盖paintComponent

您只能在绘制方法中绘制最后一个线段…

 if (this.m_alzada) { draw.setColor(Color. draw.drawLine(uX, uY, x, y); } 

这意味着当您尝试保存组件时,您将只看到最后一个段。 paint方法应该在每次调用时绘制所有线段。

在你的mouseDragged方法中,你正在这样做……

 this.paint(this.getGraphics()); 

别。 您不负责更新重绘经理的图形。 所有这一切基本上都是绘制到便笺簿图形上下文,一旦处理下一个重绘请求,它将全部擦除干净。

我想你需要通过执行自定义绘画阅读来理解一些基本概念。 我还会阅读AWT和Swing中的绘画,以了解绘画在Java中是如何工作的。

修改你的代码后,我得到了这个……

在此处输入图像描述

要像这样保存……

在此处输入图像描述

 package prueba_uno_graphics; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Shape; import java.awt.event.*; import java.awt.geom.Path2D; import java.util.ArrayList; import java.util.List; import javax.swing.JPanel; /** * * @author Uriel */ // Don't mix heavy and light weight components public class Dibujo extends JPanel implements ActionListener, MouseListener, MouseMotionListener { // ArrayList lineas = new ArrayList(); // boolean m_alzada = true, is_beginning = true, to_save = false; // int uX, uY, x, y; private Path2D shape; Dibujo() { setBackground(Color.WHITE); shape = new Path2D.Float(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D draw = (Graphics2D) g; // if (this.is_beginning || this.to_save) { // draw.setColor(Color.white); // draw.fillRect(0, 0, this.getWidth(), this.getHeight()); // this.is_beginning = false; // } // if (this.m_alzada) { // draw.setColor(Color.red); // draw.drawLine(uX, uY, x, y); // // } draw.setColor(Color.RED); draw.draw(shape); } // @Override // public void paint(Graphics g) { // // ALWAYS call super.paint // super.paint(g); // Graphics2D draw = (Graphics2D) g; // if (this.is_beginning || this.to_save) { // draw.setColor(Color.white); // draw.fillRect(0, 0, this.getWidth(), this.getHeight()); // this.is_beginning = false; // } // if (this.m_alzada) { // draw.setColor(Color.red); // draw.drawLine(uX, uY, x, y); // // } // } @Override public void actionPerformed(ActionEvent e) { } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { // this.uX = e.getX(); // this.uY = e.getY(); Point point = e.getPoint(); shape.moveTo(point.x, point.y); } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseDragged(MouseEvent e) { // this.x = e.getX(); // this.y = e.getY(); // Don't do this! // this.paint(this.getGraphics()); // ArrayList ayuda = new ArrayList(); // ayuda.add(uX); // ayuda.add(uY); // ayuda.add(x); // ayuda.add(y); // this.lineas.add(ayuda); // uX = x; // uY = y; Point point = e.getPoint(); shape.lineTo(point.x, point.y); repaint(); } @Override public void mouseMoved(MouseEvent e) { } }