Results 1 to 7 of 7
  1. #1
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default Can this be done better with the Raster class? Or some other way?

    Hi, I have an application that creates a load of grids on a panel, they are interlinked rectangles, and portions of them have uniform size, but then there are other grids with a different size and they can have different background colours. I decided to write this method that draws one of the grids, and it does just what I want.

    Java Code:
        /**
         * Generates an image which looks like a rectangular grid
         * @param cellWidth the width of the cells in pixels; one pixel is taken up with the border
         * @param cellHeight the height of the cells in pixels; one pixel is taken up with the border
         * @param cellsWide the number of cells across (number of columns)
         * @param cellsHigh the number of cells down (number of rows)
         * @param background the background colour
         * @return a new image meeting the requirements, drawn with black lines
         */
        public static Image GridImage(byte cellWidth, byte cellHeight, byte cellsWide, byte cellsHigh, Color background) {
            int widthPixels = cellWidth*cellsWide+1;
            int heightPixels = cellHeight*cellsHigh+1;
            BufferedImage bi = new BufferedImage(widthPixels, heightPixels, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.getGraphics();
            g2d.setBackground(background);
            g2d.clearRect(0, 0, widthPixels, heightPixels);
            g2d.setColor(Color.BLACK);
            for(byte i=0; i<=cellsWide; i++) {
                int x = i*cellWidth;
                g2d.drawLine(x, 0, x, heightPixels-1);
            }
            for(byte i=0; i<=cellsHigh; i++) {
                int y = i*cellHeight;
                g2d.drawLine(0, y, widthPixels-1, y);
            }
            return bi;
        }
    I have looked into the Raster class but it looks kinda complicated. Can any of you people who have experience in it tell me if it would make the above code more elegant, faster or better?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Paul Richards View Post
    Can any of you people who have experience in it tell me if it would make the above code more elegant, faster or better?
    It looks fine to me; no further questions your honour ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You could move the declaration of x and y outside the loop to prevent a lot of extra garbage collection (reassignment vs recreation) which would turn your code up one notch - to 11.

    :D

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    You could move the declaration of x and y outside the loop to prevent a lot of extra garbage collection (reassignment vs recreation) which would turn your code up one notch - to 11.

    :D
    Java doesn't work that way: whether or not you take those variable out of the loop they are stored on the stack and there is nothing to garbage collect for those primitives (check it with javap -c). Taking those variables out of the loop will take a bit more stack space until the end of their (larger) scope.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Oh really? So, if you put a declaration in a loop, it doesn't add (and then mark for garbage collection) a variable to the stack every cycle of the loop? i.e. Executing a declaration in a loop doesn't add n number of variables to the stack?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    Oh really? So, if you put a declaration in a loop, it doesn't add (and then mark for garbage collection) a variable to the stack every cycle of the loop? i.e. Executing a declaration in a loop doesn't add n number of variables to the stack?
    Here x and y are primitives (ints) so there is nothing to be garbage collected; they exist on the stack and the stack is there (same when they were defined outside of the loop). Defining (allocating) an object variable inside of the loop (and forgetting about it at the end of the loop body) is an entirely different cup of tea; it doesn't matter for the references to it, i.e. they can be defined in the loop body without a problem.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Ah, good point - for some reason I wasn't thinking about the fact that they were primitive values (not objects!). Very good point, and this back and forth should prove very useful to others.

    Cheers!

Similar Threads

  1. Replies: 0
    Last Post: 10-21-2010, 11:09 AM
  2. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  3. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  4. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Posting Permissions

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