markavo
asked on
moving a object (Java)
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);
}
}
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);
}
}
ASKER
Hi
Thank you , Do you have any code recommendations i can learn from , for the issues but i know what you are saying.
Thank you , Do you have any code recommendations i can learn from , for the issues but i know what you are saying.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,CAN VAS_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_BA T_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.setF illed(true ) ;
add (bat);
}
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,CAN
}
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_BA
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
add (bat);
}
In other words, check code getElementAt(appX, appY) for correct logic once get past this first issue.