OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.effect;
15
 
16
import java.awt.Color;
17
import java.awt.Dimension;
18
import java.awt.Graphics;
19
import java.awt.Image;
20
import java.awt.MediaTracker;
21
import java.awt.event.MouseAdapter;
22
import java.awt.event.MouseEvent;
23
import java.awt.image.ColorModel;
24
import java.awt.image.DirectColorModel;
25
import java.awt.image.MemoryImageSource;
26
import java.awt.image.PixelGrabber;
27
 
28
import javax.swing.ImageIcon;
29
import javax.swing.JComponent;
30
import javax.swing.JFrame;
31
 
32
public class Spots extends JComponent implements Runnable {
33
 
182 ilm 34
    // Allow to disable (more than 10% CPU on x2go VM (java itself + x2goagent))
35
    static public final String START_SLEEPING_PROP_NAME = Spots.class.getSimpleName() + ".startSleeping";
36
    static final boolean START_SLEEPING = Boolean.getBoolean(START_SLEEPING_PROP_NAME);
37
 
17 ilm 38
    static int width = 256;
39
    static int height = 48;
40
 
41
    private int x;
42
    private int y;
43
    private int K;
44
 
45
    private double P;
46
    private double Q;
47
    private double R;
48
    private double S;
49
 
50
    private double U;
51
    private double V;
52
    private double W;
53
    private double X;
54
    private Thread updateThread;
55
 
56
    private MediaTracker mediaTracker;
57
    private Image imagePalette;
58
    private Image imageSpot;
59
    private boolean initDone;
60
    private boolean ga;
61
    private int ha;
62
 
63
    private int ka;
64
    private float ma;
65
 
66
    private int offset;
67
    private int pa;
68
 
69
    private int pixles[];
70
    private int ua[];
71
    private int va[];
72
 
73
    private Image onScreenImage;
74
    private MemoryImageSource imageSource;
182 ilm 75
    private long sleeptime;
17 ilm 76
    private static final int DEFAULT_SLEEPTIME = 20;
182 ilm 77
    private static final int STOP_SLEEPTIME = 60 * 60 * 1000;
17 ilm 78
 
79
    public static void main(String[] args) {
80
        JFrame f = new JFrame();
81
        final Spots blob1 = new Spots();
82
        blob1.setPreferredSize(new Dimension(width, height));
83
 
84
        f.setContentPane(blob1);
85
 
86
        f.pack();
87
 
88
        f.setVisible(true);
89
 
90
    }
91
 
92
    public Spots() {
182 ilm 93
        this(START_SLEEPING, true);
94
    }
17 ilm 95
 
182 ilm 96
    public Spots(final boolean startSleeping, final boolean addMouseListener) {
97
        this.sleeptime = startSleeping ? STOP_SLEEPTIME : DEFAULT_SLEEPTIME;
17 ilm 98
        ga = true;
99
 
100
        pixles = new int[width * height];
101
        ua = new int[0x10100];
102
        va = new int[4146];
103
        setPreferredSize(new Dimension(width, height));
104
        setMinimumSize(new Dimension(width, height));
105
        setMaximumSize(new Dimension(width, height));
182 ilm 106
        if (addMouseListener) {
107
            this.addMouseListener(new MouseAdapter() {
108
                @Override
109
                public void mouseClicked(MouseEvent e) {
110
                    if (sleeptime == DEFAULT_SLEEPTIME) {
111
                        // Redessine un fois par heure
112
                        sleeptime = STOP_SLEEPTIME;
113
                    } else {
114
                        sleeptime = DEFAULT_SLEEPTIME;
115
                        updateThread.interrupt();
116
                    }
17 ilm 117
                }
182 ilm 118
            });
119
        }
17 ilm 120
    }
121
 
122
    public void init() {
123
        setBackground(Color.black);
124
        ColorModel colorModel = new DirectColorModel(32, 0xff0000, 0x00ff00, 0x0000FF, 0);
125
 
126
        imageSource = new MemoryImageSource(width, height, colorModel, pixles, 0, width);
127
 
128
        imageSource.setAnimated(true);
129
        imageSource.setFullBufferUpdates(true);
130
        onScreenImage = createImage(imageSource);
131
        mediaTracker = new MediaTracker(this);
132
        imagePalette = new ImageIcon(Spots.class.getResource("pal3.gif")).getImage();
133
        mediaTracker.addImage(imagePalette, 0);
134
        imageSpot = new ImageIcon(Spots.class.getResource("blob.gif")).getImage();
135
        mediaTracker.addImage(imageSpot, 1);
136
 
137
        initDone = false;
138
        for (int i = 0; i < width * height; i++)
139
            pixles[i] = 156565;
140
 
141
        for (int j = 0; j < 0x10000; j++)
142
            ua[j + 256] = -1;
143
 
144
        P = 0.0D;
145
        Q = 1.0D;
146
        R = 2D;
147
        S = 3D;
148
 
149
    }
150
 
151
    public void rotatePaletteAt(int i, int j) {
152
 
153
        offset = i + j * width;
154
        pa = 0;
155
        for (int k = 0; k < 64; k++) {
156
            for (int l = 0; l < 64; l++) {
157
                if (offset >= 0 && offset < pixles.length) {
158
 
159
                    pixles[offset] += va[pa];
160
                }
161
                pa++;
162
                offset++;
163
            }
164
            offset += width - 64;
165
        }
166
 
167
    }
168
 
169
    public void update(Graphics g) {
170
        paint(g);
171
    }
172
 
173
    public void paint(Graphics g) {
174
        start();
175
        if (initDone) {
176
            ha++;
177
            P += 0.04D * ma;
178
            Q += 0.054D * ma;
179
            R += 0.06D * ma;
180
            S += 0.034D * ma;
181
            for (int i = 0; i < width * height; i++)
182
                pixles[i] = 0;
183
 
184
            float centerX = (width - 64) / 2.0F;
185
            float centerY = (height - 64) / 2.0F;
186
            U = P;
187
            V = Q;
188
            W = R;
189
            X = S;
190
            for (int j = 0; j < 6; j++) {
191
                x = (int) (80 + 30 * Math.cos(U));
192
                y = (int) (centerY + 18 * Math.sin(U));
193
 
194
                rotatePaletteAt(x, y);
195
                x = (int) ((double) centerX + 50 + (centerX / 2) * Math.cos(-W));
196
                y = (int) (centerY + (centerY - 10F) * Math.sin(-W));
197
 
198
                rotatePaletteAt(x, y);
199
                x = (int) (width + 20 + 18 * Math.cos(W + 1));
200
                y = (int) (centerY + 18 * Math.sin(W));
201
                rotatePaletteAt(x, y);
202
                U += 0.10999999940395355D;
203
                V += 0.34000000357627869D;
204
                W += 2.440000057220459D;
205
                X += 9.4399995803833008D;
206
            }
207
 
208
            for (int k = 0; k < width * height; k++) {
209
                pixles[k] = ua[pixles[k]] & 0x00FFFFFF;
210
                pixles[k] = -pixles[k] + 0x00FFFFFF;
211
                int b = (pixles[k]) & (0x000000FF);
212
                float bf = ((255 - b));
213
 
214
                // System.out.println("b:"+b);
215
                int red = b + (int) ((bf) * (62f / 255));
216
 
217
                int green = b + (int) ((bf) * (5f / 255));
218
                int blue = b + (int) ((bf) * (141f / 255));
219
 
220
                /*
221
                 * if(b>200){ blue+=5; red+=5; green+=10; }
222
                 */
223
                if (red > 255)
224
                    red = 255;
225
                if (green > 255)
226
                    green = 255;
227
                if (blue > 255)
228
                    blue = 255;
229
 
230
                // Color c = new Color(red, green, blue);
231
                int value = ((255 & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
232
                pixles[k] = value;// c.getRGB();//
233
                // <<b<<b;//0xFF+(b)*0xFF+0x00FFFF;//+0xFF;//+0xFF*b+0xFFFF*b;
234
                // System.out.println(pixles[k]+" /"+0x00FFFFFF);
235
 
236
            }
237
            if (imageSource != null)
238
                imageSource.newPixels();
239
 
240
            g.drawImage(onScreenImage, 0, 0, null);
241
            getToolkit().sync();
242
        }
243
    }
244
 
245
    private void b() {
246
 
247
        if (ga)
248
            while (!mediaTracker.checkAll(true))
249
                try {
250
                    Thread.sleep(20L);
251
                } catch (Exception exception) {
252
                }
253
        PixelGrabber pixelgrabberPalette = new PixelGrabber(imagePalette, 0, 0, 256, 1, ua, 0, 256);
254
        try {
255
            pixelgrabberPalette.grabPixels();
256
        } catch (InterruptedException interruptedexception) {
257
        }
258
        PixelGrabber pixelgrabberSpot = new PixelGrabber(imageSpot, 0, 0, 64, 64, va, 0, 64);
259
        try {
260
            pixelgrabberSpot.grabPixels();
261
        } catch (InterruptedException interruptedexception1) {
262
        }
263
        offset = 0;
264
        for (int i = 0; i < 64; i++) {
265
            for (int j = 0; j < 64; j++) {
266
                K = va[offset] & 0xff;
267
                va[offset] = K;
268
                offset++;
269
            }
270
 
271
        }
272
 
273
        initDone = true;
274
    }
275
 
276
    public void start() {
277
        if (updateThread == null) {
278
            init();
279
            b();
280
            updateThread = new Thread(this, this.getClass().getName());
281
            updateThread.setDaemon(true);
282
            updateThread.start();
283
        }
284
    }
285
 
286
    public void stop() {
287
        updateThread = null;
288
    }
289
 
290
    public void run() {
291
 
292
        long lastCurrentTimeMillis = System.currentTimeMillis() - 250;
293
        long currentTimeMillis;
294
 
295
        ha = 0;
296
        ma = 0.0002F;
297
 
298
        while (Thread.currentThread() == updateThread) {
299
 
300
            repaint();
301
            currentTimeMillis = System.currentTimeMillis();
302
            if (currentTimeMillis - lastCurrentTimeMillis > 500) {
303
                ka = ha;
304
                lastCurrentTimeMillis = currentTimeMillis;
305
                ha = 0;
306
 
307
                ma = ma * 0.35F + 0.65F * (50F / (ka * 2));
308
 
309
                if (ma < 2E-005F)
310
                    ma = 2E-005F;
311
                if (ma > 40F)
312
                    ma = 40F;
313
            }
314
            try {
315
                Thread.sleep(sleeptime);
316
            } catch (InterruptedException interruptedexception) {
317
            }
318
        }
319
    }
320
 
321
}