Processing codes

00:14 Sreekanth R 0 Comments






Processing code for emulating arrow keys :


PImage up;
PImage left;
PImage right;
PImage down;
int dec;
int iwidth, iheight;
int xpos, ypos;
String nout,oout;
void setup() {
  size (1080, 700);
  background(12);
  iwidth =90;
  iheight =90;   
  up = loadImage("img/up.png");
  right = loadImage("img/right.png");
  left = loadImage("img/left.png");
  down = loadImage("img/down.png");
  dec=0;
  xpos=860;   
  ypos=495;
  nout="Stopped";
  oout="stopped";
  
}
void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      nout="goForward";
    } else if (keyCode == DOWN) {
      nout="goBack";
    } else if (keyCode == LEFT) {
      nout="goLeft";
    } else if (keyCode == RIGHT) {
      nout="goRight";
    }
  } else {
    nout="Stopped";
  }
}
void draw() {
  update(mouseX, mouseY);
  translate(xpos, ypos);
  tint(0, 0, 255);
  image(up, 0, 0, iwidth, iheight );
  image(right, 100, 100, iwidth, iheight );
  image(left, -100, 100, iwidth, iheight );
  image(down, 0, 100, iwidth, iheight );
  if(nout!=oout)
  {
  println(nout);
  oout=nout;
  }
}
void mousePressed()
{
  switch(dec) {
  case 1: 
    nout="goForward"; 
    break;
  case 2: 
    nout="goRight"; 
    break;
  case 3: 
    nout="goBack"; 
    break;
  case 4: 
    nout="goLeft"; 
    break;
    default : println("Press arrow keys or use mouse to control");
  }
}
void update(int x, int y) {
  //keyPressed();
  if (over(xpos, ypos, iwidth, iheight)) {
    dec=1;
  } else if (over(xpos+100, ypos+100, iwidth, iheight)) {
    dec=2;
  } else if (over(xpos, ypos+100, iwidth, iheight)) {
    dec=3;
  } else if (over(xpos-100, ypos+100, iwidth, iheight)) {
    dec=4;
  }
}

boolean over(int x, int y, int w, int h) {
  if (mouseX >= x && mouseX <= x+w &&  mouseY >= y && mouseY <= y+h) {
    return true;
  } else {
    return false;
  }
}


You can download the Processing code with zip file here.




Processing code for Scroll bar :


PFont logFont;
Scrollbar bar;
String content = "";
float scrollbarRatio;
String absolutePath;
int line;

void setup()
{
  size(1080, 700);
  logFont = loadFont("CenturyGothic-16.vlw");

  bar = new Scrollbar(width-20, 85, 20, 250, 1);
  setLogPath();
}
void draw()
{

  drawLogContent();

}
void setLogPath()
{
  absolutePath = "data/new.txt";  //selection.getAbsolutePath();
  String lines[] = loadStrings(absolutePath);
  content = "";
  line = 0;

  for (int i = 0; i < lines.length; i++) {
    String lineContent = lines[i] + "\n";
    content += lineContent;
    line++;
  }
}




void drawLogContent()
{
  String[] adjustedLines = content.split("\n");

  int maxLines = 19;//floor((height - 10) / 42);

  if (adjustedLines.length > maxLines)
  {
    scrollbarRatio = ((float) maxLines) / ((float) adjustedLines.length);
  }

  // Text Box
  //fill(80);
  //rect(0, 0, width, height);

  textAlign(LEFT);
  textLeading(25);
  fill(225);

  //String[] contentLines = content.split("\n");
  String display = "";

  int lineDifference = bar.getLineDifference();

  if (adjustedLines.length > maxLines)
  {
    for (int i = lineDifference; i < maxLines + lineDifference; i++)
    {
      if (i < adjustedLines.length)
      {
        display += adjustedLines[i];
        display += "\n";
      }
    }
  } else
  {
    display = content;
  }
  noStroke();
  fill(0);
  rect(653, 70, 425, 415);
  textFont(logFont);
  fill(70, 233, 22);
  text("Log display", 830, 95);
  text(display, 660, 125);
  fill(240);
  bar.resizeScrollbar(width-20, 85, 20, 250);
  bar.update(scrollbarRatio);
  bar.display(scrollbarRatio);
}

You can download Processing code for Scroll bar here.



If you got any doubts or suggestions then comment below . . . . !!



0 comments: