Background Colour has vanished!

Discussion in 'Java' started by Ali, Dec 23, 2003.

  1. Ali

    Ali Guest

    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 :eek:)


    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++);
    }
    }
    }
     
    Ali, Dec 23, 2003
    #1
    1. Advertising

  2. Andrew Thompson, Dec 23, 2003
    #2
    1. Advertising

  3. Hello

    for drawing of images in the game, you have to use bltbit function. the new
    image is just add to the previous image by using bits calculations. say the
    new image white dots are not displayed at all. this is used for all games.

    I have not been programming in java as of late. hence i cannot submit you
    fill source code. but you will find it on the net.

    sumeet shroff


    "Ali" <> wrote in message
    news:...
    > 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 :eek:)
    >
    >
    > 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++);
    > }
    > }
    > }
     
    Prateeksha Web Services, Dec 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G. Völkl

    ASP 2: item from arraylist vanished ?!

    G. Völkl, Nov 18, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    366
    Karl Seguin
    Nov 18, 2005
  2. Marvel
    Replies:
    16
    Views:
    632
    Marvel
    Mar 24, 2006
  3. netnatter

    Server Explorer vanished

    netnatter, Jun 23, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    292
    Cowboy \(Gregory A. Beamer\)
    Jun 23, 2008
  4. Dj Frenzy
    Replies:
    3
    Views:
    351
    Robert
    Feb 10, 2004
  5. jodleren
    Replies:
    2
    Views:
    218
    Thomas 'PointedEars' Lahn
    Jan 12, 2008
Loading...

Share This Page