Results 1 to 10 of 10
  1. #1
    Kamlanie is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Double Buffering help

    Hello all!

    I am trying to figure out why my sprite is not showing on the screen! heres my Main class:

    Java Code:
    import com.kam.entity.Entity;
    import com.kam.spritesheet.BufferedImageLoader;
    import com.kam.spritesheet.SpriteSheet;
    
    public class Main extends JFrame {
    
    	public int WIDTH = 800;
    	public int HEIGHT = 600;
    	public SpriteSheet ss = null;
    
    	private ArrayList<Entity> entities = new ArrayList<Entity>();
    	private BufferStrategy strategy;
    
    	public Main() {
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		
    		createBufferStrategy(2);
    		strategy = getBufferStrategy();
    
    		init();
    	}
    
    	private void init() {
    		BufferedImageLoader loader = new BufferedImageLoader();
    		BufferedImage spriteSheet = null;
    		try {
    			spriteSheet = loader.loadImage("/res/00_01.png");
    		} catch (IOException ex) {
    			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    		}
    		this.ss = new SpriteSheet(spriteSheet);
    		createIntities();
    	}
    
    	private void createIntities() {
    		Entity e = new Entity(ss.grabSprite(0, 0, 15, 15), 100, 100);
    		entities.add(e);
    
    		gameLoop();
    	}
    
    	private void gameLoop() {
    
    		Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, 800, 600);
    
    		// draw stuffs...
    
    		for (int i = 0; i < entities.size(); i++) {
    			Entity entity = (Entity) entities.get(i);
    			entity.draw(g);
    		}
    
    		g.dispose();
    		strategy.show();
    		Toolkit.getDefaultToolkit().sync();
    
    	}
    
    	public static void main(String[] args) {
    		new Main();
    	}
    
    }
    Buffered Image Loader class:
    Java Code:
    package com.kam.spritesheet;
    
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class BufferedImageLoader {
    
    	public BufferedImage loadImage(String p) throws IOException {
    		return ImageIO.read(this.getClass().getResource(p));
    	}
    }
    Sprite sheet class:
    Java Code:
    package com.kam.spritesheet;
    
    import java.awt.image.BufferedImage;
    
    public class SpriteSheet {
    
    	private BufferedImage ss;
    
    	public SpriteSheet(BufferedImage ss) {
    		this.ss = ss;
    	}
    
    	public BufferedImage grabSprite(int x, int y, int w, int h) {
    		return this.ss.getSubimage(x, y, w, h);
    	}
    }
    and finally the Entity class:
    Java Code:
    package com.kam.entity;
    
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.Image;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    
    import com.kam.render.Renderer;
    
    public class Entity {
    
    	private BufferedImage bi;
    	protected int x;
    	protected int y;
    	public Image image;
    
    	public Entity(BufferedImage bi, int x, int y) {
    		this.x = x;
    		this.y = y;
    
    		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    		this.image = gc.createCompatibleImage(bi.getWidth(), bi.getHeight(), Transparency.BITMASK);
    
    	}
    
    	public void draw(Graphics g) {
    		Renderer.draw(g, this.image, (int) x, (int) y);
    	}
    
    }
    Renderer.java

    Java Code:
    package com.kam.render;
    
    import java.awt.Graphics;
    import java.awt.Image;
    
    public class Renderer {
    
    	
    	public static void draw(Graphics g,Image i,int x, int y){
    			g.drawImage(i,x,y,null);
    	}
    }
    All i want it to do is show the correct sprite on the screen. but all it does is open a Window with a black background.

    I think the problem lies within my lack of understanding how to implement a double buffer correctly ( first time trying here ;)

    any suggestions would be awesome!

    thanks, kam
    Last edited by Kamlanie; 06-05-2012 at 09:15 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Double Buffering help

    If you use Swing classes, they use double buffering.

    The posted code is missing classes and import statements and will not compile.
    Last edited by Norm; 06-05-2012 at 08:58 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Kamlanie is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Double Buffering help

    i only added custom imports to the code above

    here are the rest of the imports to the code i posed...

    Main.java imports:
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import javax.swing.*;
    BufferedImageLoader.java
    Java Code:
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO
    add those and try it

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Double Buffering help

    what about the missing class?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Kamlanie is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Double Buffering help

    oops, forgot the renderer... sorry...

    Renderer.java

    Java Code:
    package com.kam.render;
    
    import java.awt.Graphics;
    import java.awt.Image;
    
    public class Renderer {
    
    	
    	public static void draw(Graphics g,Image i,int x, int y){
    			g.drawImage(i,x,y,null);
    	}
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Double Buffering help

    Why so many 1 line classes and methods?

    I'm not familiar with the BufferStrategy classes that you are trying to use. I am able to get an image drawn by removing method calls and simplifying the code to use the image returned by ImageIO. Then go back through the code and add back in the removed methods one by one until you have resolved the problems.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Kamlanie is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Double Buffering help

    i started this project this morning, the reason i have so many classes that dont have much at the moment, is because i want as much reusable classes as possible for future games / projects.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Double Buffering help

    If a class only contains one line of code, that seems expensive and confusing.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Kamlanie is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Double Buffering help

    ok so i figured it out, but having a new problem.

    Here is the updated code:

    Entity.java
    Java Code:
    package com.kam.entity;
    
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.Image;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    
    public class Entity {
    
    	protected int x;
    	protected int y;
    	public Image image;
    
    	/**
    	 * 
    	 * @param i
    	 *            sprite buffered image (use SpriteSheet.grabSprite())
    	 * @param x
    	 *            X position to draw sprite
    	 * @param y
    	 *            Y position to draw sprite
    	 * 
    	 **/
    	public Entity(BufferedImage bi, int x, int y) {
    		this.x = x;
    		this.y = y;
    
    		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    		this.image = gc.createCompatibleImage(bi.getWidth(), bi.getHeight(), Transparency.BITMASK);
    
    		image.getGraphics().drawImage(bi, 0, 0, null);
    	}
    
    	public void draw(Graphics g) {
    		g.drawImage(this.image, x, y, null);
    	}
    
    }
    for some reason if i put the position X and Y of the sprite to 0 in Entity e = new Entity(ss.grabSprite(0, 0, 15, 15), 0, 0); inside the Main.java class, it shows above the screen.

    from what i understood, it calculates the location of the sprite biased on the upper left? if this is so, then why when i put it at 0x and 0y its invisible because it is above the screen.

    any ideas?

    to get it to show in the upper left part of the screen, the 15x15 sprite has to be at 3x, 25y location... O_o im confused
    Last edited by Kamlanie; 06-06-2012 at 10:14 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Double Buffering help

    For debugging Add a println to print the x,y values that the drawImage() method uses when it draws the image.

    The image is inside the black screen for me.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Double Buffering
    By Macpwnage in forum Java 2D
    Replies: 8
    Last Post: 07-18-2011, 09:17 PM
  2. Help with double Buffering
    By Kyle227 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2010, 09:08 PM
  3. Double Buffering an Application?
    By Moncleared in forum New To Java
    Replies: 8
    Last Post: 09-14-2009, 04:45 AM
  4. Double Buffering
    By dunff2k in forum SWT / JFace
    Replies: 1
    Last Post: 11-14-2008, 03:26 PM
  5. [SOLVED] Double Buffering
    By Zosden in forum AWT / Swing
    Replies: 1
    Last Post: 07-24-2008, 02:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •