/** Alternative Relativity: Doppler Effect Demonstrator by Robert John Morton UK-YE572246C (Belo Horizonte-MG 27 September 2006, 02 May 2012) */ // THE WAVES DISPLAY PANEL import javax.swing.*; //library of swing widgets import java.awt.*; //for graphics operations (GUI) class wpanel extends JPanel { private int X = 5, //initial position of source mp = 16, //size of the wave objects references array ds = 0, //default mode selection wi = 0, //wave instance index SH = 0, //image shift delay subcounter sh = -600; //image shift variable private boolean cf = true; //true means new mode selected private Image H; //off-screen image private Graphics h; //graphics reference variable //for references to instances of the wave class private wave MP[] = new wave[mp]; wpanel(Image H, Graphics h, int ds) { // CONSTRUCTOR this.H = H; this.h = h; this.ds = ds; /* Set the default mode for the applet (classical or alternative) and the quick calculation sine values for the wave class. */ wave.sineValues(); wave.setMode(ds); } public void atualizar() { // UPDATE THE DISPLAY if(cf) changeMode(); //if a new mode has been selected // mini loop to do only 4 waves per pass (CPU loading) for(int q = 0; q < 4; q++) { wave r = MP[wi]; //create a reference to this wave instance if (r != null) { //if this wave is still alive (not yet faded out) r.atualizar(h); //update this wave's position if(r.getmh()) //if it is time to launch the following wave /* then; if a new wave has not yet been launched, continue traversing the image; else if the wave has now faded out, kill its wave object. */ if(!r.getnl()) imageTraverse(r); else if(r.getah()) MP[wi] = null; } // If wave index has been incremented to its maximum, show image. if(++wi >= mp) showImage(); } //end of for-loop } private void changeMode() { cf = false; //cancel the mode change flag h.setColor(Color.black); //set graphics fields's background colour h.fillRect(0,0,1200,191); //wipe the graphics field's display area for(int i = 0; i < mp; i++) MP[i] = null; //kill all wave objects /* If applet is in "classical relativity" mode, set start position and shift the off-screen image; otherwise set the corresponding values for the "alternative relativity" mode. */ if(ds == 0) { X = 200; sh = -250; } else { X = 500; sh = -600; } MP[0] = new wave(X); //spawn a new wave object (generate a new one) wi = 0; //reinitialize the wave instance index } private void imageTraverse(wave r) { int k = wi; //create a temporary cyclic index variable for(int j = 0; j < mp; j++) { //for all possible wave objects // cycle round back to the start of the references array if (++k >= mp) k = 0; if (MP[k] == null) { //if no live wave exists in this element if(ds == 0) //if applet is in "classical relativity" mode... /* If the light-source has not yet reached its destination, shift it another 10 pixels to the right; otherwise, set it back to its extreme left position (off-screen) and trigger the start of a new traverse of the light source. */ if(X < 1195) X += 10; else { X = 250; cf = true; } /* Spawn a new wave object, signal that this has been done then break out of the search 'for' loop. */ MP[k] = new wave(X); r.setnl(); break; } } //end of 'for all possible wave objects' } private void showImage() { wi = 0; //reset wave index back to zero /* If the applet is operating in its "alternative relativity" mode AND the delay sub-counter has exceeded its upper limit, reset it back to zero ... */ if(ds == 1 && SH++ > 1) { SH = 0; /* then, if the image has shifted its maximum distance to the right, set it back to its extreme left position again and trigger the start of new traverse of the light source. */ if(sh++ > 95) { sh = -600; cf = true; } } } // draw graphics image onto the visible panel public void paint(Graphics g) { g.drawImage(H, sh, 0, null); } void setCF(boolean cf) {this.cf = cf;} void setDS(int ds) {this.ds = ds;} }