/* * HPApplet.java * * Copyright (C) 2005-2008 Huseyin Boyaci. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later * version. This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU General Public License version 2 for more details * (a copy is included in the LICENSE file that accompanied this code) * (also available at http://www.gnu.org) You should have received a copy of * the GNU General Public License along with this program; if not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * */ // The following lines make it possible to run this applet with // appletviewer appplication simply by firing the command: // // appletviewer HPApplet.java // // // import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JPanel; import psychWithJava.NormalWindow; /** * displays the text "Hello Psychophysicist (Applet)" * and two images in a Java Applet */ // First we need a JApplet to place our Hello Psychophysicist animation public class HPApplet extends JApplet { private AnimationPanel animationPanel; private JButton playButton; public void init() { // create the animation animationPanel = new AnimationPanel(); // it makes much more sense to use passive rendering in an applet animationPanel.setPassiveRendering(true); // initialize the animation animationPanel.initAnimation(); // place it at the center of the Applet add(animationPanel, "Center"); // Just for fun... // let's create a button to play the whole thing again - cause it is so exciting playButton = new JButton("Play Again"); playButton.setToolTipText("Cause it's so much fun!"); // does this show in browsers? // but the button should be disabled during the animation playButton.setEnabled(false); // add this button to the center of a JPanel JPanel buttonPanel = new JPanel(); buttonPanel.add(playButton, "Center"); // add this JPanel to the buttom of our Applet window add(buttonPanel, "South"); // set the size of the Applet window setSize(750, 612); // what happens when the Play Again button is pressed? playButton.addActionListener( // create an anonymous inner class on the fly new ActionListener() { public void actionPerformed(ActionEvent e) { // simply re-start the animation animationPanel.startAnimation(); // and disable the Play Again button playButton.setEnabled(false); } } ); // Note: the above way of doing things // is known as the "anonymous inner class technique" of // event handling in Java. There are various other ways to the // same thing. But this is the most compact and the easiest way // for our example here. } // From JDK API documentation: // Called by the browser or applet viewer to inform this applet that // it should start its execution. It is called after the init method // and each time the applet is revisited in a Web page. public void start() { animationPanel.startAnimation(); } // From JDJ API documentation: // Called by the browser or applet viewer to inform this applet that // it should stop its execution. It is called when the Web page that // contains this applet has been replaced by another page, and also // just before the applet is to be destroyed. public void stop() { animationPanel.stopAnimation(); playButton.setEnabled(false); } // Here is our animation as an inner class of the Applet // this makes it easier for us to communicate with the // Play Again button class AnimationPanel extends NormalWindow implements Runnable { private Thread animator; private boolean running = false; // we should pay a bit attention at how to initiate and to start/stop // the animation. The following three methods take care of that. // the rest is nearly identical to the other Hello Psychophysicist // examples public void initAnimation() { // the first time we are invoked, lets make sure that we have a thread if (animator == null || !animator.isAlive()) animator = new Thread(this); } public void startAnimation() { if (!animator.isAlive()) animator = new Thread(this); if (!running) { running = true; animator.start(); } } public void stopAnimation(){ running = false; } public void run() { try { blankScreen(); displayText("Hello Psychophysicist (Applet)"); updateScreen(); Thread.sleep(2000); blankScreen(); hideCursor(); BufferedImage bi1 = ImageIO.read(HPApplet.class .getResource("psychophysik.png")); displayImage(bi1); updateScreen(); Thread.sleep(2000); blankScreen(); BufferedImage bi2 = ImageIO.read(HPApplet.class .getResource("fechner.png")); displayImage(bi2); updateScreen(); // let's skip the next pause and enable the Play Again button // as soon as the last image is up //Thread.sleep(2000); } catch (IOException e) { System.err.println("File not found"); e.printStackTrace(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { // no closeScreen() method! It isn't very meaningful to // have that in an Applet running = false; // we are done playing the animation playButton.setEnabled(true); // enable the Play Again button } } } }