swing - Java draw Image in wrong position, Clip Region position is wrong -


when see application, draws image not on (0,0) somewhere middle in program, debug program,and saw clipregion.lox 456 , loy 130 , location program draw (0,0). think clip region position wrong, don't know how fix it. please?

code:

public void changestate(int bef,int cur){     if(bef==1){         if(cur==2){             intro.setvisible(false);             this.setcontentpane(play);             play.init();             play.setvisible(true);         }     } } 

this when first panel started. in play.init() set socket connect server program, , start sound file. all.

 public void paintcomponent(graphics g){     super.paintcomponent(g);     this.setbackground(new color(255,255,255));     g.drawimage(backgroundimg, 0, 0, this);     g.drawimage(player, 368, 280, this);     for(int i=0;i<60;i++){         for(int j=0;j<100;j++){             if(map[i][j]==1){                 g.drawimage(stone, (j-10)*32-dx, (i-10)*32, this);             }             else if(map[i][j]==2){                 if(i>0&&map[i-1][j]==0) g.drawimage(grass, (j-10)*32-dx, (i-10)*32, this);                 else g.drawimage(dirt, (j-10)*32-dx,(i-10)*32,this);             }         }     }     g.drawstring(servermessage, 320, 200); } 
  • i erased println code , thread sleep code. debugging , when deleted however, nothing changed.

plus, repainted it, , draws in (0,0) doesn't draw full screen.

i think drawn image same size (456,130) (800,600) think picture cut out.

i can't post picture because of low reputation... too?

this.setbackground(new color(255,255,255)); 

don't set background in painting method. set background in constructor of class.

it draws image not on (0,0) somewhere middle in program, g.drawimage(backgroundimg, 0, 0, this);

no, image drawn @ (0, 0) relative location of panel. mean panel not being painted @ (0, 0) relative frame. because of layout manager using positioning panel in center of frame. check layout code.

i saw clipregion.lox 456 , loy 130 ,

if think clipping area wrong may because panel not have preferred size. whenever custom painting should override getpreferredsize() method return size of panel layout manager can job properly. maybe preferred size should size of image?

read section swing tutorial on custom painting more information , working examples.


Comments