Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
Like Tree5Likes

Thread: Re-painting

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

    Default Re: Re-painting

    Thanks god it's working I love you.

    edit:
    For moving the picture smoothly an fast I did something like that:

    Java Code:
    if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
    		{
    			for(int k=0;k<10;k++)
    			{
    				this.posX -= 1;
    				repaint();
    			}
    		}
    It moves fast but I see the picture before the loop and after and not in every iteration of the loop.
    Do you know why?
    btw I'm doing it because moving pixel by pixel is very slow.
    Last edited by tnrh1; 11-08-2012 at 05:22 PM.

  2. #22
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Re-painting

    Quote Originally Posted by tnrh1 View Post
    Thanks god it's working I love you
    I have been called many things in my life, but god wasn't one of those... Glad that you got it working.
    tnrh1 likes this.

  3. #23
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Re-painting

    That's because you are trying to do GUI/painting stuff while you are on the infamous EDT. You can either move the workload using the earlier mentioned Swing timer or just do this.posX -=10;
    Last edited by SurfMan; 11-08-2012 at 06:22 PM. Reason: Replaced AWT with EDT (What was I thinking)

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

    Default Re: Re-painting

    The diffrence between a good video camera to a bad one is the amount of pictures it takes in a second.
    Doing this " this.posX -=10;" will reduce it. I want to see my picture moving every single pixel on the screen BUT after 1 click it will move something like 10 pixels 1 by 1.

  5. #25
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Re-painting

    Nice analogy. :) Go for the Swing Timer solution then, like Darryl Burke suggested in post #6.

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Re-painting

    Quote Originally Posted by SurfMan View Post
    That's because you are trying to do GUI/painting stuff while you are on the infamous AWT. You can either move the workload using the earlier mentioned Swing timer or just do this.posX -=10;
    I think you mean EDT.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #27
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Re-painting

    Quote Originally Posted by Tolls View Post
    I think you mean EDT.
    True. It's been a long day :)

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

    Default Re: Re-painting

    I'm trying to use the Timer class in my game but I can't figure out how to use it correctly.
    I got the same result as I mentioned above.
    Java Code:
    public class MyPanel extends JPanel implements KeyListener
    {
    	private Timer timer;
    	private Image img1;
    	private int posX;
    	private int posY;
    	private ActionListener al;
        public MyPanel() 
        {
        	this.addKeyListener(this);
            this.posX = (getPreferredSize().width/2)-(29/2);
    		this.posY = getPreferredSize().height-70;
    		this.img1 = getToolkit().getImage("kenny.gif");
    		this.setFocusable(true);
    		al = new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) 
    			{
    				repaint();
    			}
    		};
    		timer = new Timer(100,al);
    		timer.setInitialDelay(1);
    		timer.start();
        }
    
        public void paint(Graphics g)
        {
        	super.paint(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)
    		{
    			for(int k=0;k<10;k++)
    			{
    				this.posX -= 1;
    				//repaint();
    			}
    		}
    		else
    			{
    				this.posX += 1;
    				//repaint();
    			}
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  9. #29
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Re-painting

    Nitpick: you should override paintComponent, not paint.

    But you aren't using the Timer how we're implying you should. Simply calling repaint() isn't enough. You need to move your image in the Timer.

    Actually, you should set up a game logic function. In that game logic function, you should check which keys are pressed, and move the sprite accordingly, then call repaint(). Then all you have to do is call that function from a Timer repeatedly - 30 times a second is a good starting point.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Re-painting

    I do have that kind of function, the key pressed function "check which keys are pressed, and move the sprite accordingly, then call repaint()".
    Then you say that I need to call that function from a timer, what do you mean?how can I call to another function from a timer?timer is an instance not a function.
    I think I got you wrong.

  11. #31
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Re-painting

    No, the KeyListener is only called when the user presses a key. That's going to make it hard to do things like animations, game logic, etc.

    Instead, simply update a set of booleans that tell you which keys are pressed from the KeyListener. Then from the Timer, call a function that updates the state of the game based on those booleans. That way you can do smooth animations over time instead of only when the user presses a key.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Re-painting

    Ok so generally your suggestion is to repaint every x time if some kind of boolean that represent a key pressed is true, right?
    Now the only thing I still don't understand is "Then from the Timer, call a function that updates" how can I call with a timer instance to a function that written in another class?

  13. #33
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Re-painting

    Quote Originally Posted by tnrh1 View Post
    Ok so generally your suggestion is to repaint every x time if some kind of boolean that represent a key pressed is true, right?
    Now the only thing I still don't understand is "Then from the Timer, call a function that updates" how can I call with a timer instance to a function that written in another class?
    I mean from the Timer's ActionListener's actionPerformed() method. The skeleton of your game will look something like this:

    Java Code:
    class Game extends JPanel{
    
    private boolean upPressed;
    //booleans for down, left, right, etc
    
    public Game(){
       //initialize, background color, etc
    
       addKeyListener(new KeyListener(){
          public void keyPressed(KeyEvent e){
             if(e.key ==up){
                upPressed = true;
             }
             //logic for other keys
          }
    
          public void keyReleased(KeyEvent e){
             if(e.key == up){
                upPressed = false;
             }
             //logic for other keys
          }
       });
    
    
       Timer t = new Timer(32, new ActionListener(){
             public void actionPerformed(ActionEvent e){
                update();
             }
          });
    
          t.start();
    
    }
    
    public void update(){
       if(upPressed){
          //move image up
       }
       //check other booleans, update game
       repaint();
    }
    
    
    public void paintComponent(Graphics g){
       //paint your game
       //no game logic here! Just painting!
    }
    }
    tnrh1 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!

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

    Default Re: Re-painting

    It's working now perfectly but instead of overriding paintComponent I overrided paint since it got the first error i had in #1
    Just wanted to tell you how much I appreciate your help and support and that the reason why I want to help other people and promoting this amazing forum.

  15. #35
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Re-painting

    Quote Originally Posted by tnrh1 View Post
    It's working now perfectly but instead of overriding paintComponent I overrided paint since it got the first error i had in #1
    Just wanted to tell you how much I appreciate your help and support and that the reason why I want to help other people and promoting this amazing forum.
    No problem. But you should really stick with overriding paintComponent() and not paint(). The paint() method handles things like double buffering, so you should leave it alone. Stick with paintComponent, and just make sure you have the call to super in there as the first line.
    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. #36
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Re-painting

    ooooops I called to super.paint instead of super.paintComponent, it's working now as you said :)

  17. #37
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Re-painting

    Shouldn't that be just super.paint() instead of super.paintComponents()?
    Yeah, I think so. I do all of my drawing in a component (JPanel subclass) inside the paintComponent method, so in that case the syntax is correct - but if using just the "paint()" method, then a call to the super's paint method makes more sense.

Page 2 of 2 FirstFirst 12

Similar Threads

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