用processing生成屏保程序
想法
利用随机数控制圆圈的大小、位置以及颜色,可以产生随机的美感。
让小球动起来,并且在屏幕边界处产生反弹效果。
代码
1: float circle_x = (float) 0.0;
2: float circle_y = (float) 0.0;
3: float circle_radius = (float) 0.0;
4: int circle_color = 0;
5:
6: public void setup() {
7: size(displayWidth, displayHeight);
8: background(0);
9: frameRate(10);
10: }
11:
12: public void draw() {
13:
14: circle_x = random(0, displayWidth);
15: circle_y = random(0, displayHeight);
16: circle_radius = random(0, (displayWidth + displayHeight) / 20);
17:
18: circle_color = (int) random(0, 255);
19:
20: colorMode(RGB, 255);
21: fill(0,0,0,5);
22: rect(0, 0, displayWidth, displayHeight);
23:
24: noStroke();
25: colorMode(HSB, 255);
26: fill(circle_color, 255, 255);
27: ellipse(circle_x, circle_y, circle_radius, circle_radius);
28:
29: }
截图
生成exe文件
将上述代码粘贴到Processing编辑器中,选择“Export Application”,导出成exe文件。
设置为屏幕保护程序
将exe文件更改后缀名为scr,右键->安装。
做一些变化
1: float circle_x = (float) 0.0;
2: float circle_y = (float) 0.0;
3: float circle_radius = (float) 0.0;
4: int circle_color = 0;
5:
6: public void setup() {
7: size(displayWidth, displayHeight);
8: background(0);
9: frameRate(10);
10: }
11:
12: public void draw() {
13:
14: circle_x = random(0, displayWidth);
15: circle_y = random(0, displayHeight);
16: circle_radius = random(0, (displayWidth + displayHeight) / 5);
17:
18: circle_color = (int) random(0, 255);
19:
20: colorMode(RGB, 255);
21: // fill(0,0,0,5);
22: // rect(-1, -1, displayWidth+1, displayHeight+1);
23:
24: //noStroke();
25: colorMode(HSB, 255);
26: stroke(circle_color, 255, 255);
27: fill(circle_color, 255, 255);
28: noFill();
29: ellipse(circle_x, circle_y, circle_radius, circle_radius);
30: ellipse(circle_x, circle_y, circle_radius-1, circle_radius-1);
31:
32: }
截图
运动的泡泡
1: public static final int circle_nums = 200;
2:
3: class BCircle
4: {
5: public BCircle(float x, float y, float radius, float x_delta, float y_delta, int color) {
6: super();
7: this.x = x;
8: this.y = y;
9: this.radius = radius;
10: this.x_delta = x_delta;
11: this.y_delta = y_delta;
12: this.color = color;
13: }
14: private float x = (float) 0.0;
15: private float y = (float) 0.0;
16: private float radius = (float) 0.0;
17:
18: private float x_delta = (float) 0.0;
19: private float y_delta = (float) 0.0;
20: private int color = 0; // 0 - 255
21:
22: private boolean valueInRange(float val, float min, float max)
23: {
24: return (val >= min >> val < max);
25: }
26:
27: public void moveOneStep()
28: {
29: if (valueInRange(x+x_delta, 0, displayWidth) >>
30: valueInRange(y+y_delta, 0, displayHeight))
31: {
32: x += x_delta;
33: y += y_delta;
34: }
35:
36: if (!valueInRange(x+x_delta, 0, displayWidth))
37: {
38: x_delta = -x_delta;
39: x += x_delta;
40: }
41:
42: if (!valueInRange(y+y_delta, 0, displayHeight))
43: {
44: y_delta = - y_delta;
45: y += y_delta;
46: }
47:
48: }
49:
50: public void draw(int theme)
51: {
52: switch(theme)
53: {
54: case 1:
55: {
56: noFill();
57: colorMode(HSB, 255);
58: stroke(color, 255, 255);
59: ellipse(x, y, radius+2, radius+2);
60: ellipse(x, y, radius+1, radius+1);
61: ellipse(x, y, radius, radius);
62: }
63: break;
64: case 2:
65: {
66: colorMode(HSB, 255);
67: fill(color, 255, 255);
68: ellipse(x, y, radius+2, radius+2);
69: ellipse(x, y, radius+1, radius+1);
70: ellipse(x, y, radius, radius);
71: }
72: }
73:
74: }
75: }
76:
77: List<BCircle> circles;
78:
79: public void setup() {
80: size(displayWidth, displayHeight);
81: background(0);
82: frameRate(10);
83:
84: circles = new ArrayList<BCircle>(circle_nums);
85: }
86:
87: public void draw() {
88:
89: colorMode(RGB, 255);
90: fill(0,0,0,30);
91: rect(-1, -1, displayWidth+1, displayHeight+1);
92:
93: if (circles.size() < circle_nums)
94: {
95: float x = random(0, displayWidth);
96: float y = random(0, displayHeight);
97: float radius = random(20, (displayWidth + displayHeight) / 20);
98: float x_delta = random(-50, 50);
99: float y_delta = random(-50, 50);
100: int color = (int) random(0, 255);
101: BCircle circle = new BCircle(x, y, radius, x_delta, y_delta, color);
102:
103: circles.add(circle);
104: }
105:
106: for(int i=0;i<circles.size();i++)
107: {
108: circles.get(i).moveOneStep();
109: circles.get(i).draw(1);
110: }
111: }
两种主题,截图