Solved

moving a object (Java)

Posted on 2010-09-18
4
851 Views
Last Modified: 2012-05-10
Hi all

Im new and learning java , what i'm struggling with is moving a object on mouse movement ,my thinking and trying to do :  if no object is at reference appX / appY then great a new GRect .
If after its been created its there don't create it again but move it. i have added a label just to show me how its being executed and the execution is all good but not moving the GRect.

Help appreciated....

the code i have created :-

      public void  mouseMoved(MouseEvent e){
                  int appX = (APPLICATION_WIDTH / 2) - (BAT_WIDTH / 2) ;
                  int appY = APPLICATION_HEIGHT - (BAT_HEIGHT * 2);
            
                  GRect bat = new GRect (appX ,appY ,BAT_WIDTH,BAT_HEIGHT);
                  bat.setFillColor(Color.red);
                  bat.setColor(Color.red);
                  bat.setFilled(true);
            
            
            if ( getElementAt(appX,appY) == null){
                  add(bat) ;
                  
                  GLabel gg = new GLabel ("Null",50,60) ;
                  add (gg);
            } else {
                  bat.move(e.getX(),appY) ;
                  
                  GLabel gg = new GLabel ("Not Null",60,70) ;
                  add (gg);
                }
}
0
Comment
Question by:markavo
  • 2
  • 2
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33709322
I believe this is a scope issue.  The bat is a local variable, so each time you run the mouse event you are creating a new instance, so if you issue the command move() you are moving the new one that is not the same one you added previously.  Additionally, once you start to get this working to move the first time, bat will no longer be at appX, appY; therefore, I would reconsider how you are looking that up.

In other words, check code getElementAt(appX, appY) for correct logic once get past this first issue.
0
 

Author Comment

by:markavo
ID: 33709348
Hi

Thank you , Do you have any code recommendations i can learn from , for the issues but i know what you are saying.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 33709368
Aside from moving the bat object to an instance variable (class scope), I would recommend following examples of the MouseListener tutorials:
http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
You may already have that information, but just in case.
0
 

Author Comment

by:markavo
ID: 33736514
HI

Thank you ... I have learnt so much i must have spent 10 -15 hours getting things wrong but it then fell into place thank you..

The code i came up with is :-

private static final int CANVAS_Y_BAT_START = APPLICATION_HEIGHT - (BAT_HEIGHT * 2);//fixed height position for bat

private static final int CANVAS_X_BAT_START = (APPLICATION_WIDTH / 2) - (BAT_WIDTH / 2);//fixed width position for bat
      
      public void init () {
            addMouseListeners() ;// listen for mouse
            addKeyListeners() ;// listen for keys
      }

public void run() {
    bat(CANVAS_X_BAT_START,CANVAS_Y_BAT_START); // add object GRect
}

      public void mouseMoved(MouseEvent e){
                  
      public void mouseMoved(MouseEvent e){
                  
                        for (int x =1 ;x<=APPLICATION_WIDTH ;x++){ //scan bottom of canvas
                              
                              if (getElementAt( x ,CANVAS_Y_BAT_START) !=null )//check if object there - bottom of canvas
                              {
                                    GObject bat1 = getElementAt(x,CANVAS_Y_BAT_START);//assign object to bat1
                                    bat1.setLocation (e.getX(), CANVAS_Y_BAT_START) ;// move object to mouse pointer possition
                              }
                        }      
            

      //Create a bat
      public void bat (int x , int y){
            GRect bat = new GRect (x, y,BAT_WIDTH,BAT_HEIGHT);
            bat.setColor(Color.RED); bat.setFillColor(Color.RED);bat.setFilled(true) ;
            add (bat);
      }
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
web application structure 18 102
arguments to jar 5 35
Adding multiple JVM environments to RedHat 6 7 49
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question