Results 1 to 6 of 6
  1. #1
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Animation coding

    For my school I have to code a panel that will repaint when I move with my mouse. Now I've added the MouseMotionListener etc. but it doesn't seem to work when I compile it..
    Any suggestions or ideas what I did wrong? Here's the code :)


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class Mouse {
    
    	int x= 70;
    	int y = 70;
    	
    	public static void main(String[] args) {
    		Mouse x = new Mouse();
    		x.go();
    	}
    	
    	public void go(){
    		
    		JFrame frame = new JFrame("Animation");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		MyDrawPanel2 z = new MyDrawPanel2();
    		
    		JButton button = new JButton ();
    		
    		frame.getContentPane().add(z);
    		frame.setSize(300,300);
    		frame.setVisible(true);
    	}
    
    
    	class MyDrawPanel2 extends JPanel implements MouseMotionListener{
    	
    		public MyDrawPanel2(){
    		this.addMouseMotionListener(this);
    		}
    	
    	public void PaintComponent(Graphics g){
    		
    
    		g.setColor(Color.white);
    		g.fillRect(0,0, this.getWidth(), this.getHeight());
    		
    		g.setColor(Color.green);
    		g.fillOval(x,y,40,40);
    		
    		}
    	
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		
    	}
    @Override
    	public void mouseMoved(MouseEvent e) {
    	x = e.getY();
    	y = e.getX();
    	repaint();
    		
    	}
    
    }//end inner class
    }//end outer class

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

    Default Re: Animation coding

    paintComponent has a lower case 'p'.

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

    Default Re: Animation coding

    Quote Originally Posted by JavaJimme View Post
    but it doesn't seem to work when I compile it.
    If your compiler complains, it tries to tell you exactly what is wrong; if you don't understand what it is trying to tell you, copy the message here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Animation coding

    Quote Originally Posted by JosAH View Post
    If your compiler complains, it tries to tell you exactly what is wrong; if you don't understand what it is trying to tell you, copy the message here.

    kind regards,

    Jos
    The thing is, that the compiler won't complain in this case. PaintComponent(Graphics g) is now just another method. "It doesn't work" is sort of valid here, although better would be: "I don't see the green circle that I expected, but only a <insert what you see here>." :)

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

    Default Re: Animation coding

    According to the OP things 'didn't work' when s/he tried to compile the code, so I assumed compilation errors ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Animation coding

    Omg how can I miss such a retarded mistake :( Thanks a bunch you guys! paintComponent was indeed the problem ;)

Similar Threads

  1. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 05:52 PM
  2. help with animation coding
    By loopsnhoops in forum New To Java
    Replies: 2
    Last Post: 02-22-2011, 10:15 PM
  3. Replies: 4
    Last Post: 10-25-2010, 03:49 PM
  4. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 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
  •