Results 1 to 8 of 8
Thread: Drawing graphics primitives
- 09-20-2009, 07:05 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
Drawing graphics primitives
Hi, I'm doing a program to draw graphics primitives (lines, rectangles, etc...) with my own implementations.
I think that the best approach would be to implement my own Graphics2D class, but that would be a lot of work and a lots of abstract methods that I don't know how to implement.
So, to make this a little bit easier, I'm extending a JPanel, and there, I'm drawing my primitives. The basic structure for the drawing is this:
Particulary, the cleanRaster() method is not efficient.Java Code:protected void paintComponent(Graphics g) { super.paintComponent(g); if (!isInitialized()) { initialize(); } cleanRaster(); try { for (GraphicPrimitive gp : getImageController().getGraphicPrimitives()) { if (gp instanceof Point) { drawPoint((Point) gp); } else if (gp instanceof Line) { drawLine((Line) gp); } else if (gp instanceof Rectangle) { drawRectangle((Rectangle) gp); } else if (gp instanceof Oval) { drawOval((Oval) gp); } else if (gp instanceof Curve) { } else if (gp instanceof PolyLine) { drawPolyLine((PolyLine)gp); }else { System.out.println("Canvas: I don't know how to draw a " + gp.getClass()); } if (gp.isSelected()) { for (HandlePoint hp : gp.getHandlePoints()) { drawHandlePoint(hp); } } } } catch (ArrayIndexOutOfBoundsException e) { ... } g.drawImage(getImage(), 0, 0, null); } public void cleanRaster() { WritableRaster wr = raster.createCompatibleWritableRaster(); raster.setDataElements(0,0, wr); }
I can see the line as a I draw it. And when it is a large line, and I move the mouse, I can notice that this algorithm is pretty "heavy".
How should I do to improve this methods?
-
Why? Why not use the standard Graphics, or better, Graphics2D methods here?
Again, I'm afraid that it begs the question of "why"?I think that the best approach would be to implement my own Graphics2D class, but that would be a lot of work and a lots of abstract methods that I don't know how to implement.
In my experience, most initialization code is not called from the paintComponent method. Rather non-drawing code does the initialization, and then calls repaint to ask the graphics methods to display it.So, to make this a little bit easier, I'm extending a JPanel, and there, I'm drawing my primitives. The basic structure for the drawing is this:
I've no idea what your code is doing, perhaps someone else other than you does.Java Code:try { for (GraphicPrimitive gp : getImageController().getGraphicPrimitives()) { if (gp instanceof Point) { drawPoint((Point) gp); ......
Have you tried to maximize the offloading of non-painting routines out of the paint/paintComponent methods?Particulary, the cleanRaster() method is not efficient.
I can see the line as a I draw it. And when it is a large line, and I move the mouse, I can notice that this algorithm is pretty "heavy".
How should I do to improve this methods?
The best way to find the bottle necks is to use a profiler. Have you done this yet?
- 09-20-2009, 04:41 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
Additional info
Thank you for your answer!
The thing is, this will be a little CAD for the University, and they focus on how the drawing methods are implemented. Of course using Graphcis2D it's much easier! But I have to do it painting pixel by pixel. By the way, I'm obtaining a WritableRaster from a BufferedImage, and then painting the pixel with the setPixel(...) method.Why? Why not use the standard Graphics, or better, Graphics2D methods here?
This line of code is going to be out of here. I was having some troubles at the beginning when trying to initialize the raster, so I directly put that line there, knowing that it wasn't the best place to do it. I could move forward at least with the rest of the implementations.In my experience, most initialization code is not called from the paintComponent method. Rather non-drawing code does the initialization, and then calls repaint to ask the graphics methods to display it.
I have. And that's why I'm particullary asking about my cleanRaster() method. That was a solution to see only the things that I'm drawing at the moment, but I don't know if it is the best way to do it.The best way to find the bottle necks is to use a profiler. Have you done this yet?
Thank you again for your answer.
- 09-24-2009, 03:59 PM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Clean your code up, get this part out of the paintComponent method.
Besides, where do you think an ArrayIndexOutOfBoundsException can be thrown?
Java Code:try { for (GraphicPrimitive gp : getImageController().getGraphicPrimitives()) { if (gp instanceof Point) { drawPoint((Point) gp); } else if (gp instanceof Line) { drawLine((Line) gp); } else if (gp instanceof Rectangle) { drawRectangle((Rectangle) gp); } else if (gp instanceof Oval) { drawOval((Oval) gp); } else if (gp instanceof Curve) { } else if (gp instanceof PolyLine) { drawPolyLine((PolyLine)gp); }else { System.out.println("Canvas: I don't know how to draw a " + gp.getClass()); } if (gp.isSelected()) { for (HandlePoint hp : gp.getHandlePoints()) { drawHandlePoint(hp); } } } } catch (ArrayIndexOutOfBoundsException e) { ... }I die a little on the inside...
Every time I get shot.
- 09-24-2009, 05:41 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
The ArrayOutOfBoundsException could be thrown inside any of the draw methods, since they are writing directly to the raster. It's just a workaround because I'm controlling if the user wants to draw a graphic primitive outside the painting area.
What do you advice me to do to draw my primitives everytime needed? How can I do that putting that code away from the paintComponent method?
- 09-24-2009, 06:25 PM #6
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
just put drawAll(); or so instead of that code, and copy the code to that method. ;) Looks cleaner. :p
I die a little on the inside...
Every time I get shot.
- 09-24-2009, 07:26 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
Thank you! I thought of doing something like that at the beginning. But I didn't because that is the only thing that the method paintComponent does. Anyway, I will take your advice into account.
What I'm most worried about now is cleaning the raster in an effective way...
- 09-24-2009, 11:10 PM #8
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Have a look at the source code of 'raster' (that's how you named it, I'm not sure what the class is called), see what it does, and try to improve it. Just check the elapsed time clearing the raster the api way and your own way (System.currentTimeMillis()).
You'll find something better. And if you won't, you could use something like buffers, i spose. Not sure, i'm not really into rasters.I die a little on the inside...
Every time I get shot.
Similar Threads
-
Help me with graphics
By 7oclock in forum New To JavaReplies: 12Last Post: 04-04-2009, 11:20 PM -
Help with 2d graphics please
By xbox_nutter in forum New To JavaReplies: 0Last Post: 04-02-2009, 11:48 AM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
graphics
By Joe2003 in forum Advanced JavaReplies: 4Last Post: 01-18-2008, 07:44 PM -
Graphics
By feniger in forum New To JavaReplies: 1Last Post: 12-29-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks