![]() |
CSCI A348/A548Lecture Notes 16 Fall 1999 |
We start with an .html
file to deliver the applet.
We put this in<html> <head><title>Feedback One</title></head> <body> <applet code=Feedback1.class width=400 height=400> </applet> </body> </html>
Feedback1.html
(version 1).
We then write the applet. This first applet is not really a feedback
applet, it's just a reminder on how the AWT
and event
handling works in Java 1.1. Again A202 students should find this
very familiar.
We then compile and check the result on the web.import java.applet.*; import java.awt.*; import java.awt.event.*; public class Feedback1 extends Applet implements ActionListener { private Button up, down, left, right; Circle circle = new Circle(100, 100, 30); public void init() { up = new Button("Up" ); down = new Button("Down" ); right = new Button("Right"); left = new Button("Left" ); add(up); up.addActionListener(this); add(down); down.addActionListener(this); add(right); right.addActionListener(this); add(left); left.addActionListener(this); } public void actionPerformed(ActionEvent e) { Button button = (Button)e.getSource(); if(button == up) circle.up(); else if (button == down) circle.down(); else if (button == right) circle.right(); else circle.left(); repaint(); } public void paint(Graphics g) { circle.draw(g); } } class Circle { int x, y, radius; Circle (int x, int y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void draw(Graphics g) { g.drawOval(x, y, 2*radius, 2*radius); } public void up () { y -= 3; } public void down () { y += 3; } public void right() { x += 3; } public void left () { x -= 3; } }
Now that we have this running let's make the change in the interface.
Then, of course, we needimport java.applet.*; import java.awt.*; import java.awt.event.*; public class Feedback2 extends Applet implements ActionListener { private Button submit = new Button("Send Comments"); private TextField nameField = new TextField("", 20); private TextArea commentsField = new TextArea(5, 45); public void init() { add(new Label("Name")); add(nameField); add(new Label("Comments")); add(commentsField); add(submit); submit.addActionListener(this); } public void actionPerformed(ActionEvent e) { Button button = (Button)e.getSource(); if (button == submit) { } } }
Feedback2.html
:
So now we can check the result online. The interface, though not too spiffy, resembles an HTML form. Let's change<html> <head><title>Feedback One</title></head> <body> <applet code=Feedback2.class width=400 height=400> </applet> </body> </html>
actionPerfomed
such
that when we push the button the text of the message is displayed
(while the interface used to collect the data goes away). You will
notice that so far you can type in the text field and the text area
and even push the button but nothing happens when you push the button.
And the result is here.public void actionPerformed(ActionEvent e) { Button button = (Button)e.getSource(); if (button == submit) { String data = nameField.getText() + "\n" + commentsField.getText() + "\n\n"; mailComments(data); goodBye(data); } } void mailComments(String data) { nameField.setEditable(false); commentsField.setEditable(false); submit.setVisible(false); } void goodBye(String data) { commentsField.setText( "Dear " + nameField.getText() + ":\n" + "\nThanks for your comments!\n\n---------------\n" + commentsField.getText() ); }
Now let's add the code that actually sends the user's comments to the perl script over the network.
The interface is unchanged, but the applet now sends the message to the perl script.import java.io.*; import java.net.*; import java.applet.*; import java.awt.*; import java.awt.event.*; public class Feedback4 extends Applet implements ActionListener { private Button submit = new Button("Send Comments"); private TextField nameField = new TextField("", 20); private TextArea commentsField = new TextArea(5, 45); public void init() { add(new Label("Name")); add(nameField); add(new Label("Comments")); add(commentsField); add(submit); submit.addActionListener(this); } public void actionPerformed(ActionEvent e) { Button button = (Button)e.getSource(); if (button == submit) { String data = nameField.getText() + "\n" + commentsField.getText() + "\n\n"; mailComments(data); goodBye(data); } } void mailComments(String data) { nameField.setEditable(false); commentsField.setEditable(false); submit.setVisible(false); try { Socket con = new Socket("tucotuco.cs.indiana.edu", 19904); PrintWriter out = new PrintWriter(con.getOutputStream(), true); out.println( "POST /cgi-bin/lecture15/mailto.pl HTTP/1.0\r\n" + "Content-type: application/octet-stream\r\n" + "Content-length: " + data.length() + "\r\n\r\n" + data ); out.flush(); System.out.println("Sent: (" + data.length() + ") " + data); } catch (Exception e) { System.out.println("E: " + e); } } void goodBye(String data) { commentsField.setText( "Dear " + nameField.getText() + ":\n" + "\nThanks for your comments!\n\n---------------\n" + commentsField.getText() ); } }
Now the last part shows you how you can make a snatdalone application from an applet (that remains an applet and can still be used as such).
We start by creating an AppletApplication
:
To provide a frame we createimport java.applet.*; class AppletApplication extends Applet { public void init() { } static void main(String[] args) { new AppletFrame(new AppletApplication(), 400, 400); } }
AppletFrame
:
We then use inheritance to create theimport java.io.*; import java.net.*; import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class AppletFrame extends Frame implements WindowListener, AppletStub, AppletContext { AppletFrame (Applet a, int x, int y) { setTitle(a.getClass().getName()); resize(x, y); add("Center", a); a.setStub(this); a.init(); show(); a.start(); addWindowListener(this); } public void windowClosing (WindowEvent e) { System.exit(0); } public void windowActivated (WindowEvent e) { } public void windowClosed (WindowEvent e) { } public void windowDeactivated (WindowEvent e) { } public void windowDeiconified (WindowEvent e) { } public void windowIconified (WindowEvent e) { } public void windowOpened (WindowEvent e) { } public boolean isActive () { return true; } public URL getDocumentBase () { return null; } public URL getCodeBase () { return null; } public String getParameter (String name) { return ""; } public void appletResize (int width, int height) { } public AppletContext getAppletContext () { return this; } public AudioClip getAudioClip (URL url) { return null; } public Image getImage (URL url) { return null; } public Applet getApplet (String name) { return null; } public Enumeration getApplets () { return null; } public void showDocument (URL url) { } public void showDocument (URL url, String target) { } public void showStatus (String status) { } }
FeedbackAppletApplication
:
You'll experiment with this during the next lab.public class FeedbackAppletApplication extends FeedbackApplet { public static void main(String[] args) { new AppletFrame(new FeedbackApplet(), 400, 400); } }