Results 1 to 7 of 7
- 01-14-2011, 12:38 PM #1
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
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.
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?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; }
- 01-14-2011, 02:23 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-14-2011, 08:20 PM #3
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
- 01-15-2011, 07:19 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-15-2011, 02:21 PM #5
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?
- 01-15-2011, 02:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-15-2011, 03:03 PM #7
Similar Threads
-
Render WMF, EMF into Raster Graphics Format & Convert WMF to PNG
By sherazam in forum Java SoftwareReplies: 0Last Post: 10-21-2010, 11:09 AM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks