Results 1 to 7 of 7
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default My BufferedImage Flickering

    here is my mini Game Code (Mario Project..) but for a reason my BufferedImage is Flickering.. but i can't figure it out why!

    code :

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    @SuppressWarnings("serial")
    class MarioGameFrame extends JFrame implements KeyListener {
    	static boolean gameOn = true;
    	BufferedImage marioSprite ;
    	BufferedImage sprite;
    	static int x = 0;
    	
    	
    	public MarioGameFrame(){
    		setSize(500,500);
    		setTitle("Super Mario Bros v1.0");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		unit();
    		addKeyListener(this);
    		
    	}
    	
    	public void unit(){
    		
    		try {
    			marioSprite = ImageIO.read(getClass().getResource("g0.png"));
    		} catch (IOException e) {
    			System.out.println("Sh*t!");
    		}
    	}
    	
    	public void paint(Graphics g){
    		super.paint(g);
    		sprite = marioSprite.getSubimage(0, 15, 20, 20); 
    		g.drawImage(sprite,x, 435,50,50 ,null);
    	}
    	
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			x = x + 15;	
    		}
    	}
    	
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    	}
    	
    	public boolean gameOver(){
    		return gameOn;
    	}
    }
    
    class MarioGame{
    	public static void main(String args []) throws InterruptedException{
    		MarioGameFrame element = new MarioGameFrame();
    		element.setVisible(true);
    		element.setFocusable(true);
    		
    		while(element.gameOver()){
    			element.repaint();
    			Thread.sleep(10);
    		}
    	}
    }

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

    Default Re: My BufferedImage Flickering

    Moved from New to Java

    db
    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,188
    Rep Power
    19

    Default Re: My BufferedImage Flickering

    Custom painting isn't done in a JFrame, but in an extended JPanel or JComponent. Learn the right way to do it: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Additionally, it might be more efficient to create and cache an array of subimages rather than extract a subimage every time through the painting routine.

    And catch blocks should print or log the exception's stack trace, which is meaningful for trouble shooting and debugging, not some meaningless and frivolous message.

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

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: My BufferedImage Flickering

    Well for one you should not be overriding paint() but paintComponent(). If you must use Swing you should also look into using a Swing Timer in stead of the endless game loop with a sleep() in it.

    BTW: be careful with confusing code, it has a tendency to haunt you and bite you later on:

    Java Code:
           while(element.gameOver()){
    I'd expect this:

    Java Code:
           while(!element.gameOver()){
    Have a look at the custom painting section of the Swing tutorial.

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    and also swing timer:

    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    edit: ninja'd! Well, also what Deliberate Bully says :) Although if I remember correctly subImage does not in fact return a new image with copied over pixel data but rather a "window into" the original image. It should not impact performance. But I may be wrong, its been a long time ago since I used this stuff.
    Last edited by gimbal2; 07-25-2013 at 11:42 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: My BufferedImage Flickering

    Thx you all! i have a lot tou learn ;)

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

    Default Re: My BufferedImage Flickering

    Quote Originally Posted by gimbal2 View Post
    Although if I remember correctly subImage does not in fact return a new image with copied over pixel data but rather a "window into" the original image. It should not impact performance
    ... much. But it's still a method call and avoidable code execution.

    Cacheing an array or List of references to subimages (that would still share the raster date of the spritesheet) may be nanoscopically faster, but would certainly lead to cleaner, more maintainable code when a certain sprites is to be conditionally shown. I would consider that an efficiency boost.

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

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: My BufferedImage Flickering

    Well in this case the call can simply be moved to unit(), indeed no need to do it every paint() execution.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Flickering on a 2D Sprite
    By driiper in forum Java 2D
    Replies: 5
    Last Post: 05-06-2012, 05:30 PM
  2. Replies: 3
    Last Post: 02-29-2012, 09:38 PM
  3. Screen Flickering
    By dewitrydan in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 07:45 PM
  4. flickering problem
    By dimril in forum Java 2D
    Replies: 5
    Last Post: 06-11-2010, 08:52 AM
  5. Applet flickering
    By samson in forum Java 2D
    Replies: 3
    Last Post: 09-21-2007, 10:51 PM

Posting Permissions

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