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 6 of 6

Thread: Combining Shapes + Text

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Combining Shapes + Text

    I have a question regarding combining shapes.

    I currently had a simple class (below):
    import java.awt.geom.Ellipse2D.Double;
    import java.awt.Rectangle;
    import java.awt.geom.Area;
     
    public class Island extends HashiSymbol
    {
    	int value;
     
        public Island(int x,int y,int valuec) 
        {
        	super(x,y);
        	value = valuec;
        }
     
        public Area getShape()
        {
        	Area ret = new Area();
        	Rectangle rect = new Rectangle(x,y,30,30);
        	ret.add(new Area(rect));
        	Double circ = new Double(x,y,30,30);
        	ret.exclusiveOr(new Area(circ));
        	return ret;
        }
    }

    I have several graphical objects that extend HashiSymbol, which is another class I made that I will probably make extend Area (Area (Java Platform SE 6)) later on. Each of these objects present themselves differently, but they will all be painted to the main component. Because of stylistic reasons, I provided a method that returns the Shape (Shape (Java Platform SE 6)) of the object to be painted by my Graphics2D (Graphics2D (Java Platform SE 6)) object in my paintComponent method. Since these Shapes can contain several different shapes, I used the Area class to combine Shapes to create one paintable Shape.

    The Island object should contain 3 different "shapes":
    1) A rectangle border (mainly to show bounds)
    2) A circle border
    3) A single number between 1 and 8 in the center of the circle border

    I have the first two in the above code, but I cannot figure out how to represent Text as a Shape that I can add to Area. Does anyone know how to do this? Simply painting a String to the main component is not desired, as the text should be part of the Island object and not an individual component. Any suggestions or help is appreciated.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Combining Shapes + Text

    Instead of having each Object return a Shape that is then drawn, why not give each Object a draw(Graphics2D g) method, then let each of them draw itself? Then in your paintComponent(Graphics g) method, simply call the draw method of each Object?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Combining Shapes + Text

    Quote Originally Posted by KevinWorkman View Post
    Instead of having each Object return a Shape that is then drawn, why not give each Object a draw(Graphics2D g) method, then let each of them draw itself? Then in your paintComponent(Graphics g) method, simply call the draw method of each Object?
    I'm going to think that over and get back to you. After I get all the actual graphical displaying and other things down, I will be adding mouse listeners that alter the design of the objects based on what the user determines. It will be important to know what object was pressed by the user and change it accordingly. I was using Shapes because I could call the .contains() method to determine if a mouse event occurred in a given object. I'm not sure of any drawback that could occur by using a draw method instead of a getShape() method, but I'll think it over and give it a try if nothing sounds like a problem.

    Incidentally, does anyone know of any path-creating algorithm that I can use/modify? The goal of this will be to have an X number of "Islands" and have them all connected by lines. Not necessarily directly connected, but linked at least. The total path will be bounded in the window and the lines are either horizontal or vertical with no lines overlapping each other and dead ends are acceptable. I'm considering drawing out the path and then determining where the Islands should be based on where the lines change directions, meet, or lead to dead ends. Anyone have any other ideas that could work?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Combining Shapes + Text

    Quote Originally Posted by aussiemcgr View Post
    I'm going to think that over and get back to you. After I get all the actual graphical displaying and other things down, I will be adding mouse listeners that alter the design of the objects based on what the user determines. It will be important to know what object was pressed by the user and change it accordingly. I was using Shapes because I could call the .contains() method to determine if a mouse event occurred in a given object. I'm not sure of any drawback that could occur by using a draw method instead of a getShape() method, but I'll think it over and give it a try if nothing sounds like a problem.
    Well, you could give each Object a contains() method as well.


    Quote Originally Posted by aussiemcgr View Post
    Incidentally, does anyone know of any path-creating algorithm that I can use/modify? The goal of this will be to have an X number of "Islands" and have them all connected by lines. Not necessarily directly connected, but linked at least. The total path will be bounded in the window and the lines are either horizontal or vertical with no lines overlapping each other and dead ends are acceptable. I'm considering drawing out the path and then determining where the Islands should be based on where the lines change directions, meet, or lead to dead ends. Anyone have any other ideas that could work?
    Path2D (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Combining Shapes + Text

    Quote Originally Posted by KevinWorkman View Post
    Well, you could give each Object a contains() method as well.
    I was attempting to avoid that since I'm lazy. Obviously if there was no other way of dealing with this other than using a drawing method, I could do that (it's only a handful of lines anyway). My hope was that it would just be generally neater to have the object as an object instead of just a bunch of seemingly randomly drawn lines.


    The Path2D is a path Shape, not an actual pathing algorithm. I'm trying to figure out a way to effectively draw out a maze of lines.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: Combining Shapes + Text

    I agree with Kevin's suggestion...delegate the drawing to each object you want to draw. You could theoretically create an interface that has 2 methods: a draw and contains. This will abstract the drawing and selection routines away from the actual drawer component and give you a lot more flexibility in what you draw (for example rendering images, drawing text, etc...).

    That being said, to answer your original question you can get a Shape of text using the following (presuming the variable g is a Graphics2D instance):
    Font font = g.getFont();
    TextLayout layout = new TextLayout("2", font, g.getFontRenderContext());
    Shape s = layout.getOutline(g2.getTransform());

    I'm trying to figure out a way to effectively draw out a maze of lines.
    Create a graph data structure, then traverse the graph and (if needed) drawing lines between.
    Last edited by copeg; February 17th, 2011 at 10:18 AM.

Similar Threads

  1. 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
  2. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  3. demarkating portions of images in different shapes
    By forgetlosing in forum Java Theory & Questions
    Replies: 0
    Last Post: March 25th, 2010, 08:41 AM
  4. Converting Images to Shapes
    By Ian in forum Java Theory & Questions
    Replies: 2
    Last Post: February 7th, 2010, 05:18 PM
  5. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 04:35 PM