Page 1 of 2 12 LastLast
Results 1 to 20 of 37
Like Tree5Likes

Thread: Re-painting

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re-painting

    Hello everyone,
    I have an image that displayed on a JFrame.
    I added a key listener and when i press right or left the image moves but I don't know how to "clean" the last place the image been.
    Here is a picture to demonstrate:
    Re-painting-bug.png
    So how do I clean my tracks?

    Thanks in advacned.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Re-painting

    Sounds like you're missing a call to super.paintComponent(), but without seeing an SSCCE, I'm only guessing.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: Re-painting

    I like it the way it is: a bunch of tiny Eskimos, neatly lined up and looking scared to death (maybe they just saw a ferocious yeti ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Re-painting

    Quote Originally Posted by JosAH View Post
    I like it the way it is: a bunch of tiny Eskimos, neatly lined up and looking scared to death (maybe they just saw a ferocious yeti ;-)

    kind regards,

    Jos
    I think it looks like an Eskimo Caterpillar!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Re-painting

    Thank you!
    It's working now but I got another problem now.
    When I click the picture disapear and then re-apear in the new position, this proccess takes to long so my picture kinda disapear for alot of time (less the a second of course)
    so when I give a long press on the button the picture disapear.

    Java Code:
    public void paint(Graphics g) 
    	 { 
    		 super.paintComponents(g);
    		 g.drawImage(this.img1, this.posX, this.posY, null);
    	 }

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

    Default Re: Re-painting

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

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Re-painting

    I read this 2 links.
    I know how to work with threads, and looks like the answer is in the first link.
    But I don't understand how to use the Timer class.. it needs an actionListener as a parameter but my image moves by the keys which is a key listener.
    And I'm confused how to use the timer.. I'm confused.

    edit:
    I tryed to make a thread let it sleep for a while then re paint but I still get the same results.

    Something like that:

    Java Code:
    public void keyPressed(KeyEvent arg0) {
    		switch(arg0.getKeyCode())
    		{
    			case KeyEvent.VK_LEFT:
    			{
    				this.posX -= 1;
    				Update u1 = new Update(this);
    				
    			}
    		}
    Java Code:
    public class Update extends Thread
    {
    	private Game g1;
    	public Update(Game g1)
    	{
    		this.g1 = g1;
    		this.run();
    	}
    	public void run()
    	{
    		
    		try {
    			sleep(10);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		this.g1.repaint();
    	}
    }
    I tryed diffrent values for the sleep method.
    Last edited by tnrh1; 11-08-2012 at 12:34 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Re-painting

    Quote Originally Posted by tnrh1 View Post
    Click image for larger version. 

Name:	bug.png 
Views:	19 
Size:	1.0 KB 
ID:	4211
    Oh my God!
    They duplicated Kenny!

    (sorry, couldn't resist)
    PhQ likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Re-painting

    Super.paintComponent() clears the component, then draws the new picture on it. That's what we see as flicker. JComponent.paintComponent() calls JComponentUI.update() which clears the component:

    Java Code:
    public void update(Graphics g, JComponent c) {
      if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, c.getWidth(),c.getHeight());
      }
      paint(g, c);
    ]
    You should override de update(Graphics) method and implement a double buffer technique, It's not as hard as it sounds. What you basically do is create an image the size of you component, draw on it, and when it's done, copy it to the Graphics of the component.
    Here's (Java Cooperation: double buffering) a link I found googling for "java double buffer paint". Experiment with that.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Re-painting

    Quote Originally Posted by SurfMan View Post
    Super.paintComponent() clears the component, then draws the new picture on it. That's what we see as flicker. JComponent.paintComponent() calls JComponentUI.update() which clears the component:

    Java Code:
    public void update(Graphics g, JComponent c) {
      if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, c.getWidth(),c.getHeight());
      }
      paint(g, c);
    ]
    You should override de update(Graphics) method and implement a double buffer technique, It's not as hard as it sounds. What you basically do is create an image the size of you component, draw on it, and when it's done, copy it to the Graphics of the component.
    Here's (Java Cooperation: double buffering) a link I found googling for "java double buffer paint". Experiment with that.
    Sorry, but I disagree with this advice. Darryl had it right when he recommended understanding Swing's threading better. It seems like the OP is tying up the EDT, which is A Very Bad Thing. But like I said, without an SSCCE, we're just guessing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Re-painting

    Quote Originally Posted by KevinWorkman View Post
    Sorry, but I disagree with this advice. Darryl had it right when he recommended understanding Swing's threading better. It seems like the OP is tying up the EDT, which is A Very Bad Thing. But like I said, without an SSCCE, we're just guessing.
    I think we're both right. The EDT is tied up, and the image is not cleared when Kenny is moved. :) An SSCCE would indeed be appreciated.

  12. #12
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Re-painting

    It's not such a long code so I past it all:

    Game class
    Java Code:
    package pack1;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.sql.Time;
    
    import javax.swing.*;
    
    public class Game extends JFrame implements KeyListener
    {
    	private Image img1;
    	private int posX;
    	private int posY;
    	public Game()
    	{
    		
    		setSize(900, 900);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		setVisible(true);
    		addKeyListener(this);
    		
    		this.posX = (getWidth()/2)-(29/2);
    		this.posY = getHeight()-70;
    		this.img1 = getToolkit().getImage("kenny.gif");
    
    	}
    	 public void paint(Graphics g) 
    	 { 
    		 super.paintComponents(g);
    		 g.drawImage(this.img1, this.posX, this.posY, null);
    	 }
    
    	public void incX(int x){
    		this.posX += x;
    	}
    	
    	@Override
    	public void keyPressed(KeyEvent arg0) {
    		switch(arg0.getKeyCode())
    		{
    			case KeyEvent.VK_LEFT:
    			{
    				this.posX -= 1;
    				new Update(this);	
    			}break;
    			
    			case KeyEvent.VK_RIGHT:
    			{
    				this.posX += 1;
    				new Update(this);
    			}
    		}
    		
    	}
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		//System.out.println(arg0);
    		
    	}
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		//System.out.println(arg0);
    		
    	}
    
    	
    	public static void main(String[] args) 
    	{
    		new Game();
    	}
    
    }
    Update class
    Java Code:
    package pack1;
    
    public class Update extends Thread
    {
    	private Game g1;
    	public Update(Game g1)
    	{
    		this.g1 = g1;
    		this.run();
    	}
    	public void run()
    	{
    		
    		try {
    			sleep(10);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		this.g1.repaint();
    	}
    }
    Again, the problem is when I click left or right the image DISAPEAR for x time and then re-appear in the new position and this proccess takes to much time so it looks bad you can test it.
    I can't show you a screen shot because of this situation xD

    Thanks in advanced!

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Re-painting

    Yeah, you're tying up the EDT. You aren't doing threading correctly at all, so you're doing everything on the EDT. That's causing your program to become unresponsive.

    First of all, follow this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) - you should be extending JPanel, not JFrame.

    Then get rid of your Thread class completely. For your purposes, you should be able to simply call repaint() from the KeyListener. Eventually you'll probably want to use a Swing Timer.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Re-painting

    Do not use run() on a Thread. Instead, use start() to actually spawn the new Thread (Update.class, line 9).

    Other than that, with the current code, the image is visible all the time here when I move it left and right, so I don't have your problem. What version JDK are you using?

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Re-painting

    Quote Originally Posted by SurfMan View Post
    Do not use run() on a Thread. Instead, use start() to actually spawn the new Thread (Update.class, line 9).

    Other than that, with the current code, the image is visible all the time here when I move it left and right, so I don't have your problem. What version JDK are you using?
    The threading issues are going to make this program's behavior unpredictable on different platforms. Threads are completely unnecessary here, so I'd highly recommend simply taking the threading out entirely.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Re-painting

    Quote Originally Posted by KevinWorkman View Post
    The threading issues are going to make this program's behavior unpredictable on different platforms. Threads are completely unnecessary here, so I'd highly recommend simply taking the threading out entirely.
    True. A simple repaint() will do for now. I agree with all your comments above. I wasn't trying to change his mind, or yours for that matter, just pointed him to a basic Thread error.
    KevinWorkman likes this.

  17. #17
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Re-painting

    Ok I followed that guide and I'm using the JPanel now but it's leading me to another error, my keyListener doesn't respond to any key pressed.

    Game:
    Java Code:
    package pack1;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Game 
    {
    	public static void main(String[] args) 
    	 {
    	        SwingUtilities.invokeLater(new Runnable() 
    	        {
    	            public void run() 
    	            {
    	                createAndShowGUI(); 
    	            }
    	        });
    	    }
    
    	    private static void createAndShowGUI() 
    	    {
    	        JFrame f = new JFrame("My game");
    	        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	        f.add(new MyPanel());      
    	        f.pack();
    	        f.setLocationRelativeTo(null);
    	        f.setVisible(true);
    	    }
    }
    MyPanel:
    Java Code:
    package pack1;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JPanel;
    
    public class MyPanel extends JPanel implements KeyListener
    {
    
    	private Image img1;
    	private int posX;
    	private int posY;
    	
        public MyPanel() 
        {
        	this.addKeyListener(this);
            this.posX = (getPreferredSize().width/2)-(29/2);
    		this.posY = getPreferredSize().height-70;
    		this.img1 = getToolkit().getImage("kenny.gif");
        }
    
        public void paint(Graphics g)
        {
        	super.paintComponents(g);
        	g.drawImage(this.img1, this.posX, this.posY, null);
        }
        
        
        public Dimension getPreferredSize() 
        {
            return new Dimension(900,900);
        }
    
    	@Override
    	public void keyPressed(KeyEvent arg0) 
    	{
    		if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
    			this.posX --;
    		else
    			this.posX++;
    		repaint();
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Re-painting

    Ah, yeah. You have to make sure your JPanel is focusable, then you need to request that it has the focus in the window. Both methods are in the API: JPanel (Java Platform SE 7 )
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Re-painting

    Ok I added this line: this.setFocusable(true);
    It's working now but I'm turning to my first error now:
    Re-painting-untitled.png

  20. #20
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Re-painting

    It doesn't seem to make sense to call super.paintComponents() inside a paint() method. Shouldn't that be just super.paint() instead of super.paintComponents()?

Page 1 of 2 12 LastLast

Similar Threads

  1. Grid Painting?
    By iCon09 in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 07:04 AM
  2. painting whole label
    By simprepol in forum AWT / Swing
    Replies: 9
    Last Post: 01-27-2011, 08:10 AM
  3. Painting Problem!
    By Jcbconway in forum Advanced Java
    Replies: 3
    Last Post: 11-17-2010, 04:14 AM
  4. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  5. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 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
  •