graphics - Java 2D Rendering Ripple Artifacts -
i'm working on isometric 2d game in java , noticed there grid of lines on screen rendering seems repeat pixel horizontally , vertically. here's screenshot show mean:
i checked loop in render method , compared each pixel's color value previous value in array see if there occurrence of 2 black pixels in row , no such occurrence found. i'm @ loss causing effect. can fix this? here's relevant code:
game class variables:
private bufferedimage m_imgimage = new bufferedimage( intcanvas_width, intcanvas_height, bufferedimage.type_int_rgb ); private int[ ] m_aintpixels = ( ( databufferint ) m_imgimage.getraster( ).getdatabuffer( ) ).getdata( );
game class render method:
// object organize data on canvas bufferstrategy bfsbufferstrategy = getbufferstrategy( ); if( bfsbufferstrategy == null ) { createbufferstrategy( 3 ); return; } for( int inty = 0; inty < intcanvas_height; inty += 1 ) { for( int intx = 0; intx < intcanvas_width; intx += 1 ) { m_aintpixels[ intx + ( inty * intcanvas_width ) ] = m_screen.m_aintpixels[ intx + ( inty * m_screen.m_intwidth ) ]; } } graphics gfxgraphics = bfsbufferstrategy.getdrawgraphics( ); gfxgraphics.drawimage( m_imgimage, 0, 0, getwidth( ), getheight( ), null ); gfxgraphics.dispose( ); bfsbufferstrategy.show( );
screen class render method populate pixel array:
// render tile specified tile id public void rendertile( int intpositionx, int intpositiony, cspritesheet spritesheet, int inttileid ) { int intpixelindex = 0; int intsheetx = 0; int intsheety = 0; int inttileoffsetx = 0; int inttileoffsety = 0; int intpixelcolor = 0; // center tile intpositionx -= m_intoffsetx; intpositiony -= m_intoffsety; intsheetx = ( inttileid % spritesheet.m_inttilecolumns ) * ctile.tile_width; intsheety = ( inttileid / spritesheet.m_inttilerows ) * ctile.tile_height; intpixelindex = intpositionx + ( intpositiony * m_intwidth ); // rows for( int inttilerow = intsheety; inttilerow != ( intsheety + ctile.tile_height ); inttilerow += 1 ) { // columns for( int inttilecolumn = intsheetx; inttilecolumn != ( intsheetx + ctile.tile_width ); inttilecolumn += 1 ) { intpixelcolor = spritesheet.m_aintpixels[ inttilecolumn + ( inttilerow * m_spritesheet.m_intwidth ) ]; // boundary checking if( intpositionx + inttileoffsetx >= 0 && intpositionx + inttileoffsetx < m_intwidth && intpositiony + inttileoffsety >= 0 && intpositiony + inttileoffsety < m_intheight && ccolor.transparent( intpixelcolor ) == false ) { m_aintpixels[ intpixelindex ] = spritesheet.m_aintpixels[ inttilecolumn + ( inttilerow * m_spritesheet.m_intwidth ) ]; } // increment inttileoffsetx += 1; intpixelindex += 1; } // increment inttileoffsety += 1; intpixelindex += m_intwidth - ( ctile.tile_width ); inttileoffsetx = 0; } }
this classic artifact of non-interpolated scaling. have:
private bufferedimage m_imgimage = new bufferedimage( intcanvas_width, intcanvas_height, bufferedimage.type_int_rgb ); ... gfxgraphics.drawimage( m_imgimage, 0, 0, getwidth( ), getheight( ), null );
the effect seeing because getwidth()
, getheight()
larger intcanvas_width
, intcanvas_height
. when draw graphic, scales larger. it's not using kind of interpolation scaling, picking nearest pixel source; therefore see duplicate rows or columns @ regular intervals.
you not see when check source data because not present in source data, it's artifact introduced in output when data rendered.
print out image , panel dimensions , see difference. judging count of duplicate rows see (i see 10 horizontal , 10 vertical), guess find getwidth()
10 more intcanvas_width
, , same height.
you pass dimensions of m_imgimage
.drawimage()
instead. rid of artifact leave gap on right , bottom since image smaller component.
if graphics
graphics2d
, can set key_interpolation
rendering hint use interpolation when scaling (it may not obey hint):
graphics2d g = (graphics2d)gfxgraphics; g.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear);
if want image drawn exactly, without scaling, , without gap on sides, disable resizing on main window , set size match image.
Comments
Post a Comment