Results 1 to 4 of 4
  1. #1
    Henriamaa is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Question Making a Drawing Class

    I would like to make a drawing class in which methods for drawing different shapes are implemented so that these methods can be used in different classes. So if the class was called Draw, then using the a method in a different class could go something like this: Draw.circle(parameters go here). The issue I am running into is that using paintComponent means that I can't separate drawing the different shapes into different methods and therefore one method has all the shape drawing and thus I can't call a particular method to to draw a particular shape. I am a physics student, so when I I want to draw shapes( to represent different grid structures) it is always within the context of a long algorithm and I would like to get all the drawing code in one class ready for use. I do not want everything lumped into paintComponent method. Help would be greatly appreciated.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default Re: Making a Drawing Class

    Quote Originally Posted by Henriamaa View Post
    ...The issue I am running into is that using paintComponent means that I can't separate drawing the different shapes into different methods ...
    Sure you can. The paintComponent method can call methods from within it, and in fact this is often done. You can even have it select which methods it wishes to call by having it respond to the "state" of the object, meaning give it logical blocks such as if blocks that will allow it to call a variety of methods depending on what is held by the object's variables.

    ...and therefore one method has all the shape drawing and thus I can't call a particular method to to draw a particular shape. I am a physics student, so when I I want to draw shapes( to represent different grid structures) it is always within the context of a long algorithm and I would like to get all the drawing code in one class ready for use. I do not want everything lumped into paintComponent method. Help would be greatly appreciated.
    Another option that is worthwhile looking into is having your paintComponent method draw a BufferedImage (if it's not null), and have your other methods change what is drawn on this image. Perhaps this could be what would work best for your purposes.

  3. #3
    Henriamaa is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Making a Drawing Class

    I can't seem to picture how exactly the BufferedImage scenario would work. Please excuse, my ignorance.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default Re: Making a Drawing Class

    Have a look through this and other forums to get some good (and not-so-good) examples of working with BufferedImages. For instance, have a look at the code in this answer here.

Similar Threads

  1. Making a class to use in a project
    By traisjames in forum New To Java
    Replies: 6
    Last Post: 10-26-2012, 01:22 AM
  2. Replies: 1
    Last Post: 11-06-2011, 11:18 PM
  3. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  4. Replies: 16
    Last Post: 05-22-2010, 03:51 AM
  5. Making a drawing tool
    By Mike_H in forum New To Java
    Replies: 1
    Last Post: 03-16-2010, 12:23 PM

Posting Permissions

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