We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › is image() required with pGraphics() pdf renderer
Page Index Toggle Pages: 1
is image() required with pGraphics() pdf renderer? (Read 816 times)
is image() required with pGraphics() pdf renderer?
Jan 20th, 2010, 11:30pm
 
Hi all!

1. Here are some stupid beginner questions, I hope someone can help.

I try to learn processing and write a sketch that draws something on a pdf document with variable width and hight.
After digging in the documentation I figured that

Code:
pGraphics(x, y, PDF, "output,pdf"); 



in the draw() function might do the job. Would this work?

2. In the pgraphics example here:

Code:
PGraphics pg;

void setup() {
 size(100, 100);
 pg = createGraphics(80, 80, P3D);
}

void draw() {
 pg.beginDraw();
 pg.background(102);
 pg.stroke(255);
 pg.line(40, 40, mouseX, mouseY);
 pg.endDraw();
 image(pg, 10, 10);
}


The last function is Code:
image(pg, 10, 10); 


I understand that it shows the content of pg.
I could not find out if it is required?

Another thing: I just get an empty pdf document, do I have to call exit()?

any help appreciated, couldn't find anything in the documentation and I am in that state where you try to hypnotize the code just staring at it Tongue



Re: is image() required with pGraphics() pdf renderer?
Reply #1 - Jan 20th, 2010, 11:34pm
 
this is not required as it just shows the image on screen.

Ive actually never used a pdf export with pgraphics. It depends on your sketch but the other examples mentioned here might be more useful
http://processing.org/reference/libraries/pdf/index.html

i always use Single Frame from an Animation (With Screen Display)
this also works when there is actually no animation it just means in a running draw loop.
Re: is image() required with pGraphics() pdf renderer?
Reply #2 - Jan 20th, 2010, 11:54pm
 
DOGMATECH wrote on Jan 20th, 2010, 11:30pm:
After digging in the documentation I figured that
Code:
pGraphics(x, y, PDF, "output,pdf"); 


in the draw() function might do the job. Would this work

No.
First, Java is case sensitive, so it would be PGraphics. Second, I see no code like this in the PDF reference page, the examples use createGraphics() (or beginRecord(), or size()). Last, you don't create graphics in draw() but rather on setup.
Re: is image() required with pGraphics() pdf renderer?
Reply #3 - Jan 21st, 2010, 12:53am
 
Cool! Thank you all for the quick replies  Cheesy

I read the documentation about PDF before, but as I understood there is no way to change size() on the fly and what I need is a pdf output that is scalable for different DPI.
Code:
pg = createGraphics(x, y, PDF, "output.pdf"); 


(excuse my sloppy example earlier) looked like the only way.

I will experiment in a new direction now. Thanks again Smiley
Re: is image() required with pGraphics() pdf renderer?
Reply #4 - Jan 21st, 2010, 1:24am
 
DOGMATECH wrote on Jan 21st, 2010, 12:53am:
what I need is a pdf output that is scalable for different DPI.

What do you mean exactly
You set a PDF size, indeed, at the size you specify. If you use only Processing shape functions like line(), rect() and ellipse(), the outputted PDF will be scalable, as the drawing will be stored as vectors.
Re: is image() required with pGraphics() pdf renderer?
Reply #5 - Jan 21st, 2010, 12:31pm
 
Hey man! Thanks for your interrest.
what I mean is that I try to create an dinA4 output with a simple line grid on it. The purpose is so a friend who is a knitwear fashion designer can enter the milimeters and dpi and then get an accurate representation if printed. Its just a challenge for me to learn.

I had it all working with capture apart from the resizing for different dpi.
so I choose createGraphics() because it allows for a size different then size().
Re: is image() required with pGraphics() pdf renderer?
Reply #6 - Jan 25th, 2010, 12:42am
 
Allright, found solutions to all the problems.

Code:
pg = (PGraphicsPDF) createGraphics(wide, hi, PDF, "raster-###.pdf"); 



does the job, and since the application exits after the conditions are done I can call it in draw().

Check the first version here:

Code:
//*****(c)2010 by Philippe Bono*****
//*****licensed under GPL*****

import processing.pdf.*;
import interfascia.*;

//radio declarations
GUIController c;
IFRadioController rc;
IFRadioButton b1, b2, b3;
//textfield stuff
IFTextField tw, th;
IFLabel lw, lh;

//declare PG
PGraphicsPDF pg;

//Grid variables
int x1;
int y1;
//for precise calculation
float mmX;
float mmY;
//input
String xStr;
String yStr;
//pdf groesse
int wide;
int hi;

/*********************************************
*SETUP
**********************************************/
void setup() {
 size(400, 400);
 
 //Text Field creation  
 th = new IFTextField("Text Field", 25, 10, 40);
 lh = new IFLabel("Breite in milimetern", 80, 10);
 
 tw = new IFTextField("Text Field", 25, 40, 40);
 lw = new IFLabel("hoehe in milimetern", 80, 40);

 //Radio Button creation
 c = new GUIController(this);
 rc = new IFRadioController("Mode Selector");
 
 b1 = new IFRadioButton("72 dpi", 20, 70, rc);
 b2 = new IFRadioButton("150 dpi", 20, 90, rc);
 b3 = new IFRadioButton("300 dpi", 20, 110, rc);
   
}

/*********************************************
*DRAW
**********************************************/
void draw(){
 
 c.add(tw);
 c.add(lw);
 
 c.add(th);
 c.add(lh);
 
 xStr = tw.getValue();
 yStr = th.getValue();
 
 //println(yStr);
 //println(xStr);
 
 c.add(b1);
 c.add(b2);
 c.add(b3);
 
 /*******das folgende ist getestet und funktioniert, die werte werden correct uebernommen******/
 if (b1.isSelected() == true){
   wide = 595;
   hi = 840;
     
 } else if (b2.isSelected() == true){
   wide = 1240;
   hi = 1745;    
   
 } else if (b3.isSelected() == true){
   wide = 2480;
   hi = 3508;    
   
 }
 
 
 if (b1.isSelected() == true || b2.isSelected() == true || b3.isSelected() == true){
   
   //3satze
   
   mmX = (Integer.parseInt(xStr)*wide)/210; //297*210 ist din a4 in milimetern
   //println(mmX);
   x1 = (int) mmX;
   
   mmY = (Integer.parseInt(yStr)*hi)/297;
   y1 = (int) mmY;
   
   //***********pgGraphics screen************************
   pg = (PGraphicsPDF) createGraphics(wide, hi, PDF, "raster-###.pdf");
 
   //println (hi);
   //println(wide);
   
   beginRecord(pg);  
 
   background (255);  
   //pg.stroke(111);
 
  for (int i = 1; i < hi; i++) {
     line(0, i*x1, wide, i*x1); //das ist die breite X
     line(i*y1, 0, i*y1, hi); //das ist die hoehe y
  if (i >= hi){ //soll den for loop brechen falls bedingung erfuellt
     break;
     }
    //println(i);
  }
   
  endRecord();
  exit();  
 }
   
}
Page Index Toggle Pages: 1