I have a simple application that allows the user to draw in the canvas control.
Now, what I want is converting that canvas into an image. So here’s 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 save 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 these result in a blank image, I know how the paint method works, and I realize that is in there where my problem is. But how can I do to drew everything the user have already drew in the paint method?
Excuse me for my bad english, I’m from Mexico. Thanks by the way.
I would like to knoe if there is anyway to make something like when you work with the Canvas og HTML5 and you get a matrix with the RGB info of each pixel in the canvas. Is it possible to do that with the canvas component in JAVA?
Apart from making sure the the component is sized properly, use
JComponent#printandJComponent#printAllmethods instead.These will disable double buffering and over come some other native peer issues when it expects to be printing to the screen
UPDATED
From the example app…
I was able to produce this dump
Using this code
UPDATED
I don’t think you paint is the source of your problem. It’s not as clean as it could though.
To start with, your “drawing” surface is extending from
java.awt.Canvas, and you’re adding it to aJFrame, mixing heavy and light weight components is never a good idea.You’re better of using something like a
JPanelNEVER DO THIS
You MUST call
super.paintthere is more going on in the back then simply filling the component. Once you start using something likeJPanel, you’ll want to overridepaintComponentinstead.You only ever draw the last line segment in you paint method…
This means when you try and save the component, you will only ever see the last segment. The
paintmethod should be painting ALL the lines segments each time it’s called.In your
mouseDraggedmethod, you are doing this…DON’T. You are not responsible for updating the graphics, that repaint manager is. All this does is basically doing is painting on to a scratch pad graphics context, as soon as the next repaint request is processed, it will all be wiped clean.
I think you need to have a read through Performing Custom Painting to understand some of the basic concepts. I would also read through Painting in AWT and Swing to understand how painting works in Java.
After modifying your code, I was able to get this…
To save like this…