Results 1 to 8 of 8
  1. #1
    diegofsza is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Cool 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:

    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);
        }
    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by diegofsza View Post
    Hi, I'm doing a program to draw graphics primitives (lines, rectangles, etc...) with my own implementations.
    Why? Why not use the standard Graphics, or better, Graphics2D methods here?
    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.
    Again, I'm afraid that it begs the question of "why"?
    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:
    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.

    Java Code:
            try {
                for (GraphicPrimitive gp : getImageController().getGraphicPrimitives()) {
                    if (gp instanceof Point) {
                        drawPoint((Point) gp);
                    ......
    I've no idea what your code is doing, perhaps someone else other than you does.

    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?
    Have you tried to maximize the offloading of non-painting routines out of the paint/paintComponent methods?
    The best way to find the bottle necks is to use a profiler. Have you done this yet?

  3. #3
    diegofsza is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default Additional info

    Thank you for your answer!

    Why? Why not use the standard Graphics, or better, Graphics2D methods here?
    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.

    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.
    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.

    The best way to find the bottle necks is to use a profiler. Have you done this yet?
    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.

    Thank you again for your answer.

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.

  5. #5
    diegofsza is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    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?

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.

  7. #7
    diegofsza is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    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...

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  1. Help me with graphics
    By 7oclock in forum New To Java
    Replies: 12
    Last Post: 04-05-2009, 12:20 AM
  2. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 12:48 PM
  3. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 01:49 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 08:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 05:22 PM

Tags for this Thread

Posting Permissions

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