/* Trivial applet that displays a string - 4/96 PNL */ import java.awt.*; import java.applet.Applet; import java.awt.Graphics; import java.applet.AudioClip; public class TrivialApplet extends java.applet.Applet implements Runnable { int offsetSignal; int offsetNeurons; int nbNeurones = 6; int step = 2; int coordXcenter[]; int coordYcenter[]; int coordYcenterFix[]; int latency[]; boolean cursor[]; int offsetFunction = 0; AudioClip clip; int insetH = 35; // parametres de l'applet Thread runner; Image offscreenImage; Graphics offscreenGraphics; // le bouton pour stopper Button b; boolean stop = true; double f(double x) { return (Math.cos((x+offsetFunction)/10) + Math.sin((x+offsetFunction)/14) + 2) * (size().height-insetH) / 16; } public void init() { b = new Button("stop it!"); add(b); clip = getAudioClip(getCodeBase(), "chirp1.au"); if (clip == null) { System.out.println("no sound"); System.exit(0); } computeCoord(); offscreenImage = createImage(size().width, size().height); offscreenGraphics = offscreenImage.getGraphics(); resetGraph(); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { if (stop) { b.setLabel("run"); stop(); } else { b.setLabel("stop it!"); start(); } stop = !stop; } return true; } public void resetGraph() { offscreenGraphics.setColor(Color.white); offscreenGraphics.fillRect(0, 0, size().width, size().height); offscreenGraphics.setColor(Color.black); for (int x = 0 ; x < size().width ; x++) offscreenGraphics.drawLine(x, (int)f(x) + offsetSignal, x + 1, (int)f(x + 1) + offsetSignal); for (int i = 0 ; i < nbNeurones; i++) { offscreenGraphics.fillOval( size().width / nbNeurones / 4+i* size().width / nbNeurones, offsetNeurons, size().width / nbNeurones / 2, size().width / nbNeurones / 2); offscreenGraphics.drawLine( coordXcenter[i], coordYcenterFix[i], coordXcenter[i], insetH ); } } void computeCoord() { offsetSignal = 3 * (size().height-insetH) / 4 + insetH; offsetNeurons = 3 * (size().height-insetH) / 5 + insetH; coordXcenter = new int[nbNeurones]; coordYcenter = new int[nbNeurones]; coordYcenterFix = new int[nbNeurones]; cursor = new boolean[nbNeurones]; for (int i = 0 ; i < nbNeurones; i++) { coordXcenter[i] = size().width / nbNeurones / 4+i* size().width / nbNeurones + size().width / nbNeurones / 4; coordYcenter[i] = offsetNeurons - 1; coordYcenterFix[i] = coordYcenter[i] + size().width / nbNeurones / 4; cursor[i] = false; } /* for (int i = 0 ; i < nbNeurones; i++) { System.out.println((int)f(coordXcenter[i])); }*/ } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void update(Graphics g) { paint(g); } public void destroy() { offscreenGraphics.dispose(); } public void run() { while (true) { boolean allSpike = false; int counter = 0; while(!allSpike) { allSpike = true; try { counter++; for (int i=0; i< nbNeurones; i++) if (coordYcenter[i] > insetH) if (((int)f(coordXcenter[i]) < counter) && !cursor[i]) cursor[i] = true; else if (cursor[i]) { coordYcenter[i]-=step; allSpike = false; } else allSpike = false; else cursor[i] = false; repaint(); Thread.sleep(100); } catch (InterruptedException e) {} } // reinitialise for (int i=0; i< nbNeurones; i++) coordYcenter[i] = offsetNeurons - 1; offsetFunction += size().width; resetGraph(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void paint(Graphics g) { for (int i = 0 ; i < nbNeurones; i++) { // efface les lignes offscreenGraphics.setColor(Color.white); if ((cursor[i]) && ( coordYcenter[i] != offsetNeurons -1)) offscreenGraphics.drawLine( coordXcenter[i]-1, coordYcenter[i]+step, coordXcenter[i]- size().width / nbNeurones / 4, coordYcenter[i]+step); // dessine les neurones if ((cursor[i]) && ( coordYcenter[i] == offsetNeurons -1)) { if (clip != null) clip.play(); offscreenGraphics.setColor(Color.red); } else offscreenGraphics.setColor(Color.black); offscreenGraphics.fillOval( size().width / nbNeurones / 4+i* size().width / nbNeurones, offsetNeurons, size().width / nbNeurones / 2, size().width / nbNeurones / 2); // dessine les decharges offscreenGraphics.setColor(Color.black); if (cursor[i]) offscreenGraphics.drawLine( coordXcenter[i]-1, coordYcenter[i], coordXcenter[i]- size().width / nbNeurones / 4, coordYcenter[i]); } g.drawImage(offscreenImage, 0, 0, this); } } /* class TickerArea extends TrivialApplet { String tickertext; Color tickercolor; Font tickerfont; int speed; // In pixels per second for scrolling int tickerx; int tickery; int tickerlen; public void handleArg(String s) { tickertext = new String("Simon Thorpe's Modeling Team "); tickercolor = Color.black; speed = 100; String fontname = "TimesRoman"; FontMetrics fm; int size; int nextsize = H; do { size = nextsize; tickerfont = new Font(fontname, Font.PLAIN, size); fm = parent.getFontMetrics(tickerfont); nextsize = (size * 9) / 10; } while (fm.getHeight() > H && size > 0); tickerlen = fm.stringWidth(tickertext); tickery = fm.getAscent(); } public void getMedia() { tickerx = 0; repaint(); lasttick = System.currentTimeMillis(); } public boolean animate() { long curtick = System.currentTimeMillis(); tickerx -= ((speed * (curtick - lasttick)) / 1000); if (tickerx > W || tickerx + tickerlen < 0) { tickerx = W; } repaint(); lasttick = curtick; return true; } public void highlight(Graphics g) { g.setColor(tickercolor); g.setFont(tickerfont); g.drawString(tickertext, X+tickerx, Y+tickery); } } */ /* public void handleArg(String s) { StringTokenizer st = new StringTokenizer(s, ","); tickertext = st.nextToken(); tickercolor = Color.black; speed = 100; String fontname = "TimesRoman"; if (st.hasMoreTokens()) { fontname = st.nextToken(); if (st.hasMoreTokens()) { String str = st.nextToken(); if (str.startsWith("#")) { str = str.substring(1); } try { int colorval = Integer.parseInt(str, 16); tickercolor = new Color((colorval >> 16) & 0xff, (colorval >> 8) & 0xff, (colorval >> 0) & 0xff); } catch (Exception e) { tickercolor = Color.black; } if (st.hasMoreTokens()) { str = st.nextToken(); try { speed = Integer.parseInt(str); } catch (Exception e) { speed = 100; } } } } FontMetrics fm; int size; int nextsize = H; do { size = nextsize; tickerfont = new Font(fontname, Font.PLAIN, size); fm = parent.getFontMetrics(tickerfont); nextsize = (size * 9) / 10; } while (fm.getHeight() > H && size > 0); tickerlen = fm.stringWidth(tickertext); tickery = fm.getAscent(); } */