Starting to make a game

Ok I know I have been pretty much spamming all the forums asking for help lately and I am very sorry for that.
The problem is that my younger bros birthday is on may 7th and for the last 2 months I have been hell bent into making him a game (he had some health implications so I wanna cheer him up). If you want proof of what I did for the last 2 months I will gladly provide it. Anyways the problem is that I have hit a brick wall. My engine works, and all the mechanics are there. But I can not find a single tutorial anywhere that will teach me how to draw png(I already made a huge library of them) images to the screen without them lagging crazily.
Now I do not want to use pre build engines or anything like that. I want this to be my own work made by me from ground up, otherwise the present will not feel as good.
Anyways if someone could please redirect me or show me some kind of tutorial where I can draw images onto the screen efficiently I will be forever grateful to you.

Currently in the last 2 months I came up with these 4 skeletons on where everything is located

Skeleton 1 http://pastebin.com/RjRDfU9V
Skeleton 2 http://pastebin.com/kpxGZA0z
Skeleton 3 http://pastebin.com/V6Jy9470
Skeleton 4 http://pastebin.com/6eWf0fzG

I tried using g.drawImage but the game starts lagging, if I replace everyimage with fillRect it works super fast and smooth, so I know the issue is drawing.

Thanks to anyone who will help ^^

The skeleton 1 is fine.
But you should also paste the way you are loading and drawing your pngs to see what’s wrong .

It’s great that you’re starting a game, I’m happy for you. We also don’t mind answering questions here at JGO. In fact, many of us enjoy doing it!

However, duplicate questions are frowned on, and posting something 3 times (as far as I’ve counted so far) is definitely a no-no. Now I’m annoyed with reading the same thing again.

This question is, more or less a duplicate of:
http://www.java-gaming.org/index.php/topic,22250.0.html

Which was already a duplicate post (the other post was deleted). I am locking the thread mentioned above.

So in the future post each question once and then be patient.
Thanks!

Ok I am very sorry about that :-\ :’(
Promise I won’t do it again ><

As to answer your question teletubo I load png into ImageIcon and then load it into Image like so


ImageIcon ii = new ImageIcon(this.getClass().getResource("images/objects/"+sprite+".PNG"));
image = ii.getImage();

And everything including platforms, main character and monsters is loaded same way

For example this is my platform class


package MediumFullScreen;
import java.awt.Image;

import javax.swing.ImageIcon;


public class Wall
{
	private Image image;
	private double x, y;
	private double leftOffset, rightOffset, topOffset, bottomOffset;
	private int width, height;
	private static int WIDTH, HEIGHT;
	
	// set the screen  parameters, if object goes out of screen it becomes null
	public static void setScreen(int width, int height)
	{
		WIDTH = width;
		HEIGHT = height;
	}
	
	public Wall(String sprite)
	{
		ImageIcon ii = new ImageIcon(this.getClass().getResource("images/objects/"+sprite+".PNG"));
		image = ii.getImage();

		width = image.getWidth(null);
		height = image.getHeight(null);
		
		x = 0;
		y = 0;
		
		leftOffset = 0;
		rightOffset = 0;
		topOffset = 0;
		bottomOffset = 0;
	}
	
	public Wall()
	{
		image = null;
		
		width = 0;
		height = 0;
		
		x = 0;
		y = 0;
		
		leftOffset = 0;
		rightOffset = 0;
		topOffset = 0;
		bottomOffset = 0;
	}
	
	public Wall(Image img)
	{
		image = img;
		
		width = image.getWidth(null);
		height = image.getHeight(null);
		
		x = 0;
		y = 0;
		
		leftOffset = 0;
		rightOffset = 0;
		topOffset = 0;
		bottomOffset = 0;
	}
	
	public Image getImage()
	{
		if(x<=WIDTH && x+width>=0 && y+height>=0 && y<=HEIGHT)
			return image;
		return null;
	}
	
	public void setOffset(int topOffset, int bottomOffset, int leftOffset, int rightOffset)
	{
		this.topOffset = topOffset;
		this.bottomOffset = bottomOffset;
		this.leftOffset = leftOffset;
		this.rightOffset = rightOffset;
	}
	
	public int getBottomY()
	{
		return (int)(y+height-bottomOffset);
	}

	public int getLeftX()
	{
		return (int)(x+leftOffset);
	}

	public int getRightX()
	{
		return (int)(x+width-rightOffset);
	}

	public int getTopY()
	{
		return (int)(y+topOffset);
	}

	public int getHeight()
	{
		return height;
	}

	public int getWidth()
	{
		return width;
	}

	public int getX()
	{
		return (int)x;
	}

	public int getY()
	{
		return (int)y;
	}

	public void setHeight(int height)
	{
		this.height = height;
	}

	public void setWidth(int width)
	{
		this.width = width;
	}

	public void setX(double x)
	{
		this.x = x;
	}

	public void setY(double y)
	{
		this.y = y;
	}
}


And the problem is that once I reach 30 images, then the whole game lags out. So is there a better way to load them? Maybe using something else?


ImageIcon ii = new ImageIcon(this.getClass().getResource("images/objects/"+sprite+".PNG"));
image = ii.getImage();

you dont have to use ImageIcon you can load Image directly

could you give a little more code ? maybe the drawing /loop part it would be more interresting to found the botleneck

first, you shouldn’t load one image for each wall . You can have a static class with just one copy of the image and then just draw the same image in different places .

You should also use BufferedImage instead of Image .
ex:


public static BufferedImage[] images;

public static final int IMAGE_WALL=0, TOTAL_IMAGES=1;
...

images = new BufferedImage[TOTAL_IMAGES];

images[IMAGE_WALL] = loadImage(.....);


and then when you can just refer to the index on this array in your sprite class, ex:



int imageIdx=0;


public void drawMe(Graphics2D g)
{
g.drawImage(x,y, ImagesLib.images[imageIdx], null);
}

by the way you didn’t post the method you are using to draw the images .

Well in the skeleton where I have *start drawing
right in between there I put this in


g.drawImage(sky.getImage(), 0, 0, this);
        
        for(int i=0; i<background.length; i++)
        {
        	g.drawImage(background[i].getImage(), background[i].getX(), background[i].getY(), this);
        }
        
        for(int i=0; i<wall.length; i++)
        {
        	g.drawImage(wall[i].getImage(), wall[i].getX(), wall[i].getY(), this);
        }
        
        for(int i=0; i<platform.length; i++)
        {
        	g.drawImage(platform[i].getImage(), platform[i].getX(), platform[i].getY(), this);
        }
        
        g.drawImage(hero.getImage(), hero.getX(), hero.getY(), this);

Thanks to everyone for helping ^^

remove the image observer (the last parameter from the drawImage calls) and pass “null”.

that is,


 	g.drawImage(background[i].getImage(), background[i].getX(), background[i].getY(), null);

Everyone I thank you from the bottom of my heart, you guys are awesome.
I used buffered image and changed the observer to null, and now it runs smoothly.
My brother sure will be happy ^^
THANK YOU ONCE AGAIN
Of course first I will give him the present once I finish designing the levels and then I will post a copy of the game here later on
THANK YOU ^^ :slight_smile: