Note: No need for beginShape/endShape because you already draw closed shapes.
And yes, using classes might be a good, flexible solution.
There are at least two ways of designing such class: one, I didn't took, is to make one class, and to feed each instance with data to draw the rectangles, storing this data into an array for example.
Another, which I illustrate here, is to make a master, generic (abstract in Java lingo) class and to make a specialized class per letter. It might need slightly more code, but is it much more flexible: for example, you can use another shape for a part of a letter.
File LetterShape.java:
Code:import processing.core.*;
abstract class LetterShape
{
static final int RECT_SCALE = 10;
PApplet pa;
int x, y; // Position
// You can add other parameters like color...
public LetterShape(PApplet pap, int posX, int posY)
{
pa = pap;
x = posX;
y = posY;
}
void drawLetter()
{
pa.pushMatrix();
pa.translate(x, y); // Put letter in place on screen
drawShape();
pa.popMatrix(); // Undo translate
}
void drawRect(int px, int py, int w, int h)
{
pa.rect(px*RECT_SCALE, py*RECT_SCALE, w*RECT_SCALE, h*RECT_SCALE);
}
abstract void drawShape();
}
class LetterZ extends LetterShape
{
public static final int WIDTH = 5;
public LetterZ(PApplet p, int x, int y) { super(p, x, y); }
void drawShape()
{
drawRect(1, 1, 5, 1);
drawRect(4, 2, 2, 1);
drawRect(2, 3, 3, 1);
drawRect(1, 4, 2, 1);
drawRect(1, 5, 5, 1);
}
}
class LetterI extends LetterShape
{
public static final int WIDTH = 1;
public LetterI(PApplet p, int x, int y) { super(p, x, y); }
void drawShape()
{
drawRect(1, 1, 1, 5);
}
}
class LetterY extends LetterShape
{
public static final int WIDTH = 3;
public LetterY(PApplet p, int x, int y) { super(p, x, y); }
void drawShape()
{
drawRect(1, 1, 1, 3);
drawRect(2, 2, 1, 4);
drawRect(3, 1, 1, 3);
}
}
File LetterShapes.pde:
Code:LetterShape lI, lY, lZ;
void setup()
{
size(800, 400);
smooth();
int pos = 0;
lI = new LetterI(this, pos, 0);
pos += LetterShape.RECT_SCALE * (1 + LetterI.WIDTH);
lZ = new LetterZ(this, pos, 0);
pos += LetterShape.RECT_SCALE * (1 + LetterZ.WIDTH);
lY = new LetterY(this, pos, 0);
}
void draw()
{
background(200);
fill(#195060);
translate(mouseX, mouseY);
lI.drawLetter();
lZ.drawLetter();
lY.drawLetter();
// Matrix is reset here
}
Code is still a bit rough, can get some more improvements...