// This is a template with examples of several Applet widgets // and processes. Just edit in your specific needs and remove // anything you don't need (see, in particular, the Threaded stuff // which will cause paint() to be called over and over... // import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.util.*; import java.lang.*; // use the old event model if you want to be compatible with Java 1.1.5 public class Sean2 extends Applet implements Runnable, ActionListener, ItemListener, AdjustmentListener, KeyListener, MouseListener, MouseMotionListener { /* Button button; Checkbox checkbox; Label label; Choice choice; Scrollbar scrollbar; Image image; String param; */ Choice timeConstChoice, tempRangeChoice, stepChoice; Choice timeConstChoice2; Button playButt, resetButt; Image backImage, buffer; Graphics gb; boolean isPlaying = false; boolean clearGraph = true; final int t1y = 180; final int t2y = 110; final int t2x2y = 40; final int x0 = 90; int x = 0; int t_start = 20; int time = 0; boolean goUp = true; double Ti = 5.; double Tf = 20.; double Tv = 0.; double Tvalue = 0.; double tau = 100.; public void init() { // defining widgets timeConstChoice = new Choice(); timeConstChoice.add("Time Constant \u03c4(s)"); timeConstChoice.add(" 1 s"); timeConstChoice.add(" 10 s"); timeConstChoice.add("100 s"); timeConstChoice.addItemListener(this); timeConstChoice2 = new Choice(); timeConstChoice2.add("Time Constant \u03c4(s)"); timeConstChoice2.add(" 1 s"); timeConstChoice2.add(" 10 s"); timeConstChoice2.add("100 s"); timeConstChoice2.addItemListener(this); timeConstChoice2.setEnabled(false); tempRangeChoice = new Choice(); tempRangeChoice.add("Temperature Range"); tempRangeChoice.add(" [ T1, T2 ]"); tempRangeChoice.add(" [ T1, 2 * T2 ]"); tempRangeChoice.addItemListener(this); stepChoice = new Choice(); stepChoice.add("Step up or down"); stepChoice.add(" Up "); stepChoice.add("Down"); stepChoice.addItemListener(this); playButt = new Button(" Play "); playButt.addActionListener(this); playButt.setEnabled(false); resetButt = new Button(" Reset "); resetButt.addActionListener(this); resetButt.setEnabled(false); backImage = getImage(getDocumentBase(),"backgnd.gif"); setLayout(null); Panel p1 = new Panel(); p1.setLayout(new FlowLayout(FlowLayout.CENTER,30,5)); p1.add(playButt); p1.add(resetButt); //p1.add(timeConstChoice2); Panel p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.CENTER,20,5)); p2.add(timeConstChoice); p2.add(tempRangeChoice); p2.add(stepChoice); Panel p3 = new Panel(); p3.setLayout(new BorderLayout(20,5)); p3.add("North",p2); p3.add("South",p1); p3.setBounds(0,265,548,60); add("South",p3); validate(); repaint(); /* // Generic code from the template follows for example only... button = new Button("Button"); button.addActionListener(this); checkbox = new Checkbox("checkbox"); checkbox.addItemListener(this); choice = new Choice(); choice.add("one"); choice.add("two"); choice.addItemListener(this); // orientation, initial value, size of bubble, minimum, maximum scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110); scrollbar.addAdjustmentListener(this); label = new Label("The Label",Label.CENTER); // parsing parameters param = getParameter("parameter"); if (param != null) { StringTokenizer st = new StringTokenizer(param, ","); int n = st.countTokens(); for (int i=0; i 400) isPlaying = false; if (time <= t_start) { gb.setColor(Color.green); gb.drawLine(time+x0-1,(int)Math.round(Ti),time+x0,(int)Math.round(Tv)); gb.setColor(Color.red); Tvalue = Ti; gb.drawLine(time+x0-1,(int)Math.round(Tvalue),time+x0,(int)Math.round(Tvalue)); } else { gb.setColor(Color.green); gb.drawLine(time+x0-1,(int)Math.round(Tv),time+x0,(int)Math.round(Tf)); gb.setColor(Color.red); double Tnew = Tf + (Ti - Tf)*Math.exp(- (time-t_start)/tau); gb.drawLine(time+x0-1,(int)Math.round(Tvalue),time+x0,(int)Math.round(Tnew)); Tvalue = Tnew; Tv = Tf; } } g.drawImage(buffer,0,0,this); } // Listeners public void actionPerformed(ActionEvent e) { if (e.getSource() == playButt) { clearGraph = false; time = 0; int k = stepChoice.getSelectedIndex(); if (k == 1) goUp = true; if (k == 2) goUp = false; k = timeConstChoice.getSelectedIndex(); tau = 1.0; if (k == 2) tau = 10.0; if (k == 3) tau = 100.; playButt.setEnabled(true); k = tempRangeChoice.getSelectedIndex(); if (k == 1) { if (goUp) { Ti = t1y; Tf = t2y; } else { Ti = t2y; Tf = t1y; } } else if (k == 2) { if (goUp) { Ti = t1y; Tf = t2x2y; } else { Ti = t2x2y; Tf = t1y; } } Tv = Ti; Tvalue = Tv; isPlaying = true; repaint(); playButt.setEnabled(false); resetButt.setEnabled(true); } else if (e.getSource() == resetButt) { playButt.setEnabled(true); isPlaying = false; clearGraph = true; repaint(); } /* //Generic code from the template follows for example only System.out.println("#### ActionEvent for "+e.getSource()); if (e.getSource() == button) { System.out.println("#### The button was pressed"); } */ } public void itemStateChanged(ItemEvent e) { System.out.println("#### "+timeConstChoice.getSelectedIndex()); if (timeConstChoice.getSelectedIndex() < 1 || tempRangeChoice.getSelectedIndex() < 1 || stepChoice.getSelectedIndex() < 1) { playButt.setEnabled(false); } else { playButt.setEnabled(true); } /* //Generic code from the template follows for example only System.out.println("#### ItemEvent for "+e.getSource()); if (e.getSource() == checkbox) { System.out.println("#### Checkbox set to "+checkbox.getState()); } if (e.getSource() == choice) { System.out.println("#### Choice selected "+choice.getSelectedItem()); } */ } public void adjustmentValueChanged(AdjustmentEvent e) { /* //Generic code from the template follows for example only System.out.println("#### AdjustmentEvent for "+e.getSource()); if (e.getSource() == scrollbar) { System.out.println("#### Scrollbar value = "+scrollbar.getValue()); } */ } // for MouseListener public void mouseClicked(MouseEvent e) { int mx = e.getX(); int my = e.getY(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} //for MouseMotionListener public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} //for KeyListener public void keyPressed(KeyEvent e) { int k = e.getKeyCode(); char ck = e.getKeyChar(); } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} // next, handle running a Thread to automatically repaint // the display every "refreshRate" milliseconds // NOTE -- This will cause paint() to be done repeatedly!! private Thread animator_thread = null; private boolean isRunning; int refreshRate = 100; public void start() { if (animator_thread == null) { isRunning = true; animator_thread = new Thread(this); animator_thread.start(); } } public void stop() { if ((animator_thread != null) && animator_thread.isAlive()) { isRunning = false; animator_thread.stop(); } animator_thread = null; } public void run () { while (isRunning) { try { Thread.sleep(refreshRate); repaint(); } catch (Exception re) { System.out.println("#### "+re); } } } // Utilities for getting images and audio clips from JAR or URL public Image loadImage(String name) { // first try the URL Image img = null; URL u = getClass().getResource( (String) name); if (u == null) { u = getDocumentBase(); img = getImage(u,name); } else { img = getImage(u); } MediaTracker mt = new MediaTracker(this); mt.addImage(img, 0); try { mt.waitForAll(); } catch ( Exception ex ) {img = null; } return img; } public AudioClip loadAudioClip(String name) { // first try the URL AudioClip auc = null; URL u = getClass().getResource( (String) name); if (u == null) { u = getDocumentBase(); auc = getAudioClip(u,name); } else { auc = getAudioClip(u); } return auc; } }