3 Replies - 317 Views - Last Post: 18 December 2013 - 04:13 PM Rate Topic: -----

#1 WalterWhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 13

How to set an image as a background in java game?

Posted 18 December 2013 - 02:46 PM

Hi, im making a game in java with 2d graphics. It uses multiple images for my spaceship, and aliens, which I can get to work just fine. But I want to set a large space image as the background the whole thing plays on, and the same process I use for the other images doesn't seem to work. I put the Image code in separate classes for Aliens and SpaceCraft . Ive already tried using the same code for the other images in the board class, but it wont load on screen. The main logic and drawing is done in a Board class here>> Any help is appreciated

package Operation.Supernova;

//import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.sound.sampled.Clip;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.ImageIcon;

public class Board extends JPanel implements ActionListener

{
    
    private String string;
    private Timer timer;
    private Craft craft;
    private Clip clip;
    private ArrayList aliens;
    private boolean ingame;
    private boolean visible;
    private int B_WIDTH = 1200;
    private int B_HEIGHT = 850;
    //file im using as background image.
    private String space = "Space.png";
    // Name given for string to differentiate from other image strings I have
    private Image bimage;
    //Alien initial positions
    private int[][] screenPos =
    { 
        {930, 425}, {888, 500}, {950, 230},
        {950, 100}, {850, 250}, {822, 730}, 
        {920, 150}, {750, 600}, {1200, 450},
        {860, 300}, {950, 650}, {1157, 260},
        {850, 250}, {1070,470}, {1023, 355},
        {750, 740}, {741, 190}, {710, 280},
        {835, 450}, {801, 310}, {995, 290},
        {750, 400}, {910, 410}, {1008, 300},
        {999, 474}, {945, 220}, {666, 310}
     
     };

    public Board() 
    {        
        ImageIcon ii = new ImageIcon(this.getClass().getResource(space));
        bimage = ii.getImage();
        visible = true;
        initAliens();  
        addKeyListener(new TAdapter());
        setFocusable(true);
        setDoubleBuffered(true);
        ingame = true;

        craft = new Craft();
        timer = new Timer(5, this);
        timer.start();
    }
    @Override
    public void addNotify() 
    {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();   
    }

    private void initAliens()
    {
        aliens = new ArrayList();

        for (int i = 0; i < screenPos.length; i++) 
        {
            aliens.add(new Aliens(screenPos[i][0], screenPos[i][1]));
        }
    }

        @Override
    public void paintComponent(Graphics g)
        {
        super.paintComponent(g);
            if (ingame)
        {
        
        Graphics2D g2d = (Graphics2D)g;
            if (craft.isVisible())
            {
                g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
            }
   
        ArrayList ms = craft.getLaser();
     
        for (int i = 0; i < ms.size(); i++ ) 
        {
           Laser m = (Laser) ms.get(i);
            g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
        }
       for (int i = 0; i < aliens.size(); i++)
        {
                Aliens a = (Aliens)aliens.get(i);
                if (a.isVisible())
                {
                    g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
                }
        }
            g2d.setColor(Color.WHITE);
            g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
        } 
            else
            {     
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
            }
        }
        
        @Override
    public void actionPerformed(ActionEvent e)
        {
            if (aliens.isEmpty())
            {
                ingame = false;
            }
  
        ArrayList ms = craft.getLaser();

        for (int i = 0; i < ms.size(); i++) {
            Laser m = (Laser) ms.get(i);
             if (m.isVisible()) 
            {
                m.move();
            }
             else 
            {
                ms.remove(i);
            }
        }
       for (int i = 0; i < aliens.size(); i++) 
       {
            Aliens a = (Aliens) aliens.get(i);
            if (a.isVisible()) 
            {
                a.move();
            }
            else
            {
                aliens.remove(i);
            }
            
        }
        
        craft.move();
        checkColl();
        repaint();   
    }
        
        public void checkColl() {

        Rectangle r3 = craft.getBounds();

        for (int j = 0; j < aliens.size(); j++) {
            Aliens a = (Aliens) aliens.get(j);
            Rectangle r2 = a.getBounds();

            if (r3.intersects(r2)) {
                craft.setVisible(false);
                a.setVisible(false);
                ingame = false;
            }
            
            ArrayList ms = craft.getLaser();

        for (int i = 0; i < ms.size(); i++)
        {
            Laser m = (Laser) ms.get(i);

            Rectangle r1 = m.getBounds();

            for (int J = 0; J < aliens.size(); J++) 
            {
                Aliens A = (Aliens) aliens.get(j);
                Rectangle R2 = a.getBounds();

                if (r1.intersects(R2))
              {
                    m.setVisible(false);
                    a.setVisible(false);
              }
           }
        }
     }
  }    
        
        public void play()
         {
         }
          
        public void stop()
         {
         }

        public void loop()
        {
        }

    private class TAdapter extends KeyAdapter {
            @Override
        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }
            @Override
        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to set an image as a background in java game?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: How to set an image as a background in java game?

Posted 18 December 2013 - 03:05 PM

Why not just g.drawImage with bimage as an argument in paintComponent as its first line?

This post has been edited by g00se: 18 December 2013 - 03:06 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 WalterWhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 13

Re: How to set an image as a background in java game?

Posted 18 December 2013 - 03:19 PM

View Postg00se, on 18 December 2013 - 03:05 PM, said:

Why not just g.drawImage with bimage as an argument in paintComponent as its first line?

sorry, im not really familiar with that way of drawing the image. Would you just specify the file name, then width and height?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: How to set an image as a background in java game?

Posted 18 December 2013 - 04:13 PM

The Javadoc tells you.

You can pass this as the last parameter

This post has been edited by g00se: 18 December 2013 - 04:14 PM
Reason for edit:: Link fix

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1