Sign in to follow this  

[java] Why does this happen!?

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok well i am creating a tile based game, in Java of course:), and well before i started this project i created a Galaga type game. I had my Driver class which set up the JFrame and all that then added the Game class which was a JPanel. I have my paintComponent method in that class and i can paint things to the screen no problem. But when i try to draw my entities ( You create the entity with a refrence string in the constructor which goes to the SpriteStore class, loads the sprite if it isnt loaded yet and then returns the sprite and blah blah) So now, when i go into my paintComponent method, i use a for loop to draw my entities and then nothing shows up until i minimize the screen and then maximize. This happens in many different applications but didnt happen in my Galaga game which uses just about the same thing essentiall (the classes are just about the same). I know i should post code or something but this happens a lot to many different people and i have no idea what causes the screen to need to be resized in order to see the painted images...

Share this post


Link to post
Share on other sites
Quote:
I know i should post code or something but this happens a lot to many different people and i have no idea what causes the screen to need to be resized in order to see the painted images...


I know I should post a helpful answer, but bugs happen a lot to different people and they have no idea how they should fix them.

We're not clairvoyant. paintComponent is how you render things in Java's component model. So it works.

If it doesn't work for you, it's a bug in your code, something which none of us has.

What does your paintComponent look like? Like this:

while (true) {
drawEntities();
}

If so, that's wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
I know i should post code or something but this happens a lot to many different people and i have no idea what causes the screen to need to be resized in order to see the painted images...


I know I should post a helpful answer, but bugs happen a lot to different people and they have no idea how they should fix them.

We're not clairvoyant. paintComponent is how you render things in Java's component model. So it works.

If it doesn't work for you, it's a bug in your code, something which none of us has.

What does your paintComponent look like? Like this:
*** Source Snippet Removed ***
If so, that's wrong.


No, thats not what my code looks like, but yea sorry for not including my code, but i thought this was a fairly common problem because it happens in applets sometimes and sometimes it doesnt.

Game.java
[Source]import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Game extends JPanel
{
ArrayList<Entity> entities;
Image bg =Toolkit.getDefaultToolkit().getImage("BLOCKED.png");
int width = 5;
int height = 5;
int tSize = 50;
int[][] Map = { { 1,1,1,1,1},
{ 1,0,1,0,1},
{ 1,0,1,0,1},
{ 1,0,1,0,1},
{ 1,1,1,1,1} };
public Game()
{
setSize(600,400);

entities = new ArrayList();

for(int i = 0; i < 24; i++)
{
entities.add(new TileEntity("BLOCKED.png",i*50,i*50));
}
}

public void paintComponent(Graphics g)
{
/*the panel Background Color*/
g.setColor(Color.black);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.white);
int y = 20;
for(int i = 0; i < entities.size();i++)
{
entities.get(i).draw(g);
}

}
}


Entity.java
[Source]import java.awt.*;

public class Entity{
// Entity x location
int x;
//Entity y location
int y;
//Entity x speed;
int xSpeed;
//Entity y speed;
int ySpeed;
//This entities Rectangle
private Rectangle me = new Rectangle();
//the others rectangle
private Rectangle him = new Rectangle();
//Entity Width and height
protected Sprite sprite;
String ref;
int width = 30;
int height = 30;

public Entity(String ref,int x, int y){
this.sprite = SpriteStore.get().getSprite(ref);
this.x = x;
this.y = y;
}

public void move(int speed)
{

}

public void draw(Graphics g) {
sprite.draw(g,(int) x,(int) y);
}

}


TileEntity.java
[Source]import javax.swing.*;
import java.awt.*;

public class TileEntity extends Entity
{
/*tile x pos*/
int x;
/*tile y pos*/
int y;
/*Which sprite to use*/
String sprite;
/*tile sizes Just for tests until i use sprites*/
int size = 50;

public TileEntity(String ref,int x, int y)
{
super(ref,x,y);
this.x = x;
this.y = y;
}

}



Sprite.java
[Source]import java.awt.Graphics;
import java.awt.Image;

/**
* A sprite to be displayed on the screen. Note that a sprite
* contains no state information, i.e. its just the image and
* not the location. This allows us to use a single sprite in
* lots of different places without having to store multiple
* copies of the image.
*
* @author Kevin Glass
*/

public class Sprite {
/** The image to be drawn for this sprite */
private Image image;

/**
* Create a new sprite based on an image
*
* @param image The image that is this sprite
*/

public Sprite(Image image) {
this.image = image;
}

/**
* Get the width of the drawn sprite
*
* @return The width in pixels of this sprite
*/

public int getWidth() {
return image.getWidth(null);
}

/**
* Get the height of the drawn sprite
*
* @return The height in pixels of this sprite
*/

public int getHeight() {
return image.getHeight(null);
}

/**
* Draw the sprite onto the graphics context provided
*
* @param g The graphics context on which to draw the sprite
* @param x The x location at which to draw the sprite
* @param y The y location at which to draw the sprite
*/

public void draw(Graphics g,int x,int y) {
g.drawImage(image,x,y,null);
}
} /source]
SpriteStore.java
[Source]import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.awt.*;
import javax.imageio.ImageIO;

/**
* A resource manager for sprites in the game. Its often quite important
* how and where you get your game resources from. In most cases
* it makes sense to have a central resource loader that goes away, gets
* your resources and caches them for future use.
* <p>
* [singleton]
* <p>
* @author Kevin Glass
*/

public class SpriteStore {
/** The single instance of this class */
Image image;
private static SpriteStore single = new SpriteStore();

/**
* Get the single instance of this class
*
* @return The single instance of this class
*/

public static SpriteStore get() {
return single;
}

/** The cached sprite map, from reference to sprite instance */
private HashMap sprites = new HashMap();

/**
* Retrieve a sprite from the store
*
* @param ref The reference to the image to use for the sprite
* @return A sprite instance containing an accelerate image of the request reference
*/

public Sprite getSprite(String ref) {
// if we've already got the sprite in the cache

// then just return the existing version

if (sprites.get(ref) != null) {
return (Sprite) sprites.get(ref);
}

// otherwise, go away and grab the sprite from the resource

// loader

BufferedImage sourceImage = null;
// The ClassLoader.getResource() ensures we get the sprite

// from the appropriate place, this helps with deploying the game

// with things like webstart. You could equally do a file look

// up here.

image =Toolkit.getDefaultToolkit().getImage(ref);

if (image == null) {
fail("Can't find ref: "+ref);
}

// use ImageIO to read the image in



// create a sprite, add it the cache then return it

Sprite sprite = new Sprite(image);
sprites.put(ref,sprite);

return sprite;
}

/**
* Utility method to handle resource loading failure
*
* @param message The message to display on failure
*/

private void fail(String message) {
// we'n't available
// we dump the message and exit the game

System.err.println(message);
System.exit(0);
}
}



Ok i realize i included a LOT of code, but im not sure where the problem is occuring, but to narrow it down for you. Basically i create a new TileEntity with the refrence "BLOCKED.png" which goes to the SpriteStore and loads BLOCKED.png if it is not loaded and then returns it, Then it is drawn.

But i have to minimize then maximize to see the images drawn.

[Edited by - Swattkidd on September 15, 2007 4:55:49 PM]

Share this post


Link to post
Share on other sites
Do you have a game loop setup that actually invokes repaint() or update() for your Game objects (or it's parent) ?

It seems like you application is waiting for the O/S to notify it when to repaint.

Share this post


Link to post
Share on other sites
Yea he was right, THANKS A LOT GUYS!

But i also have another question, not really pertaining to the main subject of this thread but here it goes.

Is it bad that i am storing all my tiles in an ArrayList because i have my tiles in their own TileEntity class which is an extension of the Entity class. I know this problem isnt the Ideal situation but i did this so i could use my sprite store class and all that to cache my sprites...but yea

I was thinking i should change all that and cache the images like most do (1 sprite sheet or something) would that be better?

Also if i do it that way, what is the best way to check to see if things are blocked? Should i just check if(Map[x][y] == BLOCKED) but i want to do rectangle collision, so how could i create a rectangle with each tile? (This is one of the reasons i created a TileEntity class so i could have the collision methods and what not in there.

Or maybe i could leave the TileEntity class and there might be a better way then storing the tiles into an arraylist?

Sorry for the off the wall questions, i was just kind of brainstorming and trying to get feedback.

[Edited by - Swattkidd on September 19, 2007 8:35:08 PM]

Share this post


Link to post
Share on other sites

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this