Results 1 to 2 of 2
  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Unhappy Help with double Buffering

    when I was creating some animation with graphics I noticed that is would flicker. So I looked up the problem and found out you had to use double buffering. So I put double buffering in the code and ran the application again. When i ran it, it was a lot better than it was before except 2 problems. One is that it would flicker a little and it might stop for 1\2 of a second. Is it the code or the computer. It will also slow down a little too. oh yeah i forgot to say that I'm moving a small red ball up the screen and when it reached the top it goes to the bottom and repeats itself. The second problem is that it looks like it is trailing behind a little. Like when you move your hand really fast across your computer screen, it looks like your hand trails behind. That is what the red ball looks like when it moves up the screen. I don't know if it is suppose to that or it's something else. Here is the code. It isn't that good because this was a test.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Bullet extends JFrame {
    	Thread thread;
    	boolean finish = false;
    	
    	private int locationY;
    	private Image image;
    	private Graphics dbg;
    	
    	public Bullet(){
    		super("Bullet demo");
    		setSize(200,700);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		locationY = 700;
    		image = createImage(getWidth(),getHeight());
    		dbg = image.getGraphics();
    		thread = new Thread(run);
    		thread.start();
    		
    		
    	}
    	
    	public void update(Graphics g){
    		paint(g);
    	}
    	
    	
    	
    	public void paint(Graphics g){
    		setBackground(Color.CYAN);
    		
    		// clears the  screen
    		dbg.setColor (getBackground());
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    		//draws the shapes
    		dbg.setColor(Color.RED);
    		dbg.fillOval(50, locationY, 10, 10);
    		// draws the image on the screen
    		g.drawImage(image, 0, 0, this);
    	}
    	
    	Runnable run = new Runnable(){
    		public void run(){
    			while(!finish){
    				for(int x = 0; x<700; x++){
    					try {Thread.sleep(3);} catch (InterruptedException e) {System.out.println(e);}
    					locationY--;
    					repaint();
    				}
    				locationY = 700;
    				}
    		}
    	};
    	
    	public static void main(String[] args){
    		new Bullet();
    	}
    }
    Just one more thing, is it possible to hide the mouse cursor when it enters the screen.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your flickering comes from doing your graphics all wrong (drawing directly in JFrame, using getGraphics, not drawing in a JPanel's paintComponent method, etc...). You don't need to explicitly do double buffering as it comes with Swing if you do the graphics correctly.

    You may wish to read the graphics tutorials before moving forward with this.

Similar Threads

  1. Help Please: SWT double buffering simple animation
    By darkthawt in forum SWT / JFace
    Replies: 1
    Last Post: 06-24-2010, 11:06 PM
  2. Double Buffering an Application?
    By Moncleared in forum New To Java
    Replies: 8
    Last Post: 09-14-2009, 04:45 AM
  3. Double Buffering
    By dunff2k in forum SWT / JFace
    Replies: 1
    Last Post: 11-14-2008, 03:26 PM
  4. [SOLVED] Double Buffering
    By Zosden in forum AWT / Swing
    Replies: 1
    Last Post: 07-24-2008, 02:28 AM
  5. Double Buffering problem
    By aprenz in forum Java Applets
    Replies: 0
    Last Post: 05-28-2008, 04:26 AM

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
  •