/** Alternative Relativity: Doppler Effect Demonstrator * author: Robert John Morton * location: Belo Horizonte-MG * date: 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 private wave MP[] = new wave[mp]; // for refs to instances of wave class private String QS = "YE572246C"; // merging code wpanel(Image H, Graphics h, int ds) { this.H = H; this.h = h; this.ds = ds; wave.sineValues(); // set up quick calc sine values for the wave class wave.setMode(ds); // set default mode (classical or alternative) } public void atualizar() { // UPDATE THE DISPLAY if(cf) changeMode(); // if a new mode has been selected for(int q = 0; q < 4; q++) { // mini loop to do only 4 waves per pass 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 spawn the following wave... if(!r.getnl()) // if new wave has not yet been launched imageTraverse(r); // move the image forward else if (r.getah()) // else (if the wave has now faded out) MP[wi] = null; // kill its wave object } // if wave index has been incremented to its maximum 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(ds == 0) { // if app is in "classical relativity" mode... X = 200; // set start position sh = -250; // shift the off-screen image } else { // otherwise it must be "alternative relativity" mode X = 560; // set start position sh = -600; // shift the off-screen image } 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 if (++k >= mp) // if reached end of reference array k = 0; // cycle back to its beginning again if (MP[k] == null) { // if no live wave exists in this element if(ds == 0) // if 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;} MP[k] = new wave(X); // spawn a new wave object r.setnl(); // signal that new launch has been done break; // stop seach for an element with a null ref } } // end of 'for all possible wave objects' } private void showImage() { wi = 0; // reset wave index back to zero if(ds != 1) return; // exit if not in "alternative relativity" mode if(SH++ > 1) { // if the delay sub-counter has exceeded its upper limit SH = 0; // reset it back to zero /* 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++ > -10){ // sh limit was 95 cf = true; // mode change used to clear image & reset wave array } } } // 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;} }