# Drawing program

**URL:** https://discourse.processing.org/t/drawing-program/12510
**Category:** Coding Questions
**Created:** [July 5, 2019, 1:44am UTC](https://discourse.processing.org/t/drawing-program/12510 "2019-07-05T01:44:28Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![AdrienGh](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@AdrienGh](https://discourse.processing.org/u/AdrienGh)
#### Post date: [July 5, 2019, 1:44am UTC](https://discourse.processing.org/t/drawing-program/12510/1 "2019-07-05T01:44:28Z")

</div>

Hello everyone,

I’m new to programming. I could use your help.  
I want to create a simple interface to draw from a circle.

The problem is that I can’t draw on this circle. And I wish I could draw on it.

```auto
PImage gomme;
PImage save;
PImage delete;
PImage circle;
PFont font;
//PShape ellipse;

void setup() {
  size(768, 1024);
  background(255);
  gomme = loadImage("gomme.png");
  save = loadImage("save.png");
  delete = loadImage("delete.png");
  circle = loadImage("circle.png");
  //ellipse = loadShape("ellipse.svg");
  font = loadFont("Grotex-Regular-48.vlw");
  textFont(font);
 
} 

void draw(){
 
  image(gomme, 600, 20, 64, 74);
  image(save, 520, 20, 64, 74); 
  image(delete, 680, 20, 64, 74);
  //shape(ellipse, 300, 380, 188, 188);
  image(circle, 300, 380, 188, 188);  
  textSize(48);
  text("un soleil", 20, 60); 
  fill(210);
  
  
  
   stroke(0);
    strokeWeight(6);
  if (mousePressed == true) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
  
  if(mousePressed){
    if(mouseX > 600 && mouseX <664){
      if (mouseY > 20 && mouseY <94){
        background(255);
      }
    }
  }

}

```

Solutions?

Thank you in advance

---

<div class="post-metadata">

### Author: ![KTibow](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KTibow](https://discourse.processing.org/u/KTibow)
#### Post date: [July 5, 2019, 3:33am UTC](https://discourse.processing.org/t/drawing-program/12510/2 "2019-07-05T03:33:35Z")

</div>

There’s a couple of things to consider.

1. Try changing the stroke color.
2. `mousePressed == true` means the same thing as `mousePressed`.

If you share the images you are loading as well, I might be able to make it work.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 5, 2019, 9:51am UTC](https://discourse.processing.org/t/drawing-program/12510/3 "2019-07-05T09:51:34Z")

</div>

You draw over the lines from the last draw cycle with your images with each new cycle of draw();

Consider:

1. draw images once in setup and NOT in main loop of draw();  
**OR** once in draw() but NOT at the start of every draw() (consider this later…)
2. draw images again with your background update with a mousePressed.
3. Put the images in an update() method; it would be easier to call that method as required.

That will work.

🙂

I just reduced your code so I could test it.  
I did not provide a solution below; I left that for you.

```auto
void setup() 
  {
  size(768, 1024, P2D);
  background(255);
  } 

void draw()
  {
  fill(210);
  stroke(0);
  strokeWeight(6);
  circle(300, 380, 188); 

  textSize(48);
  text("un soleil", 20, 60); 
  
  if (mousePressed == true) 
    {
    line(mouseX, mouseY, pmouseX, pmouseY);
    }
  
  if(mousePressed)
    {
    if(mouseX > 600 && mouseX <664)
      {
      if (mouseY > 20 && mouseY <94)
        {
        background(255);
        // Good place to add other updates
        }
      }
    }
  }  

```

---

<div class="post-metadata">

### Author: ![Glowtube](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Glowtube](https://discourse.processing.org/u/Glowtube)
#### Post date: [July 5, 2019, 9:55am UTC](https://discourse.processing.org/t/drawing-program/12510/4 "2019-07-05T09:55:46Z")

</div>

Another way would be storing the lines in an ArrayList and drawing all lines from that ArrayList in every draw.

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 5, 2019, 10:29am UTC](https://discourse.processing.org/t/drawing-program/12510/5 "2019-07-05T10:29:02Z")

</div>

Hey There!

Another suggestion is to use the method mousePresssed for your conditions within the if statement. But once you put it in the mouse pressed method you don’t have to have the if statements.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 5, 2019, 12:08pm UTC](https://discourse.processing.org/t/drawing-program/12510/6 "2019-07-05T12:08:44Z")

</div>

I came up with a small class with buttons

**The buttons**

- The delete button works,
- the circle button works (toggle between line and circle mode)

The buttons are stored in an ArrayList `buttons` containing objects of type `Button` (from the `class Button`). There is a tutorial about classes and objects: [https://www.processing.org/tutorials/objects/](https://www.processing.org/tutorials/objects/).  
The buttons get an **ID** (0,1,2…) in the order they are created.  
When evaluating the mouse in the function `mousePressed()` we use the ID to trigger the function `exec()` which executes the command for the ID (e.g. clear the canvas or set the draw mode `mode` to `circleMode`).  
When using a button, the variable `hold` is true; in `draw()` we evaluate `hold` so we avoid drawing a line on a button in this case.

**The class Button**

The class Button could in theory handle images and text, since I don’t have the images I tested it only with text.  
In the constructor of the class (when `defineButton()` would pass w and h to the constructor) you could handle `resize` of images `img` once and for all using w and h (usage of `resize()` once is better than using `image()` with 5 parameters which is doing a resize every time, very time consuming for the processor).

Chrisir

```auto

PFont font;
//PShape ellipse;

ArrayList<Button> buttons = new ArrayList();  

boolean hold=false;

// define modes 
final int lineMode = 0;
final int circleMode = 1;
//current mode
int mode = lineMode;
// names of modes
String[] stringMode={ "line", "circle (click, hold and move mouse)" };

// draw a circle variables 
boolean drawNewCircle;
PVector startCircle=new PVector(); 

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

  PImage gomme;
  PImage save;
  PImage delete;
  PImage circle;

  background(255);
  gomme = loadImage("gomme.png");
  save = loadImage("save.png");
  delete = loadImage("delete.png");
  circle = loadImage("circle.png");
  //ellipse = loadShape("ellipse.svg");

  // font = loadFont("Grotex-Regular-48.vlw");
  // textFont(font);

  defineButton(gomme, "gomme", 600, 20, 64, 74);
  defineButton(save, "save", 520, 20, 64, 74); 
  defineButton(delete, "delete", 680, 20, 64, 74);
  //shape(ellipse, 300, 380, 188, 188);
  defineButton(circle, "circle (on/off)", 300, 380, 188, 188);
} 

void draw() {
  textAlign(LEFT);
  fill(255); 
  noStroke(); 
  rect(0, 0, width, 30); 
  fill(0); 
  textSize(17);
  text("mode is " + stringMode[mode], 22, 22); 
  fill(210);

  for (Button b : buttons)
    b.display(); 

  textSize(48);
  text("un soleil", 20, 60); 
  fill(210);

  stroke(0);
  strokeWeight(6);
  if (!hold) {
    switch(mode) {
    case circleMode:
      // ignore
      break; 
    case lineMode:
      if (mousePressed) {
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
      break;
    default:
      //Error 
      break;
    }//switch
  }//if

  if (drawNewCircle) {
    float a1 = 2 * dist (mouseX, mouseY, 
      startCircle.x, startCircle.y);
    stroke(0); 
    ellipse( startCircle.x, startCircle.y, a1, a1);
  }//if
  //
}

// -----------------------------------------------------

void mousePressed() {
  for (Button b : buttons) {
    if (b.over()) {
      exec(b.id);
      hold=true; 
      return;
    }//if
  }//for
  if (mode==circleMode) {
    startCircle.x=mouseX;
    startCircle.y=mouseY;
    drawNewCircle=true;
  }
}

void mouseReleased() {
  hold=false;
  drawNewCircle=false;
}

// -------------------------------------------------------------------

void exec(int id) {
  println(id);

  switch (id) {
  case 2:
    background(255);
    break;
  case 3:
    if (mode!=circleMode)
      mode=circleMode; 
    else 
    mode=lineMode;
    break;
  default:
    break;
  }//switch
}

void defineButton(PImage img, 
  String s, 
  float x, float y, 
  float w, float h) {
  buttons.add (new Button(img, s, x, y, buttons.size()));
}

//===================================================================

class Button {

  PImage img=null; 
  String s; 
  float x, y; 
  int id; 

  Button(PImage img_, 
    String s_, 
    float x_, float y_, 
    int id_) {
    img=img_; 
    s=s_;
    x=x_;
    y=y_;

    id=id_;
  }

  void display() {
    if (img!=null) {
      imageMode(CENTER); 
      image(img, x, y);
      return;
    }

    textAlign(CENTER);
    textSize(17);
    text(s, x, y);
    textAlign(LEFT);
  }//

  boolean over() {
    return 
      dist (mouseX, mouseY, x, y) < 30;
  }
  //
}//class
//

```

---

<div class="post-metadata">

### Author: ![KTibow](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KTibow](https://discourse.processing.org/u/KTibow)
#### Post date: [July 5, 2019, 1:53pm UTC](https://discourse.processing.org/t/drawing-program/12510/7 "2019-07-05T13:53:26Z")

</div>

Let’s try to keep this simple, guys.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 5, 2019, 2:10pm UTC](https://discourse.processing.org/t/drawing-program/12510/8 "2019-07-05T14:10:56Z")

</div>

Actually, this is a good comment… because my sketch is too complex for a beginner…

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 5, 2019, 3:16pm UTC](https://discourse.processing.org/t/drawing-program/12510/9 "2019-07-05T15:16:21Z")

</div>

> [@InferNova](#):
>
> Another suggestion is to use the method mousePresssed for your conditions

I examined the mousePressed() function and mousePressed variable behavior and provided a couple of examples to illustrate this.

Observe console outptut:

```auto
void setup() 
  {
  size(200, 200);
  background(255, 255, 0);
  } 

void draw()
  {
  } 
  
void mousePressed()
  {
  println(frameCount, mouseButton, mousePressed);
  }

```

Observe console output:

```auto
void setup() 
  {
  size(200, 200);
  background(255, 255, 0);
  } 

void draw()
  {
  if (mousePressed) 
    println(frameCount, mouseButton, mousePressed);
  }

```

🙂

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 5, 2019, 3:39pm UTC](https://discourse.processing.org/t/drawing-program/12510/10 "2019-07-05T15:39:58Z")

</div>

The function mousePressed is called only once during a pressing of the mouse. That’s good for buttons.

It won’t give good results when drawing continuous lines though imo. For those the variable mousePressed is better.

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 5, 2019, 5:48pm UTC](https://discourse.processing.org/t/drawing-program/12510/11 "2019-07-05T17:48:33Z")

</div>

Yes those are good suggestions ! I guess I was proposing mouse pressed method because for example if OP is using the lines to build line shapes using it in draw will call multiple times on one line unnecessarily creating many instances of it. Depending on what OP requires either or can be correct !

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 5, 2019, 7:04pm UTC](https://discourse.processing.org/t/drawing-program/12510/12 "2019-07-05T19:04:00Z")

</div>

> [@InferNova](#):
>
> will call multiple yes on one line inecessaripy creating many instances of it

When filling points in an Arraylist of type PVector before adding we check if the value has changed or not

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 5, 2019, 7:52pm UTC](https://discourse.processing.org/t/drawing-program/12510/14 "2019-07-05T19:52:20Z")

</div>

Yes great suggestion ! In that way we wouldn’t draw over so many points.
