Anders Strøm Møller - 3 years ago 189

Java Question

so, as an assaignment i want to make an analog clock, and i am pretty far. I only need the numbers around the clock, but i cant figure out how to make these. I have made some dots right now, but i want to replace theese with the numbers 1-12.. does any1 know an easy and fast way of doing this? my code is as following:

`int cx, cy;`

float secondsRadius;

float minutesRadius;

float hoursRadius;

float clockDiameter;

void setup() {

size(1366,768);

stroke(255);

float radius = min(width/1.2, height/1.2) / 2;

secondsRadius = radius * 0.72;

minutesRadius = radius * 0.60;

hoursRadius = radius * 0.50;

clockDiameter = radius * 1.8;

cx = width / 2;

cy = height / 2;

}

void draw() {

background(random(0,255),random(0,255),random(0,255));

// Draw the clock background

fill(0);

noStroke();

ellipse(cx, cy, clockDiameter, clockDiameter);

// Angles for sin() and cos() start at 3 o'clock;

// subtract HALF_PI to make them start at the top

float s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;

float m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;

float h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;

// Draw the hands of the clock

stroke(255);

strokeWeight(1);

line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);

strokeWeight(2);

line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius);

strokeWeight(4);

line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius);

// Draw the dots arround the clock

strokeWeight(2);

beginShape(POINTS);

for (int a = 0; a < 360; a+=30) {

float angle = radians(a);

float x = cx + cos(angle) * secondsRadius;

float y = cy + sin(angle) * secondsRadius;

vertex(x, y);

}

endShape();

textSize(40);

text("Dank Clock", 570,40);

}

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You already have a loop that goes around the clock and places dots at the clock positions. Now all you need is some logic that draws the hour at those positions.

Processing has a `text()`

function that allows you to draw text (or numbers) to the screen. You can just call that instead of `vertex()`

to draw the hours.

To get the hours to draw, just use an `int`

variable that you increment each time through the loop. Something like this:

```
int hour = 3;
for (int a = 0; a < 360; a+=30) {
float angle = radians(a);
float x = cx + cos(angle) * secondsRadius;
float y = cy + sin(angle) * secondsRadius;
vertex(x, y);
fill(255);
text(hour, x, y);
hour++;
if(hour > 12){
hour = 1;
}
}
```

Notice that I'm starting at `3`

because your angle starts at `0`

, which points all the way to the right. When the loop goes over the 12, I just start `hour`

back over at `1`

.

You could probably also figure out a simple formula that maps from `a`

to an hour, that way you don't have to do the incrementing yourself.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**