Need help with printing (paper, to an actual printer)

<I’m an art student that got a Processing project in three stages for my Digital Art Class. No a programmer by myself.
I ended up making a humble but nice autogerenating art code. What i need for the instalation is Processing taking a screenshot and deliver it straight to printing, not even going through the printer setup part.
I keep googling and googling and can’t find an answer for that.
It’s possible to get a brief explanation/code guide for how can i achieve it?

Thanks for reading.

There is one of the codes i’ll run:

float x;
float y;
float variacionX;
float variacionY;
float xAnterior;
float yAnterior;
int Animaciones=48;

void setup() {
size(1360,768);

x=new float[Animaciones];
y=new float[Animaciones];
variacionX=new float[Animaciones];
variacionY=new float[Animaciones];
xAnterior=new float[Animaciones];
yAnterior=new float[Animaciones];

for (int i=0;i<Animaciones;i++) {
x[i]=random(width);
y[i]=random(height);
xAnterior[i]=x[i];
yAnterior[i]=y[i];
}

background(0);
}

void draw() {
if (mousePressed) {
background(0);
}
else {
for (int i=0;i<Animaciones;i++) {
variacionX[i] = random(-5,5);
x[i]+=variacionX[i];
variacionY[i]=random(-5,5);
y[i]+=variacionY[i];

if (x[i]<0) {
  x[i]=0;
} else if (x[i]>width) {
  x[i]=width;
}

if (y[i]<0) {
  y[i]=0;
} else if (y[i]>height) {
  y[i]=height;
}

stroke(255,20);
line(xAnterior[i],yAnterior[i],x[i],y[i]);
xAnterior[i]=x[i];
yAnterior[i]=y[i];

}
}
}

1 Like

The following source code works on my system (Mac). You’ll need to change ‘mySaveStr’ to reflect your filename and system:

void setup() {
  size(400, 400);
  surface.setTitle("Mouse press to send to printer.");
  PImage img = createImage(264, 264, RGB);
  img.loadPixels();
  for (int i = 0; i < img.pixels.length; i++) {
    img.pixels[i] = color(0, 90, 102);
  }
  img.updatePixels();
  image(img, 68, 68);
}

void draw() {
}

void mousePressed() {
// Edit this string for your system
  String mySaveStr = "/Users/yourName/Desktop/image.png";
  save(mySaveStr);
  exec("lp", mySaveStr);
  println("Image sent to printer.");
} 
1 Like

Well, thanks for taking your time. The deal is, there is no filename, there should be only clipboard data.
What a i need is directly print what’s on screen by using keyPressed or mousePressed.

With this technique, if you don’t want to save the image then you would have to print it and then delete the file with a delay() in between.

void mousePressed() {
  String mySaveStr = "/Users/xxxxxx/Desktop/image2.png";
  save(mySaveStr);
  exec("lp", mySaveStr);
  println("Image sent to printer.");
  delay(1000);
  File imgFile = new File(mySaveStr);
  if (imgFile.exists()) {
    imgFile.delete();
    println("imgFile deleted.");
  }
} 

There are other ways of printing image files using Java, but they are more complex and you would have to do your drawing on one of their controls, for example, a JPanel.

Thanks a lot, man. I can work with this.
If i crack exactly what i mean for this installations, i’ll share the code for anyone who wants something like that.