How to compare Range of numbers?

• 07-02-2011, 08:26 PM
Bagzli
How to compare Range of numbers?
For example if I am getting position of my mouse cursor from the screen
Code:

`e.getX()`
and I need to check if that location is anywhere from 50 to 100

so if i were checking just 50 i'd do this:
Code:

```addMouseMotionListener(new MouseAdapter()         {             public void mouseDragged(MouseEvent e)             {                     if(e.getX() == 50)                     {                             //Some Code                     }             }         });```
How would i check this for all numbers between 50 and 100, i can only think of looping but that somehow seems like a bad idea?

Basically I have a square on the screen and i want to check if my mouse is anywhere on the surface of that square.

this is my attempt with loop, square only moves down and to the right with this code, it wont go left or up
Code:

```addMouseMotionListener(new MouseAdapter()         {             public void mouseDragged(MouseEvent e)             {                     for(int i=squareX; i<squareX + squareW; i++)                             if(e.getX() == i)                             {                                     moveSquare(e.getX(),e.getY());                             }             }         });```
Even if this worked i'd need a second loop that would check Y Position and that is now getting way too messy...
• 07-02-2011, 08:34 PM
Fubarable
Just use an if block with a slightly more complex boolean condition:

Code:

```if (myVar >= minValue && myVar < maxValue) { // or > minValue... or <= maxValue ...   // do something }```
And actually you'll need a slightly more complex boolean condition than that since you'll want to do the same double checks for the x value and the y value.
• 07-02-2011, 08:56 PM
Bagzli
Here it is but i got a major problem...
Code:

```public void mouseDragged(MouseEvent e)             {                     if((e.getX() >= squareX && e.getX() <= squareX + squareW) && (e.getY() >= squareY && e.getY() <= squareY + squareH))                     {                             moveSquare(e.getX(),e.getY());                     }                                 }```
This will only move the box while mouse is inside it. The box is drawn from x and y cordinate and it goes by length and height. So lets say l = 20, w = 20, x = 50, y = 50.

Box will start at 50, 50 and will end at 70, 70. So while my mouse is in between 50 -70 x and 50 - 70 y it will move it. That is moving down and to the right and it works perfectly. If I try to move my mouse left or up it will exit the box and the box will not get moved, is there a way to change this? So instead of box starting at 50, 50 to make the center of the box 50, 50 so that way when i move up and left i am still in the box and the box moves.

Hope I was not too confusing >.<
• 07-02-2011, 09:30 PM
Fubarable
You need to think logically: when is it important that the mouse click inside of the box? When the mouse is being dragged or when the mouse clicks (or better mousePressed)? So which method of the MouseListener (or better, MouseAdapter) should you be checking for this? mouseDragged? or mousePressed?
• 07-02-2011, 10:30 PM
Bagzli
dragged because he is dragging the box to new location
• 07-02-2011, 10:55 PM
Fubarable
Quote:

Originally Posted by Bagzli
dragged because he is dragging the box to new location

No, in this situation it is mousePressed. You've said it yourself, the user must press the mouse button in the box and then drag the mouse with the box to a new location. The only time when it is important that the mouse be within the initial x and y confines of the box is on the initial press. Later when the user is dragging the mouse, the mouse is often in a completely different location. So the MouseAdapter must override mousePressed and be activated if the mouse press is within the box and de-activated on mouseReleased. If the mouse is dragging and the MouseAdapter is activated, then the box location must be changed based on the mouse's location.

So to reiterate what is important initially is that you only activate the MouseAdapater when the mouse is pressed within the box. Later on when the mouse is dragged, you don't care if the mouse is within the initial confines of the box.

Also you will need to add your MouseAdapter as both a MouseListener and a MouseMotionListener to your dragging JPanel.

For example, please have a look at the code in this thread: questions-about-events-differents-layers
• 07-02-2011, 11:17 PM
Fubarable
Actually the example linked to above is probably too complex. This is more along the lines of what I meant:

Code:

```import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SquareMove extends JPanel {   private static final Stroke STROKE = new BasicStroke(5);   private static final Color SQUARE_COLOR = Color.red;   private static final Color SQR_MOVE_COLOR = new Color(175, 175, 255);   private static final int PNL_WIDTH = 600;   private static final int PNL_HEIGHT = 450;   private Rectangle rect = new Rectangle(20, 20, 100, 100);   private Color squareColor = SQUARE_COLOR;   public SquareMove() {       MyMouseAdapter myMouseAdapter = new MyMouseAdapter();       addMouseListener(myMouseAdapter);       addMouseMotionListener(myMouseAdapter);   }   @Override   protected void paintComponent(Graphics g) {       super.paintComponent(g);       Graphics2D g2 = (Graphics2D) g;       g2.setStroke(STROKE);       g2.setColor(squareColor);       g2.drawRect(rect.x, rect.y, rect.width, rect.height);   }   @Override   public Dimension getPreferredSize() {       return new Dimension(PNL_WIDTH, PNL_HEIGHT);   }   private class MyMouseAdapter extends MouseAdapter {       private boolean active = false;       private Point delta = null;       @Override       public void mousePressed(MouseEvent e) {         // could use if (e.getX() >= rect.x && e.getX() <= rect.x + rect.width && ...         // but it's easier just to see if mouse is inside of the Rectangle         // via the Rectangle's contains method         if (rect.contains(e.getX(), e.getY())) {             active = true;             squareColor = SQR_MOVE_COLOR;             delta = new Point(e.getX() - rect.x, e.getY() - rect.y);             repaint();         }       }       @Override       public void mouseReleased(MouseEvent e) {         active = false;         squareColor = SQUARE_COLOR;         repaint();       }       @Override       public void mouseDragged(MouseEvent e) {         if (active) {             int x = e.getX() - delta.x;             int y = e.getY() - delta.y;             rect.setLocation(x, y);             repaint();         }       }   }   private static void createAndShowUI() {       JFrame frame = new JFrame("SquareMove");       frame.getContentPane().add(new SquareMove());       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.pack();       frame.setLocationRelativeTo(null);       frame.setVisible(true);   }   public static void main(String[] args) {       java.awt.EventQueue.invokeLater(new Runnable() {         public void run() {             createAndShowUI();         }       });   } }```
• 07-03-2011, 12:53 AM
Bagzli
ok I'll take a look at this code a bit later, been cleaning up house all day and somewhat tired, but thanks a lot I'll definitively go over it later and let you know how I did.
• 07-03-2011, 09:43 AM
Bagzli
Ugh, my level of knowledge is nowhere near this type of programming to be honest. I have been doing my best to try and keep up with your style of code but I honestly don't even know what a runable is. Is there any chance you could comment out that code telling me what each part is doing so I could try and understand it a bit more clearer?

See in school last thing i learned was maps and object writing, i only have 2 semesters of Java programming and what I am trying here is way ahead of me. I am doing this though because I want to learn more and this program is for a friend. So please stay patient with me :)
• 07-03-2011, 11:40 AM
DarrylBurke
Quote:

I honestly don't even know what a runable is.
Bookmark this page: Java Platform SE 6

db
• 07-03-2011, 12:56 PM
Iron Lion
You might also want to look up "anonymous inner class". The Runnable that's created in the main() method is an example of this.
• 07-03-2011, 01:50 PM
Dark
If you attempting to complete tasks that are beyond you then I suggest working your way up there. If you can, get a Java book and read through it. Different books cover different aspects and honestly, you will always learn something new. If not try using the Java Tutorials, they are free and go pretty in depth.

The code Fubarable posted, not the link, doesn't even contain runnable. As long as you know post good questions and you prove to us that you are trying then people will continue helping you.
• 07-03-2011, 02:18 PM
Fubarable
Quote:

Originally Posted by Dark
The code Fubarable posted, not the link, doesn't even contain runnable. As long as you know post good questions and you prove to us that you are trying then people will continue helping you.

Actually it does:
Code:

```  private static void createAndShowUI() {       JFrame frame = new JFrame("SquareMove");       frame.getContentPane().add(new SquareMove());       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.pack();       frame.setLocationRelativeTo(null);       frame.setVisible(true);   }   public static void main(String[] args) {       java.awt.EventQueue.invokeLater([b][color="red"]new Runnable()[/color][/b] {         public void run() {             createAndShowUI();         }       });   }```
To the OP: by creating a Runnable, starting the GUI in the Runnable, and adding it to the event queue, you make sure that the program is called on the Swing thread. This is as per the recommendations in the Oracle Swing tutorials. Please have a look here: Concurrency in Swing
• 07-03-2011, 03:11 PM
Dark
My apologies, I only skimmed the code and I didn't see it.