Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem loading background image

Tim29Tim29 Posts: 4Member
Noob post... I know there's loads of help all over the internet for this subject, but none of it seems to have done me any good. I was wondering if someone can point out where i'm going wrong with the below code. It's a game of breakout i got from open source and i'm trying to add a jpg as the background. (Have only included the code down to the relevant "paint" method). At the moment it runs, but the background is white rather than the image. Any help will be greatly appreciated:

import java.awt.*;
import java.applet.Applet;

public class Breakout extends Applet implements Runnable
{
Dimension d;
Font largefont = new Font("Helvetica", Font.BOLD, 24);
Font smallfont = new Font("Helvetica", Font.BOLD, 16);

FontMetrics fmsmall, fmlarge;
Graphics goff;
Image ii;
Thread thethread;

boolean ingame=false;

int player1score;
int ballx,bally;
int batpos;
int batdpos=0;
int balldx=0, balldy=0;
int dxval;
int ballsleft;
int count;
boolean showtitle=true;
boolean[] showbrick;
int bricksperline;

final int borderwidth=5;
final int batwidth=40;
final int ballsize=10;
final int batheight=5;
final int scoreheight=20;
final int screendelay=300;
final int brickwidth=40;
final int brickheight=20;
final int brickspace=2;
final int numlines=6;
final int startline=32;

Image background;


public String getAppletInfo()
{
return("BreakOut The Jubblies");
}

public void init()
{
Graphics g;
d = size();

background = getImage(getCodeBase(),"background.jpg");

bricksperline=(d.width-2*borderwidth)/(brickwidth+brickspace);
d.width=bricksperline*(brickwidth+brickspace)+(2*borderwidth);
g=getGraphics();
g.setFont(smallfont);
fmsmall = g.getFontMetrics();
g.setFont(largefont);
fmlarge = g.getFontMetrics();

showbrick=new boolean[bricksperline*numlines];
GameInit();
}

public void GameInit()
{
batpos=(d.width-batwidth)/2;
ballx=(d.width-ballsize)/2;
bally=(d.height-ballsize-scoreheight-2*borderwidth);
player1score=0;
ballsleft=3;
dxval=2;
if (Math.random()<0.5)
balldx=dxval;
else
balldx=-dxval;
balldy=-dxval;
count=screendelay;
batdpos=0;
InitBricks();
}

public void InitBricks()
{
int i;
for (i=0; i<numlines*bricksperline; i++)
showbrick[i]=true;
}

public void paint(Graphics g)
{
String s;
Graphics gg;

if (goff==null && d.width>0 && d.height>0)
{
ii = createImage(d.width, d.height);
goff = ii.getGraphics();
}
if (goff==null || ii==null)
return;

goff.drawImage (background, 0, 0, this);
goff.fillRect(0, 0, d.width, d.height);
if (ingame)
PlayGame();
else
ShowIntroScreen();
g.drawImage(ii, 0, 0, this);
}

Comments

Sign In or Register to comment.