swing - 2D Java Game. Moving sprite above tiled images -


->short introduction, can skip part

i'm glad able post on platform, because myself have gained knowledge through community; reading. wanted "hello everybody, , thank you!

actual content(prologue):

though i'm developing in objective-c in company, utterly interested in java development. quite comfortable syntax have major troubles awt/swing/graphics2d.

the concept:

java application 800*600 frame. on frame can see 16*12 tiles (i.e. gras.jpg or tree.jpg) size of 50px². above frame .png "player" moving. field generated 2 dimensional int array[16][12] 0 symbolizes "gras" , 1 means "tree".

-> "working".

first try:

adding (frame.add(...)) 16*12 jlabels imageicon "gras" or "tree" jlayeredpane
adding (frame.add(...)) class "entitylayer" wich updated @ 5ms paint(graphics g)
{
g.drawimage(imageicon.getimage());
}
this version "works" if add either field tiles or entity layer. in every case else either entitylayer overlays field grey background or "player" not visible under field;

second try:

making drawings in entitylayer paint(graphics g) method. thats not intended. want field drawn once , "player" with, translucent background, drawn above field.

question:

how can myself in position have 2 seperate layers, both equally independent out having 1 overlapping other? im 99% shure attempt wrong in way.

thank right away.

instead of adding/drawing 16*12 jlabels, draw tile image 16*12 times?

for things this, add single jpanel jframe, , override jpanel's paint() method. actually, don't add jpanel, add subclass of jpanel have created, should same, show need add field jpanel , override paint method

your background tile drawing code might similar to:

int tilewidth = 50; int tileheight = 50; ( int x = 0; x < 16; x++ ) {     ( int y = 0; y < 12; y++ )     {         image tileimage;         int tiletype = fieldarray[x][y];          switch ( tiletype )         {             case 0:             {                 tileimage = mygrassimage;                 break;             }             case 2:             {                 tileimage = mytreeimage;                 break;             }         }          graphics2d g;         g.drawimage(tileimage, x * tilewidth, y * tileheight, null);     } } 

a technique works me separate drawing logic of each layer separate class implements painter interface (or similar interface define).

public class tilepainter implements painter {      @override     public void paint( graphics2d g, object thepanel, int width, int height )     {         //the tile painting code posted above go here         //note may need add constructor class         //that provides references needed resources         //eg: images/arrays/etc         //or provides reference object resources can accessed     }  } 

then player painter:

public class entitypainter implements painter {      @override     public void paint( graphics2d g, object thepanel, int width, int height )     {         g.drawimage(player.getimage(), player.getx(), player.gety(), null);     }  } 

if wondering why passing nulls g.drawimage() function, because overloaded function call last parameter imageobserver, not need purpose.

ok, once have painters separated different classes need make jpanel capable of using them!

this field need add jpanel:

list<painter> layerpainters; 

your constructor should this:

public myextendedjpanel()     {         //i use arraylist because keep painters in order         list<painter> layerpainters = new arraylist<painter>();          tilepainter tilepainter = new tilepainter(does,this,need,args);         entitypainter entitypainter = new entitypainter(does,this,need,args);          //layers painted in order added         layerpainters.add(tilepainter);         layerpainters.add(entitypainter);      } 

now last important part:

@override public void paint( graphics g ) {     int width = getwidth();     int height = getheight();     //loops through layers in order added     //and tells them paint onto panels graphic context     for(painter painter : layerpainters)     {         painter.paint(g,this,width,height);     } } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -