RequestImage(), Threads and other questions

edited January 2014 in Android Mode

Hello everybody :) I'm doing an app for reading my sisters magazine (www.alagarta.com). I've been already programming it for a few days and there as some questions that I couldn't solve.

First of them it's related to requestImage. Before I use it, I was getting the images from the website throught loadImage(link) that was inside a while and try, so when it tried to load a magazine cover/image that doesn't exist, it catched nullExceptionPointer and I break the loop. But since I decided to change loadImage() to requestImage() I can't do it anymore, it still catching NullExceptionPointer but it doesn't break the loop.

The second thing is a problem that I'm having since I added threads to my code. The app runs only once and them everytime that I open it, it immediately closes.

For the last, I wanted to now if it has a easy way to also build an iOS app with this code.

Thanks everybody ;) and sorry for my bad english.

Code(main):

    import android.view.MotionEvent;
    import ketai.ui.*;

boolean firstRun = false;
aLagarta mag = new aLagarta();
int numImage = 0;
int numPage = 1;
PImage background;
KetaiGesture gesture;
boolean reading = false;
boolean zoom = false;
boolean isDone = false;
int numberOfEdition;
PImage topLogo;
float sizeWidth;
float sizeHeight;
float drawX;
float drawY;
SimpleThread thread1;

void setup() {
  size(displayWidth, displayHeight);
  orientation(LANDSCAPE);
  //println(displayWidth+"   "+displayHeight);
  smooth();
  gesture = new KetaiGesture(this);
  sizeWidth = width;
  sizeHeight = height;
  drawX = width/2;
  drawY = height/2;
  thread1 = new SimpleThread(10, "loading");
  thread1.start();
  mag.update();
}

void draw() {
  if (isDone == true)
    thread1.quit();
  if (reading == false) {
    if (firstRun == false) {
      mag.update();
      firstRun = true;
    }
    imageMode(CENTER);
    image(background, 394, 260);
    image(topLogo, displayWidth/2, 44);
    imageMode(CENTER);
    if (mag.thumbs.get(numImage).width == 0) {
    }
    else {
      image(mag.thumbs.get(numImage), width/2, height*0.55, round(mag.thumbs.get(numImage).width*0.55), round(mag.thumbs.get(numImage).height*0.55));
    }
  }
  if (reading == true) {
    imageMode(CENTER);
    if (mag.readingPage.width != 0 && mag.readingPage.width !=-1) {
      image(mag.readingPage, drawX, drawY, round(sizeWidth), round(sizeHeight));
    }
  }
  //delay(1000);
}

void onFlick(float x, float y, float px, float py, float v) {
  if (!reading) {
    if (x-px > 0) {
      if (numImage != 0)
        numImage--;
    }
    else if (x-px < 0) {
      if (numImage != mag.thumbs.size()-1)
        numImage++;
    }
  }
  if (reading) {
    if (!zoom) {
      if (x-px > 0) {
        if (numPage != 1) {
          numPage--;
          mag.getPage(numberOfEdition, numPage);
        }
        /*if (mag.editionPages.get(numPage-1) == null)
         mag.getPage(numberOfEdition, numPage);*/
      }
      else if (x-px < 0) {
        numPage++;
        mag.getPage(numberOfEdition, numPage);
      }
    }
    if (zoom) {
      drawX = x-px;
      drawY = y-py;
    }
  }
}

void onDoubleTap(float x, float y) {
  if (!reading) {
    if (mag.thumbs.get(numImage).width == 0) {
    }
    else {
      reading = true;
      numberOfEdition = numImage+1;
      mag.getPage(numberOfEdition, 1);
    }
  }
  if (reading) {
    sizeHeight = height;
    sizeWidth = width;
    drawX = width/2;
    drawY = height/2;
    zoom = false;
  }
}

void onPinch(float x, float y, float d)
{
  sizeWidth = constrain(sizeWidth+d, mag.readingPage.width, 1.5*mag.readingPage.width);
  sizeHeight = constrain(sizeHeight+d, mag.readingPage.height, 1.5*mag.readingPage.height);
  zoom = true;
}

public boolean surfaceTouchEvent(MotionEvent event) {

  //call to keep mouseX, mouseY, etc updated
  super.surfaceTouchEvent(event);

  //forward event to class for processing
  return gesture.surfaceTouchEvent(event);
}

class aLagarta {
  ArrayList<PImage> thumbs = new ArrayList<PImage>();
  //ArrayList<PImage> editionPages = new ArrayList<PImage>();
  PImage readingPage;
  final int OK = 0;
  final int noEditionFound = 1;
  int numberOfEditions = 1;
  boolean loadMag = true;
  boolean loadEdition = true;

  int update() {

    background = loadImage("http://alagarta.com/edicoes/"+mag.thumbs.size()+"/bg.jpg");
    topLogo = loadImage("topLogo.png");

    println("Passei - update\n");
    while (numberOfEditions < 15) {
      println("Passei - While\n");
      try {
        PImage cover;
        if (numberOfEditions == 1 || numberOfEditions > 11)
          cover = requestImage("http://www.alagarta.com/edicoes/"+numberOfEditions+"/1.png");
        else
          cover = requestImage("http://www.alagarta.com/edicoes/"+numberOfEditions+"/1.jpg");
        if (cover.width == -1) {
          break;
        }
        else if (cover.width == 0) {
          println("loading cover - "+numberOfEditions);
          /*while (cover.width == 0) {
           int n = 1
           PImage loading = loadImage("loadingscreen"+n+");
          imageMode(CENTER);
           image(loading, 394, 260);
           delay(100);
           n++;
           }*/
          thumbs.add(cover);
          numberOfEditions++;
        }
      }
      catch(NullPointerException npe) {
        loadMag = false;
        println("erro\n");
        break;
      }
    }
    if (thumbs.size() == 0)
      return(noEditionFound);
    isDone = true;
    return(OK);
  }

  int getPage(int editionNumber, int pageNumber) {
    println("Passei getEdition\n");
    try {
      //PImage page;
      if (editionNumber == 1 || editionNumber > 11)
        readingPage = requestImage("http://alagarta.com/edicoes/"+editionNumber+"/"+pageNumber+".png");
      else
        readingPage = requestImage("http://alagarta.com/edicoes/"+editionNumber+"/"+pageNumber+".jpg");
      if (readingPage.width == -1) {
        println("error");
      }
      else if (readingPage.width == 0) {
        println("loading page - "+pageNumber);
      }
      println("add page - "+pageNumber);
    }
    catch(NullPointerException npe) {
      println("erro - getEdition\n");
    }
    return(OK);
  }
}

class SimpleThread extends Thread {
  boolean running;
  int wait;
  String id;
  PImage loading;

  SimpleThread(int w, String s) {
    wait = w;
    running = false;
    id = s;
  }

  void start() {
    running = true;
    super.start();
  }

  void run() {
    int n =1;
    while (running) {
      imageMode(CENTER);
      loading = loadImage("loadingscreen"+n+".png");
      image(loading, 394, 260);
      //println("estou vivo");
      delay(100);
      if (n < 4)
        n++;
      else
        n = 1;
      try {
        sleep((long)(wait));
      }
      catch(Exception e) {
      }
    }
  }

  public void draw() {
    image(loading, 394, 260);
  }

  void quit() {
    running = false;
    interrupt();
  }
}

Images for running the app:

loadingscreen1 loadingscreen2 loadingscreen3 loadingscreen4 topLogo

Answers

  • edited January 2014

    Your urls are strange, and i don't succeed to compile your code, look at lines 143,153,153,188,190

  • Oh seems that processing forum changed my urls :/ I'm gonna post on pastebin. But here compiles with no problems.

    paste bin: 1- main - http://pastebin.com/6vw1j0XH 2- classes - http://pastebin.com/CUwpNiJi

    Just remembering you will need those images on data folder to run. And also be on android mode.

  • _vk_vk
    edited January 2014

    The forum does break urls in code. You can use a hack to get over it:

    String forumHack = "http:";
    img = loadImage(forumHack + "//someurl.com/whatever.jpg");
    
Sign In or Register to comment.