I don't want to keep writing the same code over and over.

I have written generic Point classes, line/line intersection algorithms and Layer administration code more times than I can remember. I don't like menial programming, so this is effectively my code library. But you can use it too!

The language used is Processing, which is a mostly-Java-syntax language. I hardly ever use Java-specific ninja code, so rewriting any of these snippets to your favourite language is less than a minute's worth of work, typically.

skeleton snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// REQUIRES: Drawable.pde

// a list of drawable elements
ArrayList<Drawable> drawables;
ArrayList<Point> points;

// a boilerplate setup
void setup() {
  size(500,500);
  noLoop();
  stroke(0);
  fill(0);
  drawables = new ArrayList<Drawable>();
  points = new ArrayList<Point>();
  setupAdditional();
}

// a boilerplate draw
void draw() {
  background(255);
  for(Drawable d: drawables) { d.draw(); }
  drawAdditional();
}

// unified drawable object administration
void addDrawable(Drawable d) {
  drawables.add(d);
  addPoints(d);
}

// add all points relating to a drawable to the point list
void addPoints(Drawable d) { 
  for(Point p: d.getPoints()) {
    points.add(p);
  }
}

// key handling: wrap to explicit methods
void keyPressed()  { keyPressed(key, keyCode);  redraw(); }
void keyReleased() { keyReleased(key, keyCode); redraw(); }
void keyTyped()    { keyTyped(key, keyCode);    redraw(); }

// Mouse handling: wrap to explicit methods
void mousePressed()  { mousePressed(mouseX, mouseY, mouseButton);  redraw(); }
void mouseReleased() { mouseReleased(mouseX, mouseY, mouseButton); redraw(); }
void mouseDragged()  { mouseDragged(mouseX, mouseY, mouseButton);  redraw(); }
void mouseMoved()    { mouseMoved(mouseX, mouseY);                 redraw(); }
void mouseClicked()  { mouseClicked(mouseX, mouseY, mouseButton);  redraw(); }

// actual event handling

void keyPressed(char key, int keyCode) {
  for(Drawable d: drawables) {
    d.keyPressed(key,keyCode); 
  }
  // additional code goes here
}

void keyReleased(char key, int keyCode) {
  for(Drawable d: drawables) {
    d.keyReleased(key,keyCode);
  }
  // additional code goes here
}

void keyTyped(char key, int keyCode) {
  for(Drawable d: drawables) {
    d.keyTyped(key,keyCode);
  }

  // additional code goes here
}

void mousePressed(float mouseX, float mouseY, int mouseButton) {
  for(Drawable d: drawables) {
    if(d.over(mouseX,mouseY)) {
      if(d.mousePressed(mouseX, mouseY, mouseButton)) {
        break;
      }
    }
  }

  // additional code goes here
}

void mouseReleased(float mouseX, float mouseY, int mouseButton) {
  for(Drawable d: drawables) {
    d.mouseReleased(mouseX, mouseY, mouseButton);
  }

  // additional code goes here
}

void mouseDragged(float mouseX, float mouseY, int mouseButton) {
  for(Drawable d: drawables) {
    d.mouseDragged(mouseX, mouseY, mouseButton);
  }

  // additional code goes here
}

void mouseClicked(float mouseX, float mouseY, int mouseButton) {
  for(Drawable d: drawables) {
    if(d.over(mouseX,mouseY)) {
      if(d.mouseClicked(mouseX, mouseY, mouseButton)) {
        break;
      }
    }
  }

  // additional code goes here
}

void mouseMoved(float mouseX, float mouseY) {
  cursor(ARROW);

  for(Drawable d: drawables) {
    if(d.over(mouseX,mouseY)) {
      d.mouseMoved(mouseX, mouseY);
      cursor(HAND);
    }
  }

  // additional code goes here
}

Back to the main page

Download all


Written and maintained by Mike "Pomax" Kamermans. All code snippets on this site are "do whatever you want with it" licensed.