Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Exclamation Images won't clear

    Hey!

    RiokuTheSlayer here,and i have a bit of a problem.
    I'm making a game and everything,and for some reason i get repeatetive images. in other words,the screen won't clear the images already drawn. when i use repaint(); it just makes the screen flash,not doing anything else. Heres my render code:

    Java Code:
            public void render() {
        	Graphics g = screen.getGraphics();
        	
        	
        	//Drawing things.
        	g.setColor(new Color(50 ,50 ,255));
        	g.fillRect(0, 0, pixel.width, pixel.height);
        	
        	Level.render(g);
        	Character.render(g);
           	g = getGraphics();
           	
        	g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);
            g.dispose();
    I attached an image to this page,with the result.(note,i use mario as a replacment until later. Also,the character starts at the lop left.)
    The character is always in the center,so his image isnt like the walls and roof.
    Attached Thumbnails Attached Thumbnails Images won't clear-img.png  

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Images won't clear

    For lack of seeing any of your other code I will assume you are rendering this inside a JPanel which is in a JFrame. Then you need to override JPanel's paintComponent method:

    Java Code:
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(50 ,50 ,255));
        g.fillRect(0, 0, pixel.width, pixel.height);
        Level.render(g);
        Character.render(g);
        g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);
    }
    Check out the following link for assistance in painting.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Yeah,I'm using JFrame. I'll try that.

  4. #4
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Wait,where,exactly,do i put that code? because if i replace it with the render method,it just gives me a bunch of errors when i replace all the other render() to paintComponent().

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Images won't clear

    I recommend *not* painting in the JFrame. Subclass a JPanel and add that to the JFrame. Then override paintComponent in your subclass.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Ah,got it. thanks.I dont know what i did wrong. i may just look back on the tutorial again.

  7. #7
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Angry Re: Images won't clear

    Okay,i'm honestly stumped. i tried,but i'm just to frustrated to think straight.

    Here's my main class:
    Java Code:
    package net.ulixava.RetroHunter;
    
    import java.applet.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class Component extends Applet implements Runnable{
    	private static final long serialVersionUID = 1L;
    	
    	private Image screen;
    	private static int pixelSize = 1;
    	
    	
    	public static double dir = 0;
    	public static double sX = 0, sY = 0;
    	public static Dimension size = new Dimension(700, 500);
    	public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);
    	public static String name = "Retro Hunter";
    	public static Level level;
    	public static Character character;
    	
    	public static boolean isRunning = false;
    	public static boolean isMoving = false;
    	
    	public Component() {
    		setPreferredSize(size);
    		
    		addKeyListener(new Listening());
    	}
    	
        public void start() {
        	//Defining Objects etc.
        	new Tile(); //Loading Images
        	level = new Level();
        	character = new Character(Tile.tileSize,Tile.tileSize * 2);
        	
        	//Start game loop.
        	new Thread(this).start();
        	isRunning = true;
        }
        
        public void stop() {
        	isRunning = false;
        }
        
        public static void main(String args[]) {
        	Component component = new Component();
        	
        	JFrame frame = new JFrame();
        	frame.add(component);
        	frame.pack();
        	frame.setTitle("Retro Hunter V0.01");
        	frame.setResizable(false);
        	frame.setLocationRelativeTo(null);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setVisible(true);
        	
        	component.start();
        }
        
        public void tick() {
        	Level.tick();
        	Character.tick();
        	
        }
        
        public void render() {
        	Graphics g = screen.getGraphics();
        	
        	
        	//Drawing things.
        	g.setColor(new Color(50 ,50 ,255));
        	g.fillRect(0, 0, pixel.width, pixel.height);
        	
        	Level.render(g);
        	Character.render(g);
           	g = getGraphics();
           	
        	g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);
            g.dispose();
        }
        public void run() {
        	screen = createVolatileImage(pixel.width, pixel.height);
        	
        	while(isRunning) {
        		tick();
        		render();
        		try {
        			Thread.sleep(5);
        		} catch(Exception e) {
        			
        		}
        	}
        }
    }
    I honestly don't know anything about what's going on,because i've used this same code to render every time i made a different game.(Oh,yes,it is unorganized in parts...i need to re-organize it later.)

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Images won't clear

    Ok, you are rendering to an Image. But you are then grabbing the graphics context of JApplet (via getGraphics) in the render method. What you should do is override paintComponent(Graphics g) as follows:

    Java Code:
    public void paintComponent(Graphics g) {
        if (screen == null)
                return;
        super.paintComponent(g);
        g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);
    }
    
    // in your render() method
    // replace the following
            g = getGraphics();
            g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);
    
    // with
                 repaint();
    I can't guarantee this will solve your problem but it is the proper way to draw images.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Okay,i did this,but now,where do i call the paintComponent method? because the g.drawImage is pretty much the heart of the rendering method.but when i replace what you said,and even added that,it just gives me the white screen.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Images won't clear

    You *don't* call the paintComponent(...) method. You call repaint() and let the component repaint itself.

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

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

  11. #11
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Well,what you have told me(both of you) Only gives me a flashing white screen,with my game in it,with the "bug" still present. I'm starting to think its an issue with the tiles and block's rendering methods.I'll check them out.
    Last edited by RiokuTheSlayer; 05-22-2013 at 07:59 AM.

  12. #12
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    It's not re-printing them! its stretching the blocks!I may have found a fix!

  13. #13
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Okay,new-ish problem. I've figured out what the problem really is.The images are stretching from the bottom,as if the bottom doesn't want to move.

    Java Code:
    	public void render(Graphics g){
    		if(id != Tile.air){
    			g.drawImage(Tile.tileset_terrain,(int) (x - Component.sX),(int) (y - Component.sY),(int) (x + width),(int) (y + height), id[0] * Tile.tileSize, id[1] * Tile.tileSize, id[0] * Tile.tileSize + Tile.tileSize, id[1] * Tile.tileSize + Tile.tileSize, null);
    		}
    	}
    }
    Anything going wrong there?Because I've just copied the code from the character rendering,removing the second block...(because the character is two blocks tall.)
    and the character works perfectly.

  14. #14
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Images won't clear

    Solved! NOW SOMEONE CLOSE THE POST.

Similar Threads

  1. SWT Canvas Clear
    By jionnet in forum SWT / JFace
    Replies: 1
    Last Post: 08-05-2014, 08:59 AM
  2. Replies: 0
    Last Post: 11-14-2012, 12:15 PM
  3. Replies: 0
    Last Post: 12-16-2010, 06:14 PM
  4. Replies: 4
    Last Post: 12-16-2010, 05:32 PM
  5. Clear Method
    By sweetpea123 in forum New To Java
    Replies: 3
    Last Post: 03-28-2010, 02:51 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
  •