Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Move an object

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Move an object

    Hello, I need to move an object with the arrow key.
    This is my object class:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
    public class matka {
    	private int x;
    	private int y;
     
    	public matka (int x, int y)
    	{
    		this.x=x;
    		this.y=y;
    	}
     
    	public void Paint (Graphics g)
    	{
    		//Rectangle r=new Rectangle();
    		g.fillRect(x, y, 200, 20);
     
    	}
    	[COLOR="#FF0000"]public void moveL(int dx){
    		this.x+=dx;
     
    	}
    	public void moveR(int dx){
    		this.x-=dx;
     
    	}[/COLOR]
     
    }

    And here my main page:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;;
     
    public class BallWorld1 extends Frame {
     
    	private Ball aBall;
    	private matka aMatka;
     
    	public static void main (String [ ] args)	{
    		BallWorld1 world = new BallWorld1 (Color.red);
    		world.show ();
    	}
     
     
    	private BallWorld1 (Color ballColor) {
     
    		// constructor for new ball world
    			// resize our frame
    		setSize (getHeight(),getWidth());
    		setTitle ("Ball World");
    		addMouseListener(new BallListener());
    		aMatka = new matka(740,980);
    			// initialize object data field
    		aBall = new Ball (10, 15, 5);
    		aBall.setColor (ballColor);
    		aBall.setMotion (3.0, 6.0);
     
    		addWindowListener( 
    			new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
     
     
     
    	public void paint (Graphics g) {
    			aMatka.Paint(g);
    			aBall.paint (g);
    			aBall.move();
    			if ((aBall.x() < 0) || (aBall.x() > getSize().width))
    				aBall.setMotion (-aBall.xMotion(), aBall.yMotion());
    			if ((aBall.y() < 0) || (aBall.y() > getSize().height))
    				aBall.setMotion (aBall.xMotion(), -aBall.yMotion());
    	try { Thread.sleep(100); }
    			catch (InterruptedException e) {}
    			repaint();
    	}
     
    	class BallListener extends MouseAdapter {
    		public void mousePressed(MouseEvent e) {
    			aBall.moveTo(e.getX(), e.getY());
    		}
    	}
     
    	[COLOR="#FF0000"]class MatkaListener implements KeyListener {
     
    		 public void keyPressed(KeyEvent e)
    		 {
    			 switch (e.getKeyCode())
    			 {
    			 case KeyEvent.VK_RIGHT: aMatka.moveR(20);
    				 break;
    			 case KeyEvent.VK_LEFT: aMatka.moveL(20);
    				 break;
    			 }
    		 }
    		@Override
    		public void keyReleased(KeyEvent arg0) {
    			// TODO Auto-generated method stub
     
    		}
    		@Override
    		public void keyTyped(KeyEvent arg0) {
    			// TODO Auto-generated method stub
     
    		}
     
    	}[/COLOR]
     
    }

    Can you help me to see my problem and fix that ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Move an object

    Try debugging the code by adding some println statements that print out the values of x and y that control the object's location. Print out all changes made to the x and y values and all uses of the x and y values.
    The print out should help you see where the problem is and why the object is not moving.
    Add a println() to the key listener to be sure it is being called.



    The posted code does not compile: missing class: Ball
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Move an object

    You should add 'implements Runnable
    and here is some tips:

    this is easy so i let you figure out this

    x = 1; //horizontal moving speed
    y = 1; //vertical moving speed
    dx = x; //horizontaly variable x
    dy = y; //verticaly variable y

    case PAN_UP:
    setDY(+1);
    break;

    case PAN_DOWN:
    setDY(-1);
    break;

    case PAN_LEFT:
    setDX(+1);
    break;

    case PAN_RIGHT:
    setDX(-1);
    break;

    private void setDX(int dir){
    dx = dir;

    private void setDY(int dir){
    dy = dir;

    if(e.getKeyCode() == KeyEvent.VK_DOWN){
    ***.***(***.PAN_DOWN);

    I hope this helps little bit

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Move an object

    thank you both fo you, SOLVED.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Move an object

    Please mark thread as solved
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why can't I get my cards to move?
    By Skynet928 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2013, 08:30 PM
  2. Object Speed problem! (move();)
    By Optimizer in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 13th, 2013, 06:24 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 07:11 AM
  4. Node swap (move up)
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2010, 06:56 PM
  5. How to move an image (or how to delete one)
    By User in forum AWT / Java Swing
    Replies: 3
    Last Post: December 17th, 2009, 11:25 AM