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?
Re: Combining Shapes + Text
Quote:
Originally Posted by
KevinWorkman
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?
Re: Combining Shapes + Text
Quote:
Originally Posted by
aussiemcgr
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
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)
Re: Combining Shapes + Text
Quote:
Originally Posted by
KevinWorkman
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.
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):
Code :
Font font = g.getFont();
TextLayout layout = new TextLayout("2", font, g.getFontRenderContext());
Shape s = layout.getOutline(g2.getTransform());
Quote:
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.