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

Post on 19-Jan-2016

257 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

3

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

archives and executed by JNLP Run on a server

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

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

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 {

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

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

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

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

11

Workshop Pause

12

The Applet run by AppletViewer

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 {

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

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

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

17

The (oversimplified) HTML file<html>

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

</html>

18

Workshop Pause

19

Another Simple Applet A simple but illustrative Applet

Run with the AppletViewer

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;

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

}

22

The Simple Applet Code 3 public void stop() {

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

public void destroy() {

System.out.println("preparing to

unload...");

}

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

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

25

The Appletviewer How to run applets outside a web browser

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!

top related