Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: anti doublebuffer or lag?

  1. #1
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default anti doublebuffer or lag?

    So far on my demo you can walk around (no collision detection yet) and type a message. When I was walking around I noticed a problem. Every once in a while the character sprite will lag/jump, and im not sure if this is due to the fact that I don't double buffer, or my code is laggy. Here is all of my code.


    gameMain.class
    import javax.swing.JFrame;
     
     
    public class gameMain {
    	public static void main(String[] args){
    		chatBox chatBox = new chatBox();
    		keyBoard kb = new keyBoard();
    		gui gui = new gui();
     
     
    		JFrame f = new JFrame("BrettScape");
    		f.setSize(646,500);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setResizable(false);
     
    		f.add(gui);
    		f.addKeyListener(kb);
    		f.setFocusTraversalKeysEnabled(false);
    		f.setVisible(true);
     
     
    		while(true) {
    			gui.updateScreen(chatBox.getMessages(), kb.getMessage(), kb.getX(), kb.getY());
    		}
    	}	
    }


    chatBox.class
     
    public  class chatBox {
    	String messageBox[]={"Welcome to BrettScape!","","","",""};
     
    	public String[] getMessages(){
    		return messageBox;
    	}
    	public void addMessage(String newMess){
    		messageBox[4] = messageBox[3];
    		messageBox[3] = messageBox[2];
    		messageBox[2] = messageBox[1];
    		messageBox[1] = messageBox[0];
    		messageBox[0] = newMess;
    	}
    }


    gui.class
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class gui extends JPanel {
    	BufferedImage img,chatBoxImg,avatar;
    	String messages[];
    	String typing;
    	int map[][]={ //I know this map is in my gui class, but I'm going to add a different class for maps.
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
    	};
    	int messCount,row,column,playerX,playerY;
     
    	public gui(){
    		try {
    			img = ImageIO.read(new File("tiles.png"));
    			chatBoxImg = ImageIO.read(new File("chat.png"));
    			avatar = ImageIO.read(new File("avatar.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void updateScreen(String[] messages, String typing, int playerX, int playerY){
    		this.messages = messages;
    		this.typing = typing;
    		this.playerX = playerX;
    		this.playerY = playerY;
    		repaint();
    	}
     
    	public void paintComponent(Graphics g){		
    		//draw first tile layer
    		for(column=0;column<=11;column++) {
    			for(row=0;row<=19;row++){
    				g.drawImage(img, row*32, column*32, row*32+32, column*32+32, map[column][row]*32, 0, map[column][row]*32+32, 32, null);
    			}
    		}	
     
    		//second layer
    		g.drawImage(avatar, playerX, playerY, null);
     
    		//third layer
     
    		//draws messages for chat box
    		g.drawImage(chatBoxImg, 0, 384, null);
    		for(messCount=0;messCount<=4;messCount++){
    			g.drawString(messages[messCount], 5, 400+(messCount*14));
    		}	
     
    		//draw what the person is typing
    		g.drawString(typing+"*", 5, 468);
    	}
    }


    keyBoard.class
    import java.awt.event.*;
     
    public class keyBoard implements KeyListener {
    	int keyCode;
    	int playerX,playerY=10;
    	String message="";
     
     
    	public int getX(){
    		return playerX;
    	}
     
    	public int getY(){
    		return playerY;
    	}
     
    	public String getMessage(){
    		return message;
    	}
     
    	public void keyPressed(KeyEvent e){
    		keyCode = e.getKeyCode();
     
    		if (keyCode == KeyEvent.VK_LEFT) {
    			playerX-=5;
    		}
    		if (keyCode == KeyEvent.VK_RIGHT) {
    			playerX+=5;
    		}
    		if (keyCode == KeyEvent.VK_UP) {
    			playerY-=5;
    		}
    		if (keyCode == KeyEvent.VK_DOWN) {
    			playerY+=5;
    		}
    		//Detection of normal keys
    		if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(),PeriodComma/?;:`~[]{}=+<>Space".indexOf(KeyEvent.getKeyText(keyCode)) >= 0) {
    			message+=KeyEvent.getKeyText(keyCode).replace("Space", " ").replace("Period", ".").replace("Comma", ",").toLowerCase();
     
    			//capitalize the first letter
    			if (message.length() == 1) 
    				message = message.toUpperCase();
    		}
     
    		//if backspace delete last letter
    		if (KeyEvent.getKeyText(keyCode) == "Backspace") {
    			if(message.length() >= 1)
    				message = message.substring(0, message.length()-1);
    		}
     
     
     
     
    	}
     
     
    	public void keyReleased(KeyEvent e){
     
    	}
     
    	public void keyTyped(KeyEvent e){
     
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: anti doublebuffer or lag?

    Swing components are by default double buffered, so this might not be the issue. You may wish to profile your code by printing out a bunch of times for certain routines...there are a few areas that can be optimized (for example the tiling could be written once to a BufferedImage, and then rather than loop to draw each tile each time in the paintComponent, you could just draw that the tiling composite image once).

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: anti doublebuffer or lag?

    I'm thinking of changing my engine. Instead of my character walking around im going to change it so the map scrolls and the character stays stationary. How would i optimize that?

    EDIT: When I was running my game I noticed that it was making my computer laggy. I opened the task manager and it was using 62%+ of my CPU! Am I calling update too often, should I add a sleep method in my loop? I'm not sure how to make this go down.

    Second EDIT: I had the thread sleep for 16ms and that brought it down to 11%CPU
    Last edited by Brt93yoda; August 19th, 2010 at 08:25 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: anti doublebuffer or lag?

    It's much easier to have a stationary map than a stationary character (at least in the data structure, how you draw it doesn't matter).

    Simply having to move every component of the map would take a very long time compared to moving just the one character.

    What I would recommend is having the camera follow the character around, but still have the character moving through the map.

    A simple optimization for collision detection is to quantify your entire map into a grid, then when your character moves, you only have to check collision against items that are in the same grid as the character rather than having to check collision against everything.

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: anti doublebuffer or lag?

    What I would recommend is having the camera follow the character around, but still have the character moving through the map.
    So how would I go about doing that?

Similar Threads

  1. BullDog Secuirty Fake Anti-Virus
    By Reese in forum The Cafe
    Replies: 9
    Last Post: July 31st, 2010, 05:33 PM