260 likes | 276 Views
Learn how to create Java applets from applications, explore inheritance concepts, and run applets locally and on the internet.
E N D
Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC
Running Java • Standalone applications • Like traditional programs • BUT platform independent • javac MyClass.java • java MyClass • http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/compile.html
Running Java on the Internet • Applications could be packed as jar-archives and executed by JNLP • Run on a server
Running Java on the Internet • Two other alternatives • Servlets • Running on the server side • Applets • Running in the web browser • And Java Applets is the main theme for this course !!
Inheritance for Applets • An Object extended to • A Container extended to • A Panel that is extended to • An Applet which I can extend to • to MyApplet
Application Applet How to make an applet out of an application ? THE APPLICATION: import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; public class MyApplication extends JFrame implements ActionListener {
The Application private JButton northButton; private JLabel southLabel; /** * The constructor that creates the GUI */ public MyApplication(){ //set the title and the size super("Lektion24"); setSize(300, 150);
The Application //create some Swing components northButton = new JButton("PUSH ME for a greeting"); southLabel = new JLabel("Here goes the greeting!", JLabel.CENTER); //connect the button with a listener northButton.addActionListener(this);
The Application //lay out the components add(northButton, BorderLayout.NORTH); add(southLabel, BorderLayout.SOUTH); //make the window visible and closable setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); }//constructor
The Application /** * The implementation of the method from the * interface ActionListener. The code that’s * going to be executed when the user clicks * on the button */ public void actionPerformed(ActionEvent e) { southLabel.setText("Hello Colombo!"); }//actionPerformed public static void main(String[] args) { new MyApplication(); }
The Applet Code part 1 • Even simpler than the application import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; import java.applet.*; public class MyApplet extends JApplet implements ActionListener {
The Applet Code part 2 private JButton northButton; private JLabel southLabel; /** * A method that initiates the applet and * creates the GUI */ public void init(){ //set the size of the window setSize(300, 150);
The Applet Code part 3 northButton = new JButton("PUSH ME for a greeting"); southLabel = new JLabel("Here goes the greeting!", JLabel.CENTER); //connect the button with a listener northButton.addActionListener(this); add(northButton, BorderLayout.NORTH); add(southLabel, BorderLayout.SOUTH); }//constructor
The Applet Code part 4 /** * The implementation of the method from * ActionListener. The code that is * going to be executed when the user * clicks on the button */ public void actionPerformed(ActionEvent e) { southLabel.setText("Hello Colombo!"); }//actionPerformed }//MyApplet
The (oversimplified) HTML file <html> <applet code="MyApplet.class" width="300" height="200"> Problems with the applet </applet> </html>
Another Simple Applet • A simple but • illustrative • Applet • Run with the AppletViewer
The Simple Applet Code 1 import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class SimpleApplet extends Applet{ private String text;
The Simple Applet Code 2 public void init() { text = "I'm a simple applet"; setBackground(Color.cyan); } public void start() { System.out.println("starting..."); }
The Simple Applet Code 3 public void stop() { System.out.println("stopping..."); } public void destroy() { System.out.println("preparing to unload..."); }
The Simple Applet Code 4 public void paint(Graphics g){ g.setColor(Color.blue); g.drawRect(0, 0, getSize().width -1, getSize().height -1); g.setColor(Color.red); g.drawString(text, 15, 25); }//paint }//SimpleApplet
The Simple Applet Code 5 • Further info about this applet can be found on: http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/applet.html#struct • In Swing applets you replace paint() with the newer paintComponent()
The Appletviewer • How to run applets outside a web browser
The Appletviewer • The final result in a web browser • A web browser works with a cache • Appletviewer during the development of the applet That all for now, thank you!