How to set Borders graphics G JAVA -


i trying make game there few borders on map, being generated text document. text document has 1s , 0s ther 1 shows wall. how make character stops infront of wall

my code:

main class: public class javagame {

public static void main(string[] args) {     final platno p = new platno();     final jframe okno = new jframe("test");     mapa map = new mapa();     okno.setresizable(false);     okno.setdefaultcloseoperation(jframe.exit_on_close);     okno.setsize(800, 600);     okno.setvisible(true);      map.nacti();     okno.add(p);      p.mapa = map;       okno.addkeylistener(new keylistener() {          @override         public void keytyped(keyevent e) {         }          @override         public void keypressed(keyevent e) {           int kod = e.getkeycode();           if(kod == keyevent.vk_left)           {               platno.x -= 3;               p.repaint();           }           else if(kod == keyevent.vk_right)           {               platno.x +=3;               p.repaint();           }           else if(kod == keyevent.vk_up)           {               platno.y -=3;               p.repaint();           }           else if(kod == keyevent.vk_down)           {               platno.y +=3;               p.repaint();           }         }          @override         public void keyreleased(keyevent e) {         }     });   /*   timer = new timer(1, new actionlistener() {          @override         public void actionperformed(actionevent e)         {             platno.y -=3;             p.repaint();         }     }); */ }` 

map loader class:

public void nacti() {     try (bufferedreader br = new bufferedreader(new filereader("map1-1.txt")))     {         string radek;         int cisloradku = 0;          while((radek = br.readline()) != null)         {             for(int = 0; < radek.length(); i++)             {                 char znak = radek.charat(i);                 int hodnota = integer.parseint(string.valueof(znak));                 pole[i][cisloradku] = hodnota;             }             cisloradku++;         }     }     catch (exception ex)     {         joptionpane.showmessagedialog(null, "error: "+ex.getmessage());     } }  public void vykresli(graphics g) {     try {         wall = imageio.read(classloader.getsystemresource("images/wall.gif"));     } catch (ioexception ex) {         joptionpane.showmessagedialog(null, "error: "+ex.getmessage());     }     for(int = 0; < pole[0].length; i++)     {         for(int j = 0; j < pole.length; j++)         {             if(pole[j][i] == 1)             {                 g.setcolor(color.red);                     //  g.fillrect(j*40, i*40, 40, 40);                       g.drawimage(wall, j*40, i*40, null);             }         }     } }` 

and hero class:

public class hero { public int pozicex; public int pozicey; public static boolean upb = false; public static boolean downb = false; public static boolean rightb = false; public static boolean leftb = false; bufferedimage up; bufferedimage down; bufferedimage right; bufferedimage left;  public hero() {     try {         = imageio.read(classloader.getsystemresource("images/hero_up.png"));         down = imageio.read(classloader.getsystemresource("images/hero_down.png"));         right = imageio.read(classloader.getsystemresource("images/hero_right.png"));         left = imageio.read(classloader.getsystemresource("images/hero_left.png"));     } catch (ioexception ex) {         joptionpane.showmessagedialog(null, "error: "+ex.getmessage());     } }  public void vykreslit(graphics g) {     if(upb == true)     {         g.drawimage(up, pozicex, pozicex, null);     }     else if(downb == true)     {         g.drawimage(down, pozicex, pozicex, null);     }     else if(leftb == true)     {         g.drawimage(left, pozicex, pozicex, null);     }     else if(rightb == true)     {         g.drawimage(right, pozicex, pozicex, null);     } }` 

thanks :)

you calculate 'future position' move, , test collisions. if collision occur, dont'move, otherwise you're ok move.

see if logic can you:

public boolean willcollide(int row, int col, board map) {     return map[row][col] == 1; }  public void moveleft(hero hero, board map) {   //watch index out of bounds!   int futurecol = hero.y - 1;   if (! willcollide(hero.row, futurecol)     hero.y = futurecol; } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -