Results 1 to 4 of 4
  1. #1
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Key/mouse movement

    I'm trying to get some shapes to either follow the mouse or move when a key is pressed... So far this is what I have, and my System.out.println("..."); when moved or pressed works alright, but its not repainting or moving the graphics.

    Java Code:
    package Assignment04;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JApplet;
    public class animation extends JApplet implements KeyListener, MouseMotionListener, MouseListener, Runnable
    {
    	
    	public void init ()
    	{
    		setBackground(Color.BLACK);
    		setSize(400,400);
    		
    	}
    	int x = 0, y = 0, mx=0, my=0;
    	public void paint(Graphics g)
    	{
    		
    		g.setColor(Color.blue.darker());
    		g.fillOval(x, y, 50, 100);
    		addKeyListener(this);
    		addMouseMotionListener(this);
    		addMouseListener(this);
    		this.setFocusable(true);
    		g.fillRect(mx,my,40,40);
    		repaint();
    		
    	}
    	public void run() 
    	{
    		while(true) 
            { 
                try 
                { 
                    Thread.sleep(20);  // is this necessary and/or in the right spot to "animate" movement correctly?
                } 
                catch(Exception e) {} 
                repaint();
            }
    		
    	}
    
    	public void keyTyped(KeyEvent e) 
    	{
    		int key = e.KEY_PRESSED;
    		if(key!=0)  //same issue as the snippet under this, this was just to test if input was being read. Would like to change so that certain keys do certain movement. i.e. a is left, d is right, w is up, s is down.
    		{
    			x = x+10;
    			System.out.println("Pressed key"+ x +"," + y);
    			
    		}
    	}
    
    	public void keyPressed(KeyEvent e) 
    	{
    		int key = e.getKeyChar();
    		if(key == KeyEvent.KEY_PRESSED) //initially here, I had KeyEvent.VK_D, but it wouldnt ever execute, I assume since "Pressed key" never printed....? How would I change that?
    		{
    			//x = x+10;
    			System.out.println("Pressed key");
    		}
    		
    	}
    
    	public void keyReleased(KeyEvent e) 
    	{
    		
    	}
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseDragged(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseMoved(MouseEvent e) {
    		mx=this.getX();
    		my=this.getY();
    		System.out.println("Moved...");
    		repaint();	
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're killing your app by adding all those listeners every time the applet repaints. This will overload you with listeners and will surely lead to a crash. A few recommendations:

    1) painting methods such as paint and paintComponent should be sleek and fast and should concern themselves with painting and nothing but painting. Never add listeners in there, never read from files, don't set focus in there, avoid creating objects in these methods.

    2) Your while loop is unnecessary and potentially dangerous.

    3) Check out the tutorials on how to paint in Swing as you'll need to change some assumptions.

    4) as the tutorials will show you, do your drawing in a paintComponent method of a JPanel subclass, not directly in a top-level window.

    5) Class names should begin with a capital letter.

    6) repaint in a paint method? Are you sure you want to do that?

    Luck!

  3. #3
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    So if I want a listener to be added to a specific shape that I draw in the window, how do I do that? I guess either my teacher isn't very good at teaching this stuff or something, but I've been trying to figure this crap out all week and can't get any results, and his examples, unless I'm interpreting them wrong, are doing things that you're saying I shouldn't be..

    Where are the swing paint tutorials? I googled, found very little that helps what I'm trying to do.

  4. #4
    Dr.Fox is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Car Game - Handling movement
    By Rectal Exambot in forum New To Java
    Replies: 2
    Last Post: 08-24-2010, 02:23 AM
  2. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM
  3. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  4. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 12:48 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 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
  •