1 year ago

#282015

test-img

kevlar_224

how come my back ground image is placed over all my objects java swing

I am working on an ice hockey game and my background image is being displayed over top of the objects(puck and 2 players) I know that all the objects are there because I can hear the music play when the puck hits one of the paddles

//in Game_Panel class
private BufferedImage img;
Game_Panel(BufferedImage img){

        this.image = img;
        this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
        this.setFocusable(true);
        this.addKeyListener(new AL());

        newPlayer();
        newOpponent();
        newPuck();
        score = new Score(SCREEN_WIDTH, SCREEN_HEIGHT);
        gameThread = new Thread(this);
        gameThread.start();
        new Music("Music/background.wav").playBackground();
    }
    public void paint(Graphics g){
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);

    }

//in Game_Frame calss
  private BufferedImage img;
       Game_Frame(){
           importImg();
        
        panel = new Game_Panel(img);
        this.add(panel);

        this.setTitle("Air Hockey");
        this.setResizable(false);
        this.setBackground(Color.white);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
        this.setLocationRelativeTo(null);

    }

    private void importImg() {

        try {
            img = ImageIO.read(new FileInputStream("GameImage/rink.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

java

swing

jframe

jpanel

bufferedimage

0 Answers

Your Answer

Accepted video resources