Skip to main content

Thread: Java Drawing Multiple Images


i’m attempting create simple point-n-click adventure game in java. i’m trying fundamental aspect of down drawing images on screen. scenario going same time whereby need draw background image , several other images (characters etc) on top of background. later on need move character images, imagine involve needing draw background again followed same character images moved slightly.

i working under idea of using ‘graphic2d’ , that’s why see popup in code below. original idea load image , add jlabel. use setlocation() position image in frame.

code:
public static graphics2d graphic = loadimg("images/backdrops/outside1.png").creategraphics();      //graphic.setrenderinghint(renderinghints.key_antialiasing,renderinghints.value_antialias_on);          //create graphic hold images displayed on jframe.       public static void main(string[] args)           {           jframe window = new jframe();           window.setdefaultcloseoperation(jframe.exit_on_close);           window.setsize(1000, 625);           window.setvisible(true);              //setup window using swing            window.getcontentpane().add(drawimg("images/backdrops/outside1.png",500,0));           //window.getcontentpane().add(drawimg("images/characters/test.png",500,0));           }       public static jlabel drawimg(string imageloc, int x, int y)          {         bufferedimage image = loadimg(imageloc);          //graphic.drawimage(image, x, y, null);         //graphic.dispose(); //!          jlabel readylabel = new jlabel(new imageicon(image));         readylabel.setlocation(x,y); //doesn't seem maneuver image?         return readylabel;         }       public static bufferedimage loadimg(string imgpath)               {               bufferedimage buffer = null;               try                  {                   buffer = imageio.read(new file(imgpath));                  }               catch (exception e){}               return buffer;               }

setlocation() not place image specified. there problem of displaying 1 image @ time. how can accomplish these 2 things? need use either graphic2d or canvas?

hi kodeine,

suggest "setlocation" location function does. sets location of component *in layout*, , not in pixels. if layout contains 1 element, doesn't matter if put element @ position (500,0) or (0,0) - other layout cells not used.

if want fine-grained control of stuff drawn, need paint on own. either derive jpanel , override paining methods, *or* allocate bufferedimage of correct size , draw *into* image want draw. there examples on web. after finishing bufferedimage, can convert imageicon , assign jlabel. approach of doing things used case want display doesn't change often.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Java Drawing Multiple Images


Ubuntu

Comments

Popular posts from this blog

Some mp4 files not displaying correctly (CS6)

Thread: Samba is not authenticating with LDAP