By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,736 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,736 IT Pros & Developers. It's quick & easy.

Background colour won't show

P: n/a
Ali
Hi,

I wrote the following code and when I loaded the images as static
images my background displayed as black (set at start of code),
however, now I've started (trying!) to get my image to move, my
background has defaulted to white. Can anyone tell my why this is and
how to correct it?

Thanks very much,
Ali :o)
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;

public class SpaceInvaders extends Applet
{
StringBuffer message;

public void init()
{

}

public void start()
{
message = new StringBuffer("Game Started!");
}

public void stop()
{

}

public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;

// GETS WINDOW SIZE //
int gw = getSize().width;
int gh = getSize().height;

// SETS BACKGROUND COLOUR //
setBackground(Color.black);
// LOADS SPACE INVADER GRAPHICS //
Image Invader1a = getImage(getDocumentBase(),"invader1a.gif");
Image Gun = getImage(getDocumentBase(),"gun.gif");
Image Shield = getImage(getDocumentBase(),"shield.gif");
Image InvaderCover = getImage(getDocumentBase(),
"invaderCover.gif");

// LOADS IMAGES //

int imageX = (gw/2)-16;
int imageY = gh-80;

g2.drawImage(Gun, imageX, imageY+20, this);
g2.drawImage(Shield, imageX-171, imageY, this);
g2.drawImage(Shield, imageX-21, imageY, this);
g2.drawImage(Shield, imageX+129, imageY, this);

// MOVING INVADERS //

int x = 0;
int y = 100;
int moveX = 35;
int moveY = 20;

for (y=100; y<101; y=100)
{
g2.drawImage(InvaderCover, x, y, this);
if (x>300)
{
moveX = -35;
}
else if (x<1)
{
moveX = 35;
}

x += moveX;
g2.drawImage(Invader1a, x, y, this);

for (int delay=0; delay<50000000; delay++);
}
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ay*****@hotmail.com (Ali) wrote in message news:<12**************************@posting.google. com>...
Hi,

I wrote the following code and when I loaded the images as static
images my background displayed as black (set at start of code),
however, now I've started (trying!) to get my image to move, my
background has defaulted to white. Can anyone tell my why this is and
how to correct it?

setBackground() is a high-level Component method. If you draw raw
image using Graphics object, you also have to use the Graphics method
to fill your rectangle area with a color.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.