Results 1 to 2 of 2
  1. #1
    newbie225 is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Post 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);
    }


    }

  2. #2
    gcampton Guest

    Default

    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;
    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.
    Last edited by gcampton; 11-10-2009 at 12:55 AM.

Similar Threads

  1. Replies: 10
    Last Post: 04-30-2009, 09:22 AM
  2. Help understanding packages and classpaths
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 09:22 PM
  3. I have trouble understanding this program!
    By PureAwesomeness in forum New To Java
    Replies: 1
    Last Post: 03-16-2009, 05:34 AM
  4. [SOLVED] Help with understanding nullpointexcepion
    By soxfan714 in forum New To Java
    Replies: 4
    Last Post: 11-11-2008, 04:51 AM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •