KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > pencil > examples > simpleClient > PlasmaAnimation


1 /*
2  * Copyright 2004 Andreas Siebert (j2biz community)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7
8  * http://www.apache.org/licenses/LICENSE-2.0
9
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.j2biz.pencil.examples.simpleClient;
18
19 import java.awt.Canvas JavaDoc;
20 import java.awt.Color JavaDoc;
21 import java.awt.Frame JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24
25 import com.j2biz.log.LOG;
26
27 /**
28  * @author Andreas Siebert
29  *
30  * (c) 2004 by Andreas Siebert / j2biz.com
31  */

32 public class PlasmaAnimation extends Canvas JavaDoc {
33
34        
35        static {
36               String JavaDoc className = PlasmaAnimation.class.getName();
37               LOG.trace( "initialize plazma-class: ${className}" );
38        }
39        
40        private static final int MAXX = 199;
41
42        private static final int MAXY = 199;
43
44        /**
45         * die x0 koeffizienten ...
46         */

47        private static double C = 100;
48
49        private static double F = 67;
50
51        private static double I = 32;
52
53        private static double L = 8;
54
55        private double time1 = C;
56
57        private double time2 = F;
58  
59        private double time3 = I;
60
61        private double time4 = L;
62
63        private double angle3 = time3;
64
65        private double angle4 = time4;
66
67        private double angle1 = time1;
68
69        private double angle2 = time2;
70
71        Thread JavaDoc t;
72
73        private double[] cosTable = new double[256];
74
75        {
76               LOG.debug( "prepare color-table" );
77        
78               for (int i = 0; i < cosTable.length; i++) {
79                      double colorValue = 32 + 32 * Math.cos(i * 2 * Math.PI / 256);
80                      cosTable[i] = colorValue;
81                      LOG.trace( "index[${i}] == ${colorValue}" );
82               }
83        }
84
85        public void start() {
86               t = new Thread JavaDoc() {
87
88                      /**
89                       * @see java.lang.Thread#run()
90                       */

91                      public void run() {
92                             try {
93                                    while (true) {
94                                           PlasmaAnimation.this.repaint();
95                                           Thread.sleep(100);
96                                    }
97                             } catch (InterruptedException JavaDoc e) {
98                                    e.printStackTrace();
99                                    System.exit(-1);
100                             }
101                      }
102               };
103
104               t.start();
105        }
106
107        public static void main(String JavaDoc args[]) {
108               LOG.trace("");
109
110               int mod = 4 % 2;
111
112 // System.out.println(" modulo goes on: " + (128 & 127));
113

114               Frame JavaDoc frame = new Frame JavaDoc("plasme, baby!");
115               frame.setSize(MAXX + 1, MAXY + 1);
116
117               PlasmaAnimation anim = new PlasmaAnimation();
118               frame.add(anim);
119               frame.setVisible(true);
120
121               anim.start();
122        }
123        
124        BufferedImage JavaDoc img = new BufferedImage JavaDoc( MAXX+1, MAXY+1,BufferedImage.TYPE_INT_RGB ) ;
125
126        /**
127         *
128         */

129        private void paintImg(Graphics JavaDoc g) {
130               angle3 = time3;
131               angle4 = time4;
132
133               for (int y = 0; y <= MAXY; y++) {
134                      angle1 = time1;
135                      angle2 = time2;
136                      for (int x = 0; x <= MAXX; x++) {
137                             double color = this.cosTable[(int) Math.round(angle1) & 255] + this.cosTable[(int) Math.round(angle2) & 255]
138                                           + cosTable[(int) Math.round(angle3) & 255] + cosTable[(int) Math.round(angle4) & 255];
139                             
140                             g.setColor( new Color JavaDoc((int) color) );
141                             g.drawLine( x, y, x, y);
142                             angle1 += A;
143                             angle2 += D;
144                      }
145                      
146                      angle3 += G;
147                      angle4 += J;
148               }
149
150               time1 += B;
151               time2 += E;
152               time3 += H;
153               time4 += K;
154        }
155
156        /**
157         * @see java.awt.Canvas#paint(java.awt.Graphics)
158         */

159        public void paint(Graphics JavaDoc g) {
160 // paintImg( img.getGraphics() );
161
// g.drawImage(img, 0, 0, null);
162
paintImg( g );
163        }
164
165        private static double A = 12;
166        private static double D = 4;
167        private static double J = 46;
168        private static double B = 123;
169        private static double E = 67;
170        private static double H = 76;
171        private static double K = 12;
172        private static double G = 3;
173        
174 }
Popular Tags