Question

I'm trying to implement something quite simple (in the principle) but i have some troubles with the implementation.

In my programm the user has to choose between 1 up to 4 directions which shall be excluded values. For every direction the user can choose a number range for the values to be excluded (from 0 to 360). But somehow i can't figure out how to make this in an easy way.

My panel looks like that : A)

enter image description here

Here's a short example of what i'm trying to achieve.

I want the same result for this : B)

enter image description here

As for this (but it should work for all the 4 cases of course): C)

enter image description here

Do you have any idea on how to solve this ? Or some hints? Any help will be greatly appreciated. (If you want me to provide the code i tryied, i will).

Edit : Okey, you wanted some code, here is what i did (but it's not working as i want).

content[i].getNwdMean() is the value i want to compare with those in the spinners, to see if my point is in the exclusion zone.

buttons.chkExcludedA1, buttons.chkExcludedA2, buttons.chkExcludedA3 and buttons.chkExcludedA4 are the buttons for the directions to exclude.

buttons.getDir1A(), getDir2A, getDir3A, getDir4A, getDir1B, getDir2B, getDir3B, getDir4B are the values stored in the spinners, in my example C), getDir1A = 180, getDir1B = 190; getDir2A = 190 and getDir2B = 200.

    // Is supposed to check the interval in both sides.
public static boolean isBetween(double x, double a, double c)
{
    return(x > (a < c ? a : c) && x < (a > c ? a : c));
}

private boolean parsDirections(boolean state)
{
   boolean a1 = false, a2 = false, a3 = false, a4 = false;
   if (buttons.chkExcludedA1 == true || buttons.chkExcludedA2 == true ||  buttons.chkExcludedA3 == true || buttons.chkExcludedA4 == true)
    {
        if (isBetween(content[i].getNwdMean(), buttons.getDir1A(), buttons.getDir1B()) == false)
        {
            a1 = true;
        }
        else if (isBetween(content[i].getNwdMean(), buttons.getDir2A(), buttons.getDir2B()) == false)
        {
            a2 = true;
        }
        else if (isBetween(content[i].getNwdMean(), buttons.getDir3A(), buttons.getDir3B())== false)
        {
            a3 = true;
        }
        else if (isBetween(content[i].getNwdMean(), buttons.getDir4A(), buttons.getDir4B()) == false)
        {
            a4 = true;
        }
    }
            // Basic test to see if it prints what i want, but it doesn't.
    if (a4 == true && a2 == true)
    {
        print(state);
    }
    else if (a1 == true && a2 == false)
    {
        print(state);
    }
    else 
    {
        return state;
    }
    return state;
}
Was it helpful?

Solution

You should check the individual chkExcludedAX's in every if-statement (among other issues with your logic).

Try something like:

boolean excluded1 = buttons.chkExcludedA1 &&
    isBetween(content[i].getNwdMean(), buttons.getDir1A(), buttons.getDir1B()));
boolean excluded2 = buttons.chkExcludedA2 &&
    isBetween(content[i].getNwdMean(), buttons.getDir2A(), buttons.getDir2B()));
boolean excluded3 = buttons.chkExcludedA3 &&
    isBetween(content[i].getNwdMean(), buttons.getDir3A(), buttons.getDir3B()));
boolean excluded4 = buttons.chkExcludedA4 &&
    isBetween(content[i].getNwdMean(), buttons.getDir4A(), buttons.getDir4B()));

boolean excluded = (excluded1 || excluded2 || excluded3 || excluded4);

(favouring readability above what's likely to be micro-optimizations)

Or you can combine all of them into one statement:

boolean excluded =
  (buttons.chkExcludedA1 &&
   isBetween(content[i].getNwdMean(), buttons.getDir1A(), buttons.getDir1B())) ||
  (buttons.chkExcludedA2 &&
   isBetween(content[i].getNwdMean(), buttons.getDir2A(), buttons.getDir2B())) ||
  (buttons.chkExcludedA3 &&
   isBetween(content[i].getNwdMean(), buttons.getDir3A(), buttons.getDir3B())) ||
  (buttons.chkExcludedA4 &&
   isBetween(content[i].getNwdMean(), buttons.getDir4A(), buttons.getDir4B()));

OTHER TIPS

I assume you have a Button or something to have the user confirming its selection. This selection should then trigger an ActionListeners actionPerformed Method in which you must ensure to have access to the different UI elements.

Inside the action Performed Method i would not bother about the specific selections if not absolutly necessary, instead i would just do two things (this assumes a very simple case):

For any selected checkbox (Direction 1-4)....

1.) ...get the according start values, determine the lowest value 2.) ...get the according end values, determine the hightest value 3.) Exclude all values between the lowest and highest value

This way you dont have to care about the different values that can overlap. However if you want to be able to exclude segments and still dont have to bother about overlapping you would have to do something like this:

For any selected checkbox (Direction 1-4)....

1.) ... get the according start/end values 2.) ... call a method to add numbers to be excluded to a array delivering the start/ end values 3.) ... have the method look at the current array of numbers to check if the start/ end values or the values between them is already included, if not have this method adding the values 4.) ... Use the prepared array from that method

This would be more likely the case if for example you want to exclude range 100-200 and 400-500 but still have 201-399 included.

Feel free to provide your code so we will understand much better in what form you need that exclusion (like just numbers in an array, a min/max value or a own datatype Range or something).

In your code is impossible to achieve this requirements a4 == true && a2 == true because you have code like this

boolean a1 = false, a2 = false, a3 = false, a4 = false;

if (a) {
    a1 = true;
} else if (b) {
    a2 = true;
} else if (c) {
    a3 = true;
} else if (d) {
    a4 = true;
}

Only one aX can be set to true.

If a && b && c && d == true then (a1 = true) && (a2 && a3 && a4 == false)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top