0

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

2 回答 2

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 i ,就像运行您想要的所有函数一样,因此必须在设置窗口大小之前计算您想要的任何内容。

于 2013-07-17T22:37:27.137 回答
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" );
}
于 2013-07-17T20:20:37.950 回答