Results 1 to 12 of 12
  1. #1
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default create image from canvas

    Hi everybody.........

    I have facing some problem when I try to create an image from a canvas..

    I have two class-- one of the is super class which extends JApplet and other is sub class which extends Canvas class.
    This sub class and a button are added to the super class using border layout within init().

    So sub class contains only two methods paint(Graphics g) and update(Graphics g). From paint() and and update() I have called just one method of super class updateCanvas().

    Now updateCanvas() again called another method dragAndDrawCircle(), by which we can draw circle using mouse drag.

    Now I want to create an image exactly same as whatever things are drawn on the canvas by click on the button.

    But I don't know how can I do it?

    So help me please.

    Thank you.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    Don't neglect to use the code tags so your code retains its formatting.

    db

    edit You didn't bother to return to the last thread you started.
    getClickCount() problem within mouseClicked method

    Are we going to see the same discourtesy here?
    Last edited by DarrylBurke; 08-19-2011 at 01:22 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I want to create an image exactly same as whatever things are drawn on the canvas by click on the button
    If you save what was drawn then you should be able to draw it in another place.
    You could save the image in a buffered image or you could save the points that were used to draw the image in a collection of some kind.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    one of the is super class which extends JApplet and other is sub class which extends Canvas class.
    Don't use AWT components in a Swing application.

  5. #5
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post



    edit You didn't bother to return to the last thread you started.
    getClickCount() problem within mouseClicked method

    Are we going to see the same discourtesy here?
    Sorry for not continuing that thread.
    But I solved this problem.

    Thank you.

  6. #6
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Don't use AWT components in a Swing application.

    I need to use Canvas, because I have to draw some geometrical shapes.
    Then what should I use in place of Canvas?

    Thank you.

  7. #7
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    If you save what was drawn then you should be able to draw it in another place.
    You could save the image in a buffered image or you could save the points that were used to draw the image in a collection of some kind.
    I can't get your point about "draw it another place".
    Please explain !

    Whatever I solved this problem like this ---


    Java Code:
    Dimension size = can.getSize();
    
                // create a buffered image the correct size
                BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    
                // paint the canvas to the image's graphics context
                Graphics2D graphics = image.createGraphics();
                can.paintAll(graphics);
    
                // tidy up
                graphics.dispose();
    
                // save the buffered image to a file
                FileOutputStream fos = null;
                try {
                
                fos = new FileOutputStream("image.jpg");
                //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
                //encoder.encode(image);
                ImageIO.write(image, "jpg", fos);
                fos.close();
                }
                catch (Exception err) {
                err.printStackTrace();
                }
    Here "can" is the object of sub class which extends Canvas.

    I have attached an image at the last post of this thread which I generate through the above code.
    But if u look at the image, then u will see, the image is not so clear.

    So is any other way to create image?
    Please inform me.

    Thanks.

  8. #8
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Sorry, I can't attached image to this post, it wants image url, which I hav't. Because image is in my hard disk.

    Image is not so clear, it has light black spot around the circle.
    Last edited by mitra; 08-20-2011 at 12:08 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mitra View Post
    I need to use Canvas, because I have to draw some geometrical shapes.
    This makes no sense and suggests that you haven't gone through the graphics tutorials before making this statement.


    Then what should I use in place of Canvas?
    Consider JPanel, but again most important, go through the tutorials. You won't regret this.

  10. #10
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post




    Consider JPanel, but again most important, go through the tutorials. You won't regret this.

    yes, I read before about JPanel and its paintComponent() method.
    Thanks..

    Can u tell me how can I connect with Oracle 11g database?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mitra View Post
    Can u tell me how can I connect with Oracle 11g database?
    How does this relate to your original question?

    Answer: it doesn't, and if you have a problem with database connection, you should ask it in a new thread.

  12. #12
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

Similar Threads

  1. How to save a canvas in a scrolled composite as image
    By wolfgang76 in forum SWT / JFace
    Replies: 0
    Last Post: 12-30-2010, 01:32 AM
  2. How to create a Warhol effect in an image.
    By racewithferrari in forum New To Java
    Replies: 12
    Last Post: 04-14-2010, 01:54 AM
  3. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  4. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 01:25 PM
  5. Visualizing an image on Canvas
    By a_klasanov in forum CLDC and MIDP
    Replies: 2
    Last Post: 08-01-2008, 10:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •