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

**URL:** https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288
**Category:** Coding Questions
**Created:** [June 24, 2023, 9:55pm UTC](https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288 "2023-06-24T21:55:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Illuminaria](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@Illuminaria](https://discourse.processing.org/u/Illuminaria)
#### Post date: [June 24, 2023, 9:55pm UTC](https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288/1 "2023-06-24T21:55:16Z")

</div>

\<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];

```

}  
}  
}

>

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 25, 2023, 2:20am UTC](https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288/2 "2023-06-25T02:20:06Z")

</div>

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

```auto
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.");
} 

```

---

<div class="post-metadata">

### Author: ![Illuminaria](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@Illuminaria](https://discourse.processing.org/u/Illuminaria)
#### Post date: [June 25, 2023, 5:30pm UTC](https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288/3 "2023-06-25T17:30:52Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 25, 2023, 6:20pm UTC](https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288/4 "2023-06-25T18:20:56Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![Illuminaria](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@Illuminaria](https://discourse.processing.org/u/Illuminaria)
#### Post date: [June 25, 2023, 7:05pm UTC](https://discourse.processing.org/t/need-help-with-printing-paper-to-an-actual-printer/42288/5 "2023-06-25T19:05:04Z")

</div>

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.
