how to make 2d graphic in java without repaint()? -
i want draw rectangles in different timesets, when draw first rectangle paint(), use paint() draw second 1 first rectangle disappear. found image override previos one. how can without recreating previous ones(like not using repaint())? appreciate every answer:d
public static class screen extends frame { int width = 900; static int[][] map = new int[700][700]; public screen() { super("clash of tank"); setsize(width, width); addwindowlistener(new windowadapter() { public void windowclosing(windowevent e) { system.exit(0); } }); } public void paint(graphics g) { bufferedimage image = (bufferedimage) createimage(700,700); graphics g2 = image.getgraphics(); drawtank(g2); g.drawimage(image, 325, 35, this); } void drawtank(graphics g) { g.setcolor(color.black); for(int = 0; i<=690; i+=1) for(int j = 0; j<=690; j+=1) { if(map[i][j]==0) g.setcolor(color.black); if((map[i][j]==1)) g.setcolor(color.blue); if((map[i][j]==2)) g.setcolor(color.red); if((map[i][j]==3)) g.setcolor(color.green); if((map[i][j]==4)) g.setcolor(color.yellow); g.fillrect(i, j, 1, 1); } } } public class clashoftanks { public static void main(string[] args) throws exception { screen t = new screen(); t.setvisible(true); t.repaint(); } }
yours classic xy problem ask specific solution solve wrong way something.
the key should use repaint()
, should either store collection of rectangles , draw them in loop in paint
or paintcomponent
method (if using swing), or else draw them bufferedimage , draw that in painting method. don't forget call super paint or paintcomponent method.
Comments
Post a Comment