Cannot convert current canvas data to image in java

I have a simple application that allows the user to draw a canvas control.

Now I want to convert this canvas to an image. So here is my code.

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); } } 

And this is my method for saving the image.

 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); } } 

All this leads to a blank image, I know how the drawing method works, and I understand that where my problem is. But how can I do to attract everything that the user has already used in the drawing method?

Excuse me for my bad English, I'm from Mexico. Thanks, by the way.

I would like to know if there is something like when you work with Canvas og HTML5 and you get a matrix with the RGB information of each pixel on the canvas. Is it possible to do this with the canvas component in JAVA?

+4
source share
1 answer

In addition to the component being properly configured, use JComponent#print and JComponent#printAll .

They will disable double buffering and will pass some other problems with independent experts when they expect to print on the screen.

UPDATED

In the sample application ...

enter image description here

I was able to create this dump

enter image description here

Using this code

 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(); } 

UPDATED

I do not think that you are painting, this is the source of your problem. It is not as clean as it could be.

For starters, your drawing surface extends from java.awt.Canvas , and you add it to the JFrame , mixing heavy and light components will never be a good idea.

 public class Dibujo extends Canvas ... 

Better use something like JPanel

 public class Dibujo extends JPanel ... 

NEVER DO IT

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

You MUST call super.paint , then the component is super.paint , and then just the component is populated. After you start using something like JPanel , you will want to override paintComponent instead.

You only draw the last line segment in your drawing method ...

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

This means that when you try to save the component, you will see only the last segment. The paint method should draw ALL line segments every time it calls.

In your mouseDragged method mouseDragged you do this ...

 this.paint(this.getGraphics()); 

DO NOT. You are not responsible for updating the graphics; this is a redraw manager. All this does is basically draw on the graphics context from scratch, as soon as the next redraw request is processed, it will all be cleared.

I think you need to read Performing Custom Painting to understand some basic concepts. I would also read Painting in AWT and Swing to understand how painting works in Java.

After changing the code, I was able to get this ...

enter image description here

To save like this ...

enter image description here

 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) { } } 
+5
source

Source: https://habr.com/ru/post/1440901/


All Articles