iam not beeing printed to screen
for some reason my background image is not beeing print to screen. i am using sprite_sheet.java to get all my images and iam iam print in background.java. than using main to call background.java paint.
weird this is this is beeing printed on screen. this code is in background paint method.
g.setColor(new Color(15, 77, 147)); //sky
g.fillRect(0, 0, m.getWidth(), m.getHeight()); //sky
but code right below doesnt work. any ideas????
Code :
g.drawImage(background_image_1, (int)bX1, 0, Sprite_Sheet.m1);//backgroung image
g.drawImage(background_image_1, (int)bX1+m.getWidth(), 0, Sprite_Sheet.m1);//backgroung image
Code :
public class Sprite_Sheet
{
static Image background_sheet_1;
static Image background_sheet_2;
static Image player_sheet;
static Image platform_sheet;
URL url;
static Main m1;
public Sprite_Sheet(Main m2)
{
try
{
url = m2.getDocumentBase();
}
catch(Exception e)
{
}
background_sheet_1 = m2.getImage(url, "image/background/background.png");
background_sheet_2 = m2.getImage(url, "image/background/background2.png");
player_sheet = m2.getImage(url, "image/sprite_sheet/player_sheet.png");
platform_sheet = m2.getImage(url, "image/sprite_sheet/platform.png");
this.m1 = m2;
}
}
background.java
Code :
public class Background
{
double bX1 = 0;
double bY1 = 0;
double bDX1 = 0.3;
double bX2 = 0;
double bY2 = 0;
double bDX2 = 0;
Image background_image_1;
Image background_image_2;
public Background()
{
background_image_1 = Sprite_Sheet.background_sheet_1;
background_image_2 = Sprite_Sheet.background_sheet_2;
}
/*** move background ***/
public void BACKGROUND_MOVE(Main m)
{
if(bX1 > m.getWidth() * - 1){
bX1 -= bDX1;
}
else{
bX1 = 0;
}
if(bX2 > m.getWidth() * - 1){
bX2 -= bDX2;
}
else{
bX2 = 0;
}
}
public void paint(Graphics g, Main m)
{
g.setColor(new Color(15, 77, 147)); //sky
g.fillRect(0, 0, m.getWidth(), m.getHeight()); //sky
g.drawImage(background_image_1, (int)bX1, 0, Sprite_Sheet.m1);//backgroung image
g.drawImage(background_image_1, (int)bX1+m.getWidth(), 0, Sprite_Sheet.m1);//backgroung image
g.drawImage(background_image_2, (int)bX2, 0, Sprite_Sheet.m1);//backgroung image2
g.drawImage(background_image_2, (int)bX2+m.getWidth(), 0, Sprite_Sheet.m1);//backgroung image2
}
}
main.java
Code :
public class Main extends Applet implements Runnable, KeyListener
{
//Double buffering variables
Image dbImage;
Graphics dbGraphics;
Thread thread;
//is if game is running or not
boolean running = true;
//Class variables
Background background_class;
Score score_class;
Ground ground_class;
Sprite_Sheet sprite_sheet_class;
Player player_class;
Platform platform_class[] = new Platform[5];
Enemy enemy_class;
Item item_class[] = new Item[3];
Shoot shoot_class;
Container c;
/*** init method ***/
@Override
public void init()
{
setSize(900, 400);
addKeyListener(this);
}/*** end of init method ***/
/*** start method ***/
@Override
public void start()
{
/*** set variables ***/
Random r1 = new Random();
background_class = new Background();
ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height
sprite_sheet_class = new Sprite_Sheet(this);
player_class = new Player(5, 250); //x, y
for(int i = 0; i < platform_class.length; i++)
{
Random r = new Random();
platform_class[i] = new Platform(this.getWidth()+200*i, this.getHeight()- 40 - r.nextInt(400)); //x,y --sub -40 so no cut off platform at bottom
}
//item_class = new Item_0_Background(200); //x print 1 item
for(int i = 0; i < item_class.length; i++)
{ item_class[i] = new Item_1_Fast(r1.nextInt(900)); } //x - print 1 item than swich to 2nd item
shoot_class = new Shoot();
enemy_class = new Enemy(500, 345); //x, y
score_class = new Score(5,30);
thread = new Thread(this); //start run method
thread.start();
}/*** end of start method ***/
/*** run method ***/
public void run()
{
while(player_class.isDEAD() == false) //if player is not dead keep going
{
background_class.BACKGROUND_MOVE(this);
//set score
score_class.SCORE();
score_class.health(player_class, enemy_class);
Random r = new Random();
ground_class.GROUND_COLLISION(this, player_class); //ground + player collions
player_class.PLAYER_MOVE(this, ground_class); //player moves
for(int i = 0; i < platform_class.length; i++)
{ platform_class[i].PLATFORM_ANIMATION();
platform_class[i].PLATFORM_COLLSION(this, player_class); }
shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class); //player shoot bullet moves
enemy_class.ENEMY_MOVE(this);
enemy_class.PLAYER_ENEMY_COLLISION(this, player_class, enemy_class); //player + enemy collision
for(int i = 0; i < item_class.length; i++)
{
item_class[i].PLAYER_ITEM_COLLISION(this, score_class, player_class, ground_class);
if(item_class[i].getY() == 0) //if item is top left coner
{
item_class[i] = null; //delete item
item_class[i] = new Item_0_Background(r.nextInt(900)-50);
int ra = r.nextInt(4);
if(ra == 0)
{
item_class[i] = new Item_0_Background(r.nextInt(900)-10); //create new item
}
else if(ra == 1)
{
item_class[i] = new Item_1_Fast(r.nextInt(900)-10); //create new item
}
else if(ra == 2)
{
item_class[i] = new Item_2_SLOW(r.nextInt(900)-10); // swich to different item
}
else if(ra == 3)
{
item_class[i] = new Item_3_Score(r.nextInt(900)-10);
}
}
}
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}/*** end of run method ***/
/*** update method ***/
@Override
public void update(Graphics g)
{
if(dbImage == null) //if image is empty than create new image
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbGraphics = dbImage.getGraphics();
}
dbGraphics.setColor(getBackground()); //set the background color
dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbGraphics.setColor(getForeground());
paint(dbGraphics); //call paint method
g.drawImage(dbImage, 0, 0, this);
}/*** end of update method ***/
/*** paint method ***/
@Override
public void paint(Graphics g)
{
background_class.paint(g, this);
ground_class.paint(g); //call paint method from Ground class //draw ground than player on top
player_class.paint(g); //call paint method from Player classr
for(int i = 0; i < platform_class.length; i++)
{ platform_class[i].paint(g); }
shoot_class.paint(g); //draw bullet
if(enemy_class.isDEAD() == false) //draw enemy if it's not dead
{
enemy_class.paint(g);
}
for(int i = 0; i < item_class.length; i++)
{ item_class[i].paint(g); }
score_class.paint(g);
}/*** end of paint method ***/
/*** stop method ***/
@Override
public void stop()
{
}/*** end of stop method ***/
/*** destroy method ***/
@Override
public void destroy()
{
}/*** end of destroy method ***/
/************** key *********************/
@Override
public void keyPressed(KeyEvent e)
{
int keys = e.getKeyCode();
if(keys == KeyEvent.VK_RIGHT)
{
player_class.hitRIGHT();
}
else if(keys == KeyEvent.VK_LEFT)
{
player_class.hitLEFT();
}
else if (keys == KeyEvent.VK_UP)
{
player_class.hitJUMP(ground_class);
}
else if(keys == KeyEvent.VK_SPACE)
{
shoot_class.hitSHOOT();
}
}
@Override
public void keyReleased(KeyEvent e)
{
int keys = e.getKeyCode();
if(keys == KeyEvent.VK_RIGHT)
{
player_class.stopRIGHT();
}
else if(keys == KeyEvent.VK_LEFT)
{
player_class.stopLEFT();
}
else if(keys == KeyEvent.VK_SPACE)
{
shoot_class.stopSHOOT();
}
}
@Override
public void keyTyped(KeyEvent e){}
/************ MOUSE **********/
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
Re: iam not beeing printed to screen
i think it's because you instansiate the background class before sprites_sheet class,
the background class relies on the sprite_sheet variable - ml, which is null at the time..
Re: iam not beeing printed to screen