For the longest time, I have been using the IDE "JCreator". I made previous games on it in the past for school, and when it would come to me implementing a background image in my game, I would make a background class, and have the image in my project's class folder.

With the following code on JCreator, I get exactly what I had wanted, a Background Image to be displayed when I run the applet:

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.image.*;

 class background
private Image img;
 public int xpos, ypos, height, width, speed, xdir, ydir;
 private Ellipse2D.Double c;
 public background(Image p5, int x, int y, int w, int h, int s, int xd)
  img = p5;
  xpos = x;
  ypos = y;
  width = w;
  height = h;
  speed = s;
  xdir = xd;
  ydir = 0;
  c = new Ellipse2D.Double (xpos, ypos,  width,  height);
 public Image getImage(){return img;}

 public int getX() { return xpos;}
 public int getY() { return ypos ;}
 public int getHeight() { return height; }
 public int getWidth() { return width ;}
 public int getXDir() { return xdir ;}
 public int getYDir() { return ydir ;}
 public void setDir(int x, int y)

  xdir = x;
  ydir = y;

 public boolean intersects(int x, int y, int w, int h)
  return c.intersects(x, y, w, h);
 public void displaybackground(Graphics2D g , Applet a)
  g.drawImage(img, xpos, ypos, width, height , a);
 public void paint(Graphics2D g)

public class iamcool extends Applet implements KeyListener {
 BufferedImage imageBuffer;
 Graphics2D  graphicsBuffer;
 private background b;
 Image p5;

  public void init()
    imageBuffer = (BufferedImage)createImage(getWidth(), getHeight());
       graphicsBuffer = (Graphics2D) imageBuffer.getGraphics();

    p5= getImage(getCodeBase(), "sunset.jpg");

    b=new background(p5, 0, 0, 1680, 1050, 5, 0);


 public void keyPressed(KeyEvent e)
 public void keyReleased(KeyEvent e)
 public void keyTyped(KeyEvent e) {}

 public void paint(Graphics g)

      Graphics2D g2 = (Graphics2D) g;
      b.displaybackground(graphicsBuffer, this);
      g2.drawImage(imageBuffer, 0,0, getWidth(), getHeight(), this);


I'm very very new to java, so i'm sorry if this is actually a really simple problem to solve. But I'm confused with how eclipse works in adding images to a project. Could someone walk me through how I can have an applet open up in Eclipse with a image-based background? The Above code may have worked at one time once for JCreator but it's different for Eclipse. All I want is to have a background image while will be used as the background of a game.

Thanks a ton

6 Years
Discussion Span
Last Post by runee1000
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.