Applications Applets Standalone applications Java applets Peter Mozelius

  • Slides: 26
Download presentation
Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC 1

Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC 1

Running Java o Standalone applications Like traditional programs o BUT platform independent o n

Running Java o Standalone applications Like traditional programs o BUT platform independent o n n n javac My. Class. java My. Class http: //java. sun. com/developer/online. Train ing/Programming/Basic. Java 1/compile. html 2

Running Java on the Internet o o Applications could be packed as jararchives and

Running Java on the Internet o o Applications could be packed as jararchives and executed by JNLP Run on a server 3

Running Java on the Internet o Two other alternatives n Servlets o n Applets

Running Java on the Internet o Two other alternatives n Servlets o n Applets o q Running on the server side Running in the web browser And Java Applets is the main theme for this course !! 4

Inheritance for Applets o An Object extended to o A Container extended to o

Inheritance for Applets o An Object extended to o A Container extended to o A Panel that is extended to An Applet which I can extend to to My. Applet o o 5

Application Applet How to make an applet out of an application ? THE APPLICATION:

Application Applet How to make an applet out of an application ? THE APPLICATION: import java. awt. Border. Layout; import java. awt. event. *; import javax. swing. *; public class My. Application extends JFrame implements Action. Listener { 6

The Application private JButton north. Button; private JLabel south. Label; /** * The constructor

The Application private JButton north. Button; private JLabel south. Label; /** * The constructor that creates the GUI */ public My. Application(){ //set the title and the size super("Lektion 24"); set. Size(300, 150); 7

The Application //create some Swing components north. Button = new JButton("PUSH ME for a

The Application //create some Swing components north. Button = new JButton("PUSH ME for a greeting"); south. Label = new JLabel("Here goes the greeting!", JLabel. CENTER); //connect the button with a listener north. Button. add. Action. Listener(this); 8

The Application //lay out the components add(north. Button, Border. Layout. NORTH); add(south. Label, Border.

The Application //lay out the components add(north. Button, Border. Layout. NORTH); add(south. Label, Border. Layout. SOUTH); //make the window visible and closable set. Visible(true); set. Default. Close. Operation(EXIT_ON_CLOSE); }//constructor 9

The Application /** * The implementation of the method from the * interface Action.

The Application /** * The implementation of the method from the * interface Action. Listener. The code that’s * going to be executed when the user clicks * on the button */ public void action. Performed(Action. Event e) { south. Label. set. Text("Hello Colombo!"); }//action. Performed public static void main(String[] args) { new My. Application(); } 10

Workshop Pause 11

Workshop Pause 11

The Applet run by Applet. Viewer 12

The Applet run by Applet. Viewer 12

The Applet Code part 1 o Even simpler than the application import java. awt.

The Applet Code part 1 o Even simpler than the application import java. awt. Border. Layout; java. awt. event. *; javax. swing. *; java. applet. *; public class My. Applet extends JApplet implements Action. Listener { 13

The Applet Code part 2 private JButton north. Button; private JLabel south. Label; /**

The Applet Code part 2 private JButton north. Button; private JLabel south. Label; /** * A method that initiates the applet and * creates the GUI */ public void init(){ //set the size of the window set. Size(300, 150); 14

The Applet Code part 3 north. Button = new JButton("PUSH ME for a greeting");

The Applet Code part 3 north. Button = new JButton("PUSH ME for a greeting"); south. Label = new JLabel("Here goes the greeting!", JLabel. CENTER); //connect the button with a listener north. Button. add. Action. Listener(this); add(north. Button, Border. Layout. NORTH); add(south. Label, Border. Layout. SOUTH); }//constructor 15

The Applet Code part 4 /** * The implementation of the method from *

The Applet Code part 4 /** * The implementation of the method from * Action. Listener. The code that is * going to be executed when the user * clicks on the button */ public void action. Performed(Action. Event e) { south. Label. set. Text("Hello Colombo!"); }//action. Performed }//My. Applet 16

The (oversimplified) HTML file <html> <applet code="My. Applet. class" width="300" height="200"> Problems with the

The (oversimplified) HTML file <html> <applet code="My. Applet. class" width="300" height="200"> Problems with the applet </applet> </html> 17

Workshop Pause 18

Workshop Pause 18

Another Simple Applet A simple but o illustrative o Applet o Run with the

Another Simple Applet A simple but o illustrative o Applet o Run with the Applet. Viewer o 19

The Simple Applet Code 1 import java. applet. Applet; import java. awt. Graphics; import

The Simple Applet Code 1 import java. applet. Applet; import java. awt. Graphics; import java. awt. Color; public class Simple. Applet extends Applet{ private String text; 20

The Simple Applet Code 2 public void init() { text = "I'm a simple

The Simple Applet Code 2 public void init() { text = "I'm a simple applet"; set. Background(Color. cyan); } public void start() { System. out. println("starting. . . "); } 21

The Simple Applet Code 3 public void stop() { System. out. println("stopping. . .

The Simple Applet Code 3 public void stop() { System. out. println("stopping. . . "); } public void destroy() { System. out. println("preparing to unload. . . "); } 22

The Simple Applet Code 4 public void paint(Graphics g){ g. set. Color(Color. blue); g.

The Simple Applet Code 4 public void paint(Graphics g){ g. set. Color(Color. blue); g. draw. Rect(0, 0, get. Size(). width -1, get. Size(). height -1); g. set. Color(Color. red); g. draw. String(text, 15, 25); }//paint }//Simple. Applet 23

The Simple Applet Code 5 o Further info about this applet can be found

The Simple Applet Code 5 o Further info about this applet can be found on: http: //java. sun. com/developer/online. Tr aining/Programming/Basic. Java 1/applet. html#struct o In Swing applets you replace paint() with the newer paint. Component() 24

The Appletviewer o How to run applets outside a web browser 25

The Appletviewer o How to run applets outside a web browser 25

The Appletviewer o o o The final result in a web browser A web

The Appletviewer o o o 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! 26