📄 fasthexavaluegridportrayal2d.java
字号:
// Some history here. We initially started by using setRGB in BufferedImage. // But based on some hints, we dug down. First we grabbed the Raster and // used setDataElements, which gave us a big speed boost. // Now we're digging even further down and grabbing the data buffer, which // we know is a DataBufferInt, using a scanlineStride as shown. The DataBufferInt // docs show how to compute which value in the (one-dimensional) data buffer to // poke in order to set the equivalent pixel value. This gives us a slight // improvement. We've since deleted the setRGB code, but we've kept the more // readable setDataElements code in comments because the scanlineStride code // is so illegible. :-) // // FastValueGridPortrayal2D has a significantly faster method still: directly setting elements // in the data array. But while this works great for rectangular regions, it's // quite complex to do with hexagonal stuff so instead we're just setting the // various pixels via setElem as we go. // // Apparently for opaque BufferedImages in Sun implementations (Windows, etc.), // there's a new "Managed Buffer" notion -- if you don't extract the buffer, they // do what they can to make things as fast for you as possible -- but it doesn't // help in the case of transparent images. If we add a transparency constructor, // we should undo the dbuffer code and go back to the raster code and see if that's // faster in Windows in 1.4.2 and on. See // http://weblogs.java.net/blog/chet/archive/2003/08/bufferedimage_a_1.html int load; int scanlineStride = ((SinglePixelPackedSampleModel)(raster.getSampleModel())).getScanlineStride(); if (isDoubleGrid2D) for(int x=sx;x<ex;x++) for(int y=sy;y<ey;y++) { if((x&1)==0) { // btw, setting each pixel separately is faster // than setting a 1x2 pixel grid with setDataElements! load = map.getRGB(doubleField[x][y]); _dbuffer.setElem((2*y)*scanlineStride + x, load); _dbuffer.setElem((2*y+1)*scanlineStride + x, load);// load[0] = map.getRGB(doubleField[x][y]);// _raster.setDataElements(x,2*y,load);// _raster.setDataElements(x,2*y+1,load); } else { load = map.getRGB(doubleField[x][y]); _dbuffer.setElem((2*y+1)*scanlineStride + x, load); _dbuffer.setElem((2*y+2)*scanlineStride + x, load);// load[0] = map.getRGB(doubleField[x][y]);// _raster.setDataElements(x,2*y+1,load);// _raster.setDataElements(x,2*y+2,load); } } else for(int x=sx;x<ex;x++) for(int y=sy;y<ey;y++) { if((x&1)==0) { load = map.getRGB(intField[x][y]); _dbuffer.setElem((2*y)*scanlineStride + x, load); _dbuffer.setElem((2*y+1)*scanlineStride + x, load);// load[0] = map.getRGB(intField[x][y]);// _raster.setDataElements(x,2*y,load);// _raster.setDataElements(x,2*y+1,load); } else { load = map.getRGB(intField[x][y]); _dbuffer.setElem((2*y+1)*scanlineStride + x, load); _dbuffer.setElem((2*y+2)*scanlineStride + x, load);// load[0] = map.getRGB(intField[x][y]);// _raster.setDataElements(x,2*y+1,load);// _raster.setDataElements(x,2*y+2,load); } } } // MacOS X 10.3 Panther has a bug which resets the clip, YUCK // graphics.setClip(clip); graphics.drawImage(buffer, (int)(info.draw.x+translateWidth), (int)info.draw.y, (int)(maxX*scaleWidth), (int)info.draw.height,null); } else { buffer = null; // GC the buffer in case the user had changed his mind if (endx > maxX) endx = maxX; if (endy > maxY) endy = maxY; if( startx < 0 ) startx = 0; if( starty < 0 ) starty = 0; final int ex = endx; final int ey = endy; final int sx = startx; final int sy = starty; int _x = 0; int _y = 0; int _width = 0; int _height = 0; // locals are faster... final ColorMap map = this.map; final double infodrawx = info.draw.x; final double infodrawy = info.draw.y; // 1.3.1 doesn't hoist -- does 1.4.1? if (isDoubleGrid2D) for(int x=sx;x<ex;x++) for(int y=sy;y<ey;y++) { final Color c = map.getColor(doubleField[x][y]); if (c.getAlpha() == 0) continue; _x = (int)(translateWidth + infodrawx + scaleWidth * x); _y = (int)(infodrawy + (yScale) * ((x&1)==0?2*y:2*y+1)); _width = (int)(translateWidth + infodrawx + scaleWidth * (x+1)) - _x; _height = (int)(infodrawy + (yScale) * ((x&1)==0?2*y+2:2*y+3)) - _y; // draw // MacOS X 10.3 Panther has a bug which resets the clip, YUCK // graphics.setClip(clip); if( graphics!=null ) { graphics.setColor(c); graphics.fillRect(_x,_y,_width,_height); } else { if( info.clip.intersects(_x,_y,_width,_height) ) putInHere.add(getWrapper((doubleField[x][y]), x, y)); } } else for(int x=sx;x<ex;x++) for(int y=sy;y<ey;y++) { final Color c = map.getColor(intField[x][y]); if (c.getAlpha() == 0) continue; _x = (int)(translateWidth + infodrawx + scaleWidth * x); _y = (int)(infodrawy + (yScale) * ((x&1)==0?2*y:2*y+1)); _width = (int)(translateWidth + infodrawx + scaleWidth * (x+1)) - _x; _height = (int)(infodrawy + (yScale) * ((x&1)==0?2*y+2:2*y+3)) - _y; // draw // MacOS X 10.3 Panther has a bug which resets the clip, YUCK // graphics.setClip(clip); if( graphics!=null ) { graphics.setColor(c); graphics.fillRect(_x,_y,_width,_height); } else { if( info.clip.intersects(_x,_y,_width,_height) ) putInHere.add(getWrapper((intField[x][y]), x, y)); } } } // finally, clear dirty flag if we've just drawn (don't clear if we're doing hit testing) if (graphics!=null) dirtyField = false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -