Int can't be a value?

Is there a way to make a statement where you declare that an int value cannot be a certain number? For example, if you say int test = 5; would there be a way to say test ≠ 20

Not really but why would you do that? There are surely other solutions to prevent that depending on what you want to do.

@jb4x

I am controlling the size of a rectangle by pressing up or down. However, by pressing down too many times you can make the size negative which reverses the effect (eg down increases the size, and up decreases the size). This messes up many other things that are based off of the size of the rectangle. I can include part of the code if necessary.

It would be better to include code yes.

Now you can usually achive your effect using simple if condition:

if (var > 20) {
  var = 20;
}

It prevents your variable to go over 20.

2 Likes

@jb4x

This code has another problem. It always has my int value at the number you set it equal to. For example, in your code, it would always keep var = to 20 no matter what.

Can you post some code please?

@jb4x

//Add possibility to change shapes (square, circle, arc, triangle)
//Add undo button
//Add eraser 
//Connect shapes so there isn't a gap when you draw 
//Add transparency/tint feature
PGraphics circles;  //PGraphics layer
boolean randomCol;  //Random color variable
int size = 50;  //Size of circle and square
color col;  //Color Variable 
float stop = 95;  //Disables you to draw on text
int toDraw = 1;  //Switching between left and right (circle and square)
String v = "Hold down the mouse or trackpad to paint.  When trying to use different colors,  remember that lowercase letters and uppercase letters do different things.";  //Text variables
String s = "b = Blue,  B = Black,  c = Cyan,  g = Green,  G = Gray,  m = Magenta,  M = Maroon (Brown),  o = Orange,  p = Purple,  P = Pink,  r = Red,  w = White,  y = Yellow,  Tab = Random.";
String e = "ESC = Exit,  Space Bar = Erase the whole drawing,  Up Arrow Key = Increase size,  Down Arrow Key = Decrease size,  Left Arrow Key = Circle,  Right Arrow Key = Square.";

void setup()  //Setup the canvas
{
  frameRate(6000);
  fullScreen();
  background(255);
  fill(0);
  circles = createGraphics(2000, 2000);  //Setup the PGraphics layer
  randomCol = false;  //Disables random color at the start of the program
}
void draw()  //Draw
{
  background(255);
  noStroke();
  noCursor();
  if (randomCol)  //Setting the random color variable
  {
    col = color(random(255), random(255), random(255));
  } 
  if (mousePressed)  //If mouse is pressed then draw
  {
    if (mouseY > stop)  //If mouseY variable is underneath the text, then you can draw 
    {
    drawCircles();
    }
  }
  image(circles, 0, 0);  //Print the second plane
  fill(col);
  if (toDraw == 0)  //Switches to a square or circle if left or right is pressed
  {
    fill(col);
    rect(mouseX-(size/2), mouseY-(size/2), size, size);
  }
  else if (toDraw == 1)
  {
    fill(col);
    ellipse(mouseX, mouseY, size, size);
  }
    drawText();  //Writes the text and the line
}
void drawCircles()  //The PGraphics layer
{
  if (toDraw == 0)  //Draw the square if right is pressed
  {
    circles.beginDraw();
    circles.noStroke();
    circles.fill(col);
    circles.rect(mouseX-(size/2), mouseY-(size/2), size, size);
    circles.endDraw();
  }
  else if (toDraw == 1)  //Draw the circle if left is pressed
  {
    circles.beginDraw();
    circles.noStroke();
    circles.fill(col);
    circles.ellipse(mouseX, mouseY, size, size);
    circles.endDraw();
  }
}
void drawText() //Here is where we write the text and draw the line separating it
{
  fill(0);
  stroke(0);
  text(v, 10, 20);
  text(s, 10, 40);
  text(e, 10, 60);
  line(0, 70, 2000, 70);
}
void keyPressed()  //Has all the key press inputs
{
  if (key == CODED)
  {
    if (keyCode == UP)  //Up and down change the size
    {
      size=size+5;
      stop=stop+2.5;
    }
    if (keyCode == DOWN)
    {
      size=size-5;
      stop=stop-2.5;
    }
    if (keyCode == RIGHT)  //Left and right make it a circle or square 
    {
      toDraw = 0;
    }
    if (keyCode == LEFT)
    {
      toDraw = 1;
    }
  }
  if (key == TAB)  //If tab is pressed, random colors
  {
    randomCol = !randomCol;
  }
  if (key == 'b')  //Different keys activate different colors
  {
    randomCol = false;  //Disables random colors
    col = color(0, 0, 255);  //Switches to the desired color
  }
  if (key == 'B')
  {
    randomCol = false; 
    col = color(0);
  }
  if (key == 'c')
  {
    randomCol = false; 
    col = color(0, 255, 255);
  }
  if (key == 'g')
  {
    randomCol = false; 
    col = color(0, 255, 0);
  }
  if (key == 'G')
  {
    randomCol = false; 
    col = color(128, 128, 128);
  }
  if (key == 'm')
  {
    randomCol = false; 
    col = color(255, 0, 255);
  }
  if (key == 'M')
  {
    randomCol = false; 
    col = color(139, 69, 19);
  }
  if (key == 'o')
  {
    randomCol = false; 
    col = color(255, 140, 0);
  }
  if (key == 'p')
  {
    randomCol = false; 
    col = color(148, 0, 211);
  }
  if (key == 'P')
  {
    randomCol = false; 
    col = color(255, 192, 203);
  }
  if (key == 'r')
  {
    randomCol = false; 
    col = color(255, 0, 0);
  }
  if (key == 'w')
  {
    randomCol = false; 
    col = color(255);
  }
  if (key == 'y')
  {
    randomCol = false; 
    col = color(255, 255, 0);
  }
  if (key == ' ')
  {
    circles.beginDraw();
    circles.background(255);
    circles.endDraw();
  }
}
1 Like

I can’t see why you can’t apply the concept.

if (keyCode == DOWN)
{
  size=size-5;
  if (size < 0) {
    size = 0;
  } else {
    stop=stop-2.5;
  }
}
1 Like

@jb4x

Thank you.

I believe the reason why it wasn’t working before is because I put the

Not inside the key code. :relaxed:

Note that Processing also has some built-in functions to express this idea in general.

For example, if you want to set lowest value, take the max of two values – your update or your defined minimum:

int x = 10;
x = x - 25;  // -15
x = max(0, x);  // minimum is 0, not -15

Or if you want to set the highest value, do the same thing – take the min of two values:

int x = 10;
x = x + 25;  // 35
x = min(20, x);  // maximum is 20, not 35

You can also enforce both a min and a max with constrain:

int x = 50;
x = x + 99;  // 149
x = constrain(x, 0, 100);  // to max of 100
x = x - 999;  // -899
x = constrain(x, 0, 100);  // to min of 0

A concise way of making a change and constraining it is to include the change as the argument.

int x = 0;
x = constrain( x + (int)random(-999, 999), -100, 100);

This will update x to some value between -999 and 999, then constrain it to the range -100 / 100.

1 Like