Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Filling awkward shapes with color

  1. #1
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Lightbulb Filling awkward shapes with color

    Hi,

    forgive me if this question is too general/noobish/stupid, but i've never attempting this before and so i had to ask.

    Anyway, basically i wanted to know if it was possible to fill nonstandard shapes with color. I know how to draw a rectangle/triangle/circle/etc with a color, but the shape i'm making is not a standard shape like that.

    For instance, what kind of shape is this lol?

    california-stencil.jpg

    Just to give you some background as to what i'm doing, basically i'm going to take the map of the united states, and i want to be able to fill the states with different colors. I know i can prefill the shapes with color in photoshop, but that doesnt seem very efficient especially if im going to need multiple colors.

    Is there another way to do it? what techniques can i use to achieve this.

    I want to be able to click on any state on the map and change its color.

    thanks.

    EDIT: Idk why that other California stencil below is attached to my post...
    Attached Images Attached Images


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Filling awkward shapes with color

    For instance, what kind of shape is this lol?
    It could be an instance of Polygon. There are methods in the Graphics2D class to draw and fill these things. And to use them as the clipping region for other painting operations.

    The other way to go would be to regard the shape as a bitmapped image. In that case you would paint it wherever you want it.

    The distinction is between vector and raster graphics and you have to consider what would be more appropriate in your circumstances. How easy will it be to get the data? Will the shape scale or undergo other transformations? Do you want "hit detection"? etc

    Oracle's Tutorial has a whole section on 2D graphics which will be useful in either case.

  3. #3
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Filling awkward shapes with color

    Thank you very much, that gives me a great place to start.

    How can i regard that as a vector image? I'm more comfortable using vector images than bitmaps. Are the techniques to fill it, essentially the same?

    EDIT: Okay so i read a bit, but i dont see how i the polygon can help me fill such a complex shape like the states.
    Last edited by clydefrog; February 29th, 2012 at 11:12 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Filling awkward shapes with color

    Quote Originally Posted by clydefrog View Post
    i dont see how i the polygon can help me fill such a complex shape like the states.

    Polygon implements Shape. And presuming you are using Swing (based upon the category this is posted in), you can use the Graphics2D methods to draw or fill any Shape.

  5. #5
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Filling awkward shapes with color

    okay i'll take another look, thanks for the reply.

    is there an example i can look at? I cant seem to find anything on the web about filling in complicated and asymmetrical shapes like California lol.

Similar Threads

  1. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  2. Combining Shapes + Text
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: February 17th, 2011, 10:15 AM
  3. How I can create those shapes with Java?
    By Learner in forum AWT / Java Swing
    Replies: 3
    Last Post: November 18th, 2010, 02:10 AM
  4. Converting Images to Shapes
    By Ian in forum Java Theory & Questions
    Replies: 2
    Last Post: February 7th, 2010, 05:18 PM
  5. Filling an Array?
    By Bascotie in forum Collections and Generics
    Replies: 5
    Last Post: October 14th, 2009, 06:27 PM