java - Making scrolls in GridBagLayout -


good time of day, i'm trying add scrolls gridbaglayout missing correct way it.

the code:

public gui(map map) {     //declare image icons , try read them     imageicon missing = new imageicon();     imageicon wall = new imageicon();     imageicon floor = new imageicon();     //set texture missing cases     try     {         image tempimage = imageio.read(this.getclass().getresource("/resources/images/missing.png"));         missing = new imageicon(tempimage.getscaledinstance(16, 16, image.scale_default));     }     catch(exception e)     {         e.printstacktrace();     }     try     {         image tempimage = imageio.read(this.getclass().getresource("/resources/images/wall.png"));         wall = new imageicon(tempimage.getscaledinstance(16, 16, image.scale_default));         tempimage = imageio.read(this.getclass().getresource("/resources/images/floor.png"));         floor = new imageicon(tempimage.getscaledinstance(16, 16, image.scale_default));     }     catch(exception e)     {         e.printstacktrace();     }     container pane = getcontentpane();     pane.setlayout(new gridbaglayout());     gridbagconstraints c = new gridbagconstraints();     (int i=0;i<map.getmapsize();i++)     {         (int j=0;j<map.getmapsize();j++)         {             c.gridy=i;             c.gridx=j;             if (i==0 && j==0)             {                 c.weightx=1;                 c.weighty=1;             }             else             {                 c.weightx=0;                 c.weighty=0;             }             c.gridwidth=1;             c.gridheight=1;             c.fill = gridbagconstraints.both;             jlabel tile = new jlabel(missing);             if (map.getelementat(i, j)==0)             {                 tile.seticon(wall);             }             else             {                 tile.seticon(floor);             }             pane.add(tile, c);         }     }     //c.gridx=map.getmapsize();     jscrollpane thepane = new jscrollpane();     pane.add(thepane, c);      settitle("game gui");     setsize(640, 480);     setvisible(true);     setdefaultcloseoperation(exit_on_close); } 

as far understand adding jscrollpane after filling pane incorrect , of course fails. question is: how add them correctly in case? aim overall i'm trying achieve able scroll (move) map in game if exceeds game window size. in advance:)

i guess issue try add gridbagconstraints jscollpane instead of adding jpanel....

jscrollpane thepane = new jscrollpane(); pane.add(thepane, c); 

try this:

jpanel mymappanel = new jpanel(); mymappanel.setlayout(new gridbaglayout()); // add stuff mymappanel here (do looping stuff here) jscrollpane thepane = new jscrollpane(mymappanel); pane.add(thepane, c); 

take care of this:

new jscrollpane(mymappanel); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -