Writing out sentences with mouse

Hello, I am not a programmer in any way, so I barely know anything about coding. We are doing a code based imagery project for my art class using Java coding. We are supposed to eventually turn it into a drawing app and I wanted to use words to draw with but I am not sure how to do that or if its possible. I want to do something like this:

this is what I have so far:


void setup() {
  size(1000, 1000);
  fill(126);
  background(102);
  cursor(CROSS);
}

void draw() {
    //text("80's mixtape",500,400);
    PFont mono;
    mono = createFont("Serif-48.vlw",50);
    textFont(mono);
   textAlign(CENTER,CENTER);
   text("80's mixtape",500,400);
    fill(255,random(255),random(255),255);
    if (key == 'j')
    text("~Don't stop believin'~",mouseX,mouseY);
    fill(random(255),random(255),random(255),random(255));
    textSize(30);
    if (key == 't')
    text("~I think we're alone now~",mouseX,mouseY);
    fill(random(255), 255, 255, 255);
    textSize(30);
 if (key == 'r')
    text("~Never gonna give you up~",mouseX,mouseY);
    fill(255,random(255),255,255);
    textSize(30);
}

I apologize in advance if I posted this in an incorrect format. Please and thank you for any advice you can offer.

1 Like

You can format the Code by using the </> sign when posting/editing.

As for what you are trying to doā€¦

Do you want to have the text as a predefined pattern, or be displayed along a drawn path (like in the sketch you linked)?

I just assume, that having the text like in your Code (iā€˜ll Just call it Windows Freeze Style) is not what you want, right?

1 Like

Displayed along a drawn path like in the sketch.

Yea, what I have it not what I want. Its just what I could manage to do.

Actually, i meant the sign in the menu above when posting.

:left_speech_bubble: B I | :chains: ā€ </> and so onā€¦ (the signs arenā€˜t really that close to the originals :sweat_smile:

(Posting a new Message instead of editing the old one for notification reasonsā€¦)

As for how to achieve what you want to do, the simplest approach would probably be, to have an array with the mouse positions (the positions where the mouse was while mousePressed was true every n frame).

For that, you might actually want to use a List, since youā€˜ll need to dynamically append the values, or you can go with an array of size Text.length(), if you want the drawing to stop when the text ends (like a pen missing ink at some point), or to restart the text there, in which case the first part could be deletedā€¦

Then, once you decided which approach to take, you just need to iterate over each of your positions and display the respective char at the given position.

Itā€˜s way easier than it sounds, but youā€˜ll need to decide on the approach before i can get into more Detail, Else itā€˜s just gonna be a lot to cover.

Having the drawing stop when the text ends I think would be fine. I think I get what you are saying about arrays but am not for sure and that is my own fault. A lot of this code stuff is pretty confusing for me. :frowning: Iā€™m really sorry.

Well, since you want to go with a simple array instead of Lists (arrays have fixed size), itā€˜s going to be a bit easier.

Also, itā€˜s generally confusing when you start programming, just like itā€˜s confusing to have to write in a language you donā€˜t knowā€¦ itā€˜s obviously going to take a while, but itā€˜s a lot simpler than learning a new language, since most of the things are already translated to english, so you can what things do or how to do things.

What you need to keep in mind, is that programming is basically just 2 things. Knowing what you want and knowing how to split it up into smaller parts (individual commands). Well, thereā€˜s also other stuff like knowing what is more efficient, how to do certain things, a bit of Math is also useful, knowing where to look for answers (documentation/references, github and so on) and other things, but they come naturally over time and thereā€˜s no need to know everything at once.

As for your Code, what you want is to display Text in a drawn path. So what we need for that, is :

ā€¢a way to display text in specific positions
For this we need :
ā€¢the specific positions
ā€¢the character to display in these positions
ā€¢An array of characters or a String. Weā€˜ll just use ā€œHelloā€ for now.
ā€¢The index at which the char is that we want to display at the position of the path array with the same index

ā€¢a way to get/set those locations
For that we need :
ā€¢mouse position while the mouse is pressed
ā€¢an if statement, to check if the mouse is pressed
ā€¢the last index we set in the path array, to set the position to the next index
ā€¢an array to store the position
ā€¢we need to know the length of the text

So these will be the lines of Code we need (not in that order/ not used in this way) :

//our text
String text = "Hello";

//the index we last saved a position to
int index; 

//iā€˜d rather use PVector[] for this, but letā€˜s go with this for starters
//The positions of our path
float[] pathX = new float[text.length()];
float[] pathY = new float[text.length()];

//a way to display the char at the position (both having the same index)
text(char c, float x, float y);

//a way to check if we want to add a new position
if (mousePressed) {}

//a way to check if we still want to add positions
if (index < text.length()-1) {}

//the actual positions for the points of our path
mouseX;
mouseY;

//Note that the length of an array is read with .length, while the length of a String is read using .length().
//a way to iterate over each position in the array/each char in the text we already have a position for
for (int i = 0; i < index; i++) {}

//a way to space out the characters a bit
dist();//to measure the distance to the last position we have, to space the chars out a bit

//a way to get a char at an index of a String
text.charAt(i);

I hope i didnā€˜t forget anything thereā€¦ :sweat_smile:

That should be all you need to do it. If you need help with anything, just tell me :wink:

1 Like

Thank you very much, I will do my best to work with this. Iā€™m still somewhat confused. Iā€™ve never done coding before and our art teacher didnā€™t really give us much to work with and itā€™s due tomorrow morning. I prob should have just asked sooner for help. Thank you again.

Well, iā€˜ll just add this here behind a spoiler, if you need some hints, but try to do it yourself first. Itā€˜s not all that difficult, but getting the details right can be quite annoying (just fought for half an hour, because apparently having a variable called ā€žtextā€œ is not good with the IOS version :expressionless: )

Spoiler
int index = 0;

String txt = "Hellooooooooo543210";

float[] pathX = new float[txt.length()];
float[] pathY = new float[txt.length()];

void setup() {
   size(800, 600);
}

void draw() {
   background(0,0,255);
   
   for (int i = 0; i < index; i++) {
      text(txt.charAt(i), pathX[i], pathY[i]);
   }
   
   if (mousePressed) {
      if (index > 0 && index < txt.length()) {
         if (dist(pathX[index-1], pathY[index-1], mouseX, mouseY) > 20) {
            pathX[index] = mouseX;
            pathY[index] = mouseY;
            index++;
         }
      } else if (index == 0){
         pathX[0] = mouseX;
         pathY[0] = mouseY;
         index++;
      }
   }
}

Thank you again.
Is there a way to keep the letters from turning backwards when writing out? Itā€™s not a big deal if they canā€™t be, just curious

Uhmā€¦ this might be a bit Late anyway, but what do you mean by keep from turning backwards? The letters shouldnā€˜t turn at all, since thereā€˜s no Code for rotations (although you could add that, to make it more readable, but that would involve some more complex calculations)