处理调用void setup()两次,我需要一个解决方法

时间:2013-07-17 19:38:46

标签: processing

我正在开发一个程序,它从源文件中读取一些图像(.jpg)和文本,并将它们组合成一个PDF。我知道处理可能不是最好的语言,但它是唯一一个我知道如何处理的语言。无论如何,我遇到了处理呼叫设置两次的问题。我已经看到当size()是设置中的第一行时,这个问题得到了解决,但我不能发生这种情况,因为我必须读入并存储我的所有数据,找到最宽图像的宽度,然后确保它的高度足以容纳包含多个图像的页面,并在我决定窗口的宽度和高度之前添加文本。我正在寻找关于如何构造代码的建议,以便我可以获得所有信息而无需调用两次设置,因为这导致我的PDF包含所有数据的两个副本。我已经包括设置,如果它可以帮助任何人。谢谢!

void setup(){
  font = loadFont("TimesNewRomanPSMT-20.vlw");
  File clientsFolder = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients");
  clients = clientsFolder.listFiles();
  for(File x : clients){
    println(x.getName());
  }
  //test files to see if they end in .txt, and have a matching .pdf extension that is newer
  String nextClient = needPdf();

  File nextClientData = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients/" + nextClient);
  //println(nextClientData.getName());

  //open the file for reading
  //setup can't throw the exception, and it needs it, so this should take care of it
  try{
    Scanner scan = new Scanner(nextClientData);

    while(scan.hasNextLine() ){
      exercises.add(scan.nextLine());
    }
    //println(exercises.toString());
    printedData = new Exercise[exercises.size()];
    println(exercises.size());
    for(int i = 0; i < exercises.size(); i++){
      printedData[i] = new Exercise((String)exercises.get(i));
    }

    //count the width and height
    int w = 0, h = 0;
    for(Exercise e: printedData){
      if(e.getWidest() > w){
        w = e.getWidest();
      }
      if(e.getTallest() > h){
        h = e.getHeight();
      }
    }

    //and finally we can create the freaking window
    //                                         this cuts the .txt off
    size(w, h, PDF, "C:/Users/[my name]/Desktop/" + nextClient.substring(0, nextClient.length() - 4) + ".pdf");

  }catch (FileNotFoundException e){
    println("Unknown error in PApplet.setup(). Exiting.");
    println(e.getMessage() );
    exit(); 
  }
}

2 个答案:

答案 0 :(得分:3)

如何在setup()之前移动所有这些功能?虽然处理通常会抱怨你是“混合静态和活动模式”,但这个hack似乎可以处理2.0.1:

int i = beforeSetup();
int szX,szY;

int beforeSetup() {
  println("look! I am happening before setup()!!");
  szX = 800;
  szY = 600;
  return 0;
}
void setup() {
  size(szX,szY);
  println("awww");
}

你本质上是调用一个函数来填充int我只是作为一个hack运行你想要的所有函数,因此必须在设置窗口大小之前计算你想要的任何东西。

答案 1 :(得分:1)

也许你可以在完成计算后调整窗口大小?一旦我做了这个草图,看看调整大小是如何工作的,它期待一个图像文件,看看它是否可以帮助你......

//no error handling for non image files!

PImage img;
int newCanvasWidth  = MIN_WINDOW_WIDTH;  // made global to  use in draw
int newCanvasHeight = MIN_WINDOW_HEIGHT;


java.awt.Insets insets;  //"An Insets object is a representation of the borders of a container"
                         //from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html

void setup()
{
  size(200, 200);   // always first line
  frame.pack();           insets = frame.getInsets();
  frame.setResizable(true);

      /// for debuging, system depende`nt, at least screen is...
  print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
  print("   MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
  print("   screenWidth = " + displayWidth);
  println("    screenHeight = " + displayHeight);
}


void draw()
{
  if (img != null)
  {

    image(img, 0, 0, newCanvasWidth, newCanvasHeight);
  }
}

void getImageAndResize(File selected)
{ 
  String path = selected.getAbsolutePath();

  if (path == null)
  {
    println ("nono :-|");
  } 
  else
  {

    img = loadImage(path);

        // a temp variable for readability 
    int widthInsets =insets.left + insets.right;
    int heightInsets =insets.top + insets.bottom;

        // constrain values between screen size and minimum window size
    int newFrameWidth  = constrain(img.width + widthInsets, MIN_WINDOW_WIDTH, displayWidth);
    int newFrameHeight = constrain(img.height + heightInsets, MIN_WINDOW_HEIGHT, displayHeight -20);

        // Canvas should consider insets for constraining? I think so...
     newCanvasWidth  = constrain(img.width, MIN_WINDOW_WIDTH - widthInsets, displayWidth - widthInsets);
     newCanvasHeight = constrain(img.height, MIN_WINDOW_HEIGHT - heightInsets, displayHeight -20 - heightInsets);


        // set canvas size to img size WITHOUT INSETS
    setSize(newCanvasWidth, newCanvasHeight);

        // set frame size to image + Insets size
    frame.setSize(newFrameWidth, newFrameHeight);


        //// for debuging
    println(path);
    println(" ");
    print("imgW      = " + img.width);
    println("   imgH       = " + img.height);
    print("width+ins = " + widthInsets);
    println("      height+ins = " + heightInsets);
    print("nFrameW   = " + newFrameWidth);
    println("   nFrameH    = " + newFrameHeight);
    print("nCanvasw  = " + newCanvasWidth);
    println("   nCanvsH    = " + newCanvasHeight);
    println(" ------  ");
  }

}


void mouseClicked()
{
  img = null;

  selectInput("select an image", "getImageAndResize" );
}