Results 1 to 2 of 2
Thread: Help on understanding a program
- 11-09-2009, 11:59 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
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 (){
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
add(new MoverCanvas());
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);
addMouseListener(this);
}
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, 12:53 AM #2gcampton Guest
x and y are being used as comparison operators, and just being initialized halfway through the code.Java 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;
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.Last edited by gcampton; 11-10-2009 at 12:55 AM.
Similar Threads
-
Help with understanding how to get records out of a table.
By caryr in forum JDBCReplies: 10Last Post: 04-30-2009, 09:22 AM -
Help understanding packages and classpaths
By porchrat in forum New To JavaReplies: 2Last Post: 04-24-2009, 09:22 PM -
I have trouble understanding this program!
By PureAwesomeness in forum New To JavaReplies: 1Last Post: 03-16-2009, 05:34 AM -
[SOLVED] Help with understanding nullpointexcepion
By soxfan714 in forum New To JavaReplies: 4Last Post: 11-11-2008, 04:51 AM -
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11-05-2007, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks