Search code examples
javaswingappletrenderingjapplet

JAVA: background rendeing over the player?


I'm building my first very basic game in JAVA without using any other external libraries. The problem I got is You can see that the background is rendered over player how can I fix that

You can see that the background is rendered over player how can I fix that The player rendering code is as follows

@Override
    public void render(Graphics g) {
        int bulletcount = 0;
        //render player
        g.setColor(color);
        g.fillRect(x,y,32,32);
        if(shooting) {
            handler.add(new bullet(x + 8, y - 24, ID.bullet, handler));
            bulletcount++;
        }
    }

and the background render code is as follows.

public class background extends GameObject{
    private int width;
    private Random random = new Random();
    public background(int x, int y , ID id,Handler handler){
        super(x,y,id,handler);
        valy = 2;
        width = getWidth(x,800);
    }
    public void tick() {
        y += valy;
        if(y>650)
            handler.remove(this);
    }
    public void render(Graphics g) {
        g.setColor(Color.white);
        g.fillRoundRect(x,y,width,10,1,1);
    }

    public int getWidth(int x,int width1){
        int wid;
        while(true){
            if((width - x) > 35) {
                wid = random.nextInt((width-x-35));
                break;
            }
            else{
             x -= 10;
            }
        }
        return wid;
    }
}

the main class of game can see the order of flow of execution.

 private void render() {
    // render the Game
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null)
        {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0,0,WIDTH,HEIGHT);
        handler.render(g);
        hod.render(g);    //handler is rendering every object in game
        g.dispose();
        bs.show();
    }

    private void tick() {
    //update Game
        handler.tick();
        hod.tick();
        spawner.tick(); // background is generating in spawner class.
    }

I know that telling the problem with this much less code is hard for you all. But I have complete faith in this large and powerful community.

Thanks in advance. Don't hesitate to edit and point out my mistakes Thanks!


Solution

  • I just had solution! The problem was in handler.render(g); method:- which is a follows;

        private void render(Graphics g){
          for(int i =0; i<gameobj.size(); i++){//gameobj is linkedlist of GameObject
             GameObject tempobj = gameobj.get(i);
             tempobj.render(g);//getting every object and rendering them!
    }
    

    The problem was in loop the first object added in the game is player.So , it was getting render first after that the background was getting rendered. It was a silly mistake.which got right by following change.->

    private void render(Graphics g){
      for(int i = gameobj.size()-1; i > 0; i--){//gameobj is linkedlist of GameObject
         GameObject tempobj = gameobj.get(i);
         tempobj.render(g);//getting every object and rendering them! from reverse
    }
    

    so, the last object renders first and the first object renders last. Peace!