1 applications & applets standalone applications & java applets peter mozelius dsv/ucsc

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

Upload: dortha-oneal

Post on 19-Jan-2016

255 views

Category:

Documents


3 download

TRANSCRIPT

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

1

Applications & Applets

Standalone applications & Java applets

Peter Mozelius DSV/UCSC

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

2

Running Java Standalone applications Like traditional programs

BUT platform independent javac MyClass.java java MyClass http://java.sun.com/developer/onlineTra

ining/Programming/BasicJava1/compile.html

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

3

Running Java on the Internet Applications could be packed as jar-

archives and executed by JNLP Run on a server

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

4

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 !!

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

5

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

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

6

Application AppletHow 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 {

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

7

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);

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

8

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);

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

9

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

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

10

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();}

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

11

Workshop Pause

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

12

The Applet run by AppletViewer

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

13

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 {

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

14

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);

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

15

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

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

16

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

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

17

The (oversimplified) HTML file<html>

<applet code="MyApplet.class" width="300" height="200"> Problems with the applet </applet>

</html>

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

18

Workshop Pause

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

19

Another Simple Applet A simple but illustrative Applet

Run with the AppletViewer

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

20

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

import java.awt.Graphics;

import java.awt.Color;

public class SimpleApplet

extends Applet{

private String text;

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

21

The Simple Applet Code 2 public void init() { text = "I'm a simple applet"; setBackground(Color.cyan); }

public void start() { System.out.println("starting...");

}

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

22

The Simple Applet Code 3 public void stop() {

System.out.println("stopping..."); }

public void destroy() {

System.out.println("preparing to

unload...");

}

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

23

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

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

24

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()

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

25

The Appletviewer How to run applets outside a web browser

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

26

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!