Results 1 to 3 of 3
  1. #1
    Zoid is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default Drawing a moving box with a BufferStrategy leaves a trace

    Hi Java Forums,

    my problem is that my program draws the box to the new position with every frame, but does not delete the old box.



    I use a BufferStrategy to buffer my Frames. The paintComponent method is called by the processingThread every 16 milliseconds. Here is a very simplified version of the code:

    Java Code:
    public class GFrame extends JFrame {
    
    	public static final int HEIGHT = 600, WIDTH = 1200;
    	private BufferStrategy strat;
    	private PlayerEntity player;
    
    	public GFrame(PlayerEntity player) {
    		super();
    		this.player = player;
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(WIDTH, HEIGHT);
    		this.setLocationRelativeTo(null);
    		this.setBackground(Color.BLACK);
    		this.setVisible(true);
    
    		this.createBufferStrategy(2);
    		this.strat = getBufferStrategy();
    		
    	}
    
    	public void paintObjects() {
    		Graphics g = strat.getDrawGraphics();
    		g.setColor(player.getColor());
    		g.fillRect((int) player.getXPos(), (int) player.getYPos(), player.getWidth(),
    				player.getHeight());
    		g.dispose();
    		strat.show();
    	}
    }
    Java Code:
    public class GameThread implements Runnable {
    
    	@Override
    	public void run() {
    		while (running) {
    			timePast = System.currentTimeMillis() - cycleStartTime;
    
    			cycleStartTime = System.currentTimeMillis();
    
    			this.gameFrame.getPlayer().updatePosition(timePast);
    
    			gameFrame.paintObjects();
    
    			try { if (1000 - timePast > 0) Thread.sleep((1000 - timePast) / FRAMES_PER_SECOND);
    			} catch (InterruptedException e) {e.printStackTrace();}
    		}
    	}
    }
    The x and y coordinate are the correct and expected values:
    Java Code:
    50.0 - 50.0
    65.970894 - 49.035316
    81.93143 - 50.158432
    97.713776 - 52.78856
    113.24655 - 56.6269
    128.48276 - 61.51139
    ...
    When I try to overwrite the paint(Graphics g) method of the Component I only get 1 to 4 calls of it and then it goes to sleep or something. I don't know if it has something to do with the original problem but here is the code:

    Java Code:
    	@Override
    	public void paintComponents(Graphics g) {
    		super.paintComponents(g);
    		g.setColor(player.getColor());
    		g.fillRect((int) player.getXPos(), (int) player.getYPos(), player.getWidth(),
    				player.getHeight());
    		g.dispose();
    	}
    I hope someone will find a solution soon and I thank you in advance.

    Regards,
    Atlan aka Luca

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Drawing a moving box with a BufferStrategy leaves a trace

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Drawing a moving box with a BufferStrategy leaves a trace

    And note that paintComponent(...) is not the same method as paintComponents(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. NPE on each variable for BufferStrategy!
    By Elijah in forum Thread
    Replies: 2
    Last Post: 01-01-2012, 04:31 AM
  2. Replies: 4
    Last Post: 09-25-2010, 09:03 AM
  3. BufferStrategy
    By Shadowdude04 in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 02:59 PM
  4. Drawing moving object
    By dotabyss in forum Java 2D
    Replies: 1
    Last Post: 02-16-2010, 11:20 PM

Tags for this Thread

Posting Permissions

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