Jump to content
  • Advertisement
Sign in to follow this  
NickyP101

Performance Issues???

This topic is 5010 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

Hey all, im pretty sure im doing all i can to get the best performance, but the scrolling is still bumpy. Below ill paste all the graphics code, not the entire class just the graphics section. Java
	public void init(){



		map = new Map(this);

		Mhandler = new MouseHandler();
		Khandler = new KeyHandler();
		
		//Add Listeners
		this.addKeyListener(Khandler);
		this.addMouseMotionListener(Mhandler);
		this.addMouseListener(Mhandler);

		imgCropx1 = 0;
		imgCropy1 = 0;

		imgCropx2 = 800;
		imgCropy2 = 800;


		game = new BufferedImage(800, 800, BufferedImage.TYPE_INT_ARGB);

	
		bullCount = 0;
		bullets = new HashSet();


		createBufferStrategy(2);

		strategy = getBufferStrategy();

		System.out.println("Created Strat");


	}




	
	public void game(){

		while(true){
		updateWorld();
		paintWorld();		
		strategy.show();

		try{
			Thread.sleep(0);
		}catch(Exception e){System.out.println(e);}

	
		}

	}

	public void updateWorld(){

		int v;
		v = 0;
		int p = 0;
		Bullet temp;

		it = bullets.iterator();

		while(it.hasNext()){

			temp = (Bullet) it.next();

			if(temp.dead == true){
				System.out.println("Dead");
				it.remove();
				p = p -	1;

			}
			else{

			temp.updatePos();
			}
			v++;

		}

	
	}

	public void paintWorld(){

	

		image = map.drawMap(0,0);
		Graphics2D g2d = (Graphics2D)strategy.getDrawGraphics();
	  	g2d.setColor(Color.black);
	       	g2d.fillRect(0,0,getWidth(),getHeight());
		g2d.drawImage(image, 0, 0, 800, 800, imgCropx1, imgCropy1, imgCropx2, imgCropy2, null); 
		g2d.fillRect(playerX, playerY, 20, 20);
		g2d.fillOval(crossHairX, crossHairY, 5, 5);


	}



That is my main loop class. Java
import javax.swing.*;
import java.awt.image.*;
import java.awt.*;
import java.util.*;

public class Map{

	//Grass = 0
	//Rock = 2
	//Sky = 3
	//Player = 4


	//TopLeftRock 5
	//TopMiddleRock 6
	//TopRightRock 7
	//MiddleRock 9
	//MiddleLeftRock 8
	//MiddleRightRock 10
	//BottomRightRock 13
	//BottomMiddleRock 12
	//BottomLeftRock 11
	

	int positionx = 0;
	int positiony = 0;

	int playerx;
	int playery;

	WarPanel reference;

	Iterator it;




	int[][] map ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             	      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
		      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
		      {0,0,2,2,2,4,2,2,2,4,2,2,2,2,2,4,2,2,2,2,4,2,2,2,2,2,2,4,2,2,2,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
             	      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
		      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
		      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
             	      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},};

	BufferedImage buffImage;
	Image grass;
	Image rock;
	Image sky;
	Image water;
	BufferedImage renew;

	Graphics g2D1;


	public Map(WarPanel ref){


		reference = ref;

		renew = new BufferedImage(32 * 50, 34 * 50, BufferedImage.TYPE_INT_ARGB);
		buffImage = new BufferedImage(32 * 50, 34 * 50, BufferedImage.TYPE_INT_ARGB);
		
		grass = new ImageIcon("Grass.jpg").getImage();
		rock = new ImageIcon("Rock.jpg").getImage();
		sky = new ImageIcon("stars.jpg").getImage();
		water = new ImageIcon("Water.jpg").getImage();







	
	}
	
	public BufferedImage drawMap(int p, int p1){

		positionx = positionx + p;
		positiony = positiony + p1;


		Graphics g2D = buffImage.createGraphics(); 

		for(int x = 0; x < 34; x++){

			for(int y = 0; y < 32; y++){

				if(map[x][y] == 0){

					g2D.drawImage(grass, y * 50, x * 50, null);

				}

				if(map[x][y] == 1){

					g2D.drawImage(rock, y * 50, x * 50, null);

				}

				if(map[x][y] == 2){

					g2D.drawImage(rock, y * 50, x * 50, null);

				}

				if(map[x][y] == 4){

					g2D.drawImage(water, y * 50, x * 50, null);

				}
			

	                }
		}



		int v;
		v = 0;
		Bullet temp;	
		it = reference.bullets.iterator();
		while(it.hasNext()){

				temp = (Bullet)it.next();
				g2D.drawOval((int)temp.bx, (int)temp.by, 10, 10);


			v++;

		}
		buffImage.flush();
		return buffImage;

	}
}




Above ive pasted the entire class because i feel it is where the problem is, ofcoarse it doesnt sound correct to be rewriting the entire background image over and over again, but i cant figure out away to get around it, ive tried soo many different things, making another BImage and drawing buffImage to it then adding the bullets, and many more things. Can u see any obvious bad programming? Thanks in advance, Nick

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!