# Help on understanding a program

• 11-10-2009, 12:59 AM
newbie225
Help on understanding a program
I have been learning JAVA on my own pace .

I am trying to understand a program .Two variables are initialized "xsquare and ysquare"

int xsquare;
int ysquare;

I can not find anywhere in the body of the program in which above mentioned variables point to x and y location of the moving sqaure ?

How does the compiler recognize that xsquare and ysqaure is in fact is x and y location of square ?

any tips or help will be great Thank you

here are the codes

import java.awt.Frame;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Mover extends Frame {
public static void main(String arg[]){
new Mover();
}
Mover (){
pack();
show();
}
public void processWindowEvent(WindowEvent event){
if(event.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
}
}
class MoverCanvas extends Canvas implements MouseListener {
final int SIDE =20;
boolean moving = false;
int xsquare;
int ysquare;
int xoffset;
int yoffset;
MoverCanvas(){
setSize(200,200);

}
public void mouseClicked(MouseEvent event) {

}
public void mouseEntered(MouseEvent event) {

}
public void mouseExited(MouseEvent event){
}
public void mousePressed(MouseEvent event){
int x = event.getX();
if ((x < xsquare) || (x > (xsquare + SIDE)))
return;
int y = event.getY();
if (( y < ysquare) || (y > (ysquare + SIDE)))
return;
xoffset = x - xsquare;
yoffset = y - ysquare;
moving = true;

}
public void mouseReleased (MouseEvent event){
if (moving){
xsquare = event.getX() - xoffset;
ysquare =event.getY() -yoffset;
moving = false;
repaint();
}

}
public void paint (Graphics g){
Rectangle rect = getBounds();
g.setColor(Color.cyan);
g.fillRect(0,0,rect.width,rect.height);
g.setColor(Color.blue);
g.fillRect(xsquare,ysquare,SIDE,SIDE);
}

}
• 11-10-2009, 01:53 AM
gcampton
Code:

```int x = event.getX(); if ((x < xsquare) || (x > (xsquare + SIDE))) return; int y = event.getY(); if (( y < ysquare) || (y > (ysquare + SIDE))) return; xoffset = x - xsquare; yoffset = y - ysquare;```
x and y are being used as comparison operators, and just being initialized halfway through the code.
so it assigns x to even.getx() and y to event.gety() compares against the sides of square and if true, the offset equals the the values minus the sides.