Combining Java And JavaScript-Programming for Web and Networks-Lecture Slides, Slides of Web Programming and Technologies

This lecture was delivered by Prof. Arun Ullal at Ankit Institute of Technology and Science for Web Programming course. It includes: Combining, Java, Javascript, Calling, Routines, Controlling, Applet, HTML, Elements, Archives

Typology: Slides

2011/2012

Uploaded on 07/18/2012

palvani
palvani šŸ‡®šŸ‡³

4.5

(2)

83 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 519: Web Programming
Autumn 2011
Combining Java & JavaScript

integrating Java with JavaScript

integrating Java with JavaScript
ī˜‚calling Java routines from JavaScript
ī˜‚controlling an applet from JavaScript
ī˜‚accessing JavaScript & HTML elements from an applet
related topics
ī˜‚Java archives (JARs), JavaBeans
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Combining Java And JavaScript-Programming for Web and Networks-Lecture Slides and more Slides Web Programming and Technologies in PDF only on Docsity!

COMP 519: Web Programming

Autumn 2011

Combining Java & JavaScript

^ integrating Java with JavaScript^ integrating Java with JavaScript

^ calling Java routines from JavaScript ^ controlling an applet from JavaScript ^ accessing JavaScript & HTML elements from an applet

^ related topics

^ Java archives (JARs), JavaBeans

•^ recall: JavaScript is very good for simple tasks, simple GUI layout can be handledby CSS

^ flexible data typing, primitive object types are fine for quick development ^ integration with HTML makes layout & control of GUI elements via DOM manipulation possible ^ not much library support, only primitive data structuring capabilities ^ not well-suited to multi-file projects and complex OO approach

JavaScript vs. Java

•^ Java is better at complex tasks, especially graphics

^ full-featured, more robust, extensive libraries of classes/routines ^ can support large projects, interacting objects ^ GUI layout in a webpage is difficult, integration with HTML not obvious

•^ IDEALLY: we can make use of the the strengths of each language

^ include applets in a page when needed (e.g., graphics) ^ allow communication between applet & JavaScript code

Calling Applet Methods

•^

more commonly, we want to include a more complicated applet in a page toperform some task, and control the applet via HTML events & JavaScript

•^

to call a Java applet method from JavaScript^ document.

appletName

. methodCall(…)

document.

appletName

. methodCall(…)

•^

what follows is a simple ā€œHello Worldā€ applet

• Java source code

import

java.awt.*; import

java.applet.; /**^

This

class

displays

"Hello

world!"

on

the

applet

window.

*/public

class

HelloWorld

extends

Applet

{

public

void

paint(Graphics

g)

ā€œHello Worldā€ Java applet

public

void

paint(Graphics

g)

{

//^

writes

starting

at

pixel

row

15,

col

15

g.drawString("Hello

world!",

15,

15);

} }

• compile this source code as normal into a Java class

• As one might expect, this may not really work on all browsers, due to the waydifferent browsers have implemented the element• There are pure HTML methods you can use that (are supposed to) work fordifferent browsers, or you can use JavaScript to detect different browsers andwrite appropriate HTML code depending upon the browser.

Browser differences...

•The previous example actually contains more code than was shown, as I usedsome JavaScript to detect the type of browser, and use (I hope) suitable HTMLcode that would work to load the applet.

• One could replace the element in the previous HTML code withthe following JavaScript code:^

• Suppose we return to the example on permutations...

^ We had a JavaScript method to perform this task. ^ Let’s replace that method with a Java applet ^ We can reuse much of the HTML code and need to supply Java code toperform the operation for us. ^ We continue using JavaScript methods to access the input (and output) fields

Permutations (again)

^ We continue using JavaScript methods to access the input (and output) fields^ on the HTML page, only using Java for the actual ā€œcalculationā€.

import

java.awt.*; import

java.applet.*; import

java.util.Random; import

java.lang.Math.; /**^ This

class

can^

be^ used

to^ generate

a^ permutation

to^ be

*^ inserted

into

a^ webpage.

*/public

class

JavaPermutation

extends

Applet

{ public

String

permutation(int

n)

{ Random

r^ =^

new^ Random(); int[]

p^ =^

new^ int[n]; int^ i,

k,^ temp;

Permutations (cont.)

int^ i,

k,^ temp; for^ (i

=^ 0;

i^ <^

n^ ;^ i++) p[i]^

=^ i+1; double

finish

=^ Math.pow(n,3)

*^ Math.log(n)

*^ 12;

for^ (i

=^ 1;

i^ <=

finish;

i++)

{^ if

(r.nextDouble()

<^ 0.5)

{ k

=^ r.nextInt(p.length

-^ 1);

temp^

=^ p[k]; p[k]^

=^ p[k

+^ 1]; p[k+1]

=^ temp; } }

Generating

random

permutations

You^ must

use^

a^ Java-enabled

browser

to^ view

this

applet.

Here

is^ a

function

that

will

generate

random

permutations

of^ a

set^

of^ integers,


where

a^ permutation

is^ just

an^ arrangement

of^ the

integers

{1,^

2,^ ...,

n}.

Enter

a^ positive

integer

n^ (1-50):


Random

permutation

of^ {1,

...,

n}.^

view page

MontePI

import

java.awt.*; import

java.applet.*; import

java.util.Random; public

class

Monte

extends Applet

private

static

Random

randy;

private

int SIZE; private

Image

offScreenImage;

private

Graphics

offScreenGraphics;

private

int randomInRange(int

low,

int

high)

private

double

distance(int

x1,

int

y1,

int x2,

int

y2)

public

void

init()

{

randy

=^ new Random(); Dimension

dim

=^ getSize();

SIZE =

dim.width; drawCircle();

init

creates the

random numbergenerator & getsapplet size drawDots

draws

the dots on the

drawCircle(); } public

void

drawCircle()

//^ DRAWS

CIRCLE

ON^

BOTH getGraphics()

AND

//^ offScreenGraphics } public

void

drawDots(int

numPoints)

//^ DRAWS

numPoints

RANDOM DOTS

ON^

BOTH

getGraphics()

//^ AND

offScreenGraphics } public

void

paint(Graphics

g)

g.drawImage(offScreenImage, 0,

0, null);

the dots on the screen and to theoff-screen buffer paint

redraws the

screen using thebuffer

MontePI (cont.)

Dividing Control

•^

where the control lies affects the efficiency/usability of an applet ^

want the applet to be as self-contained as possible,take advantage of speed of Java, more advanced features

^

but if GUI controls are in HTML, then JavaScript needs overall control

•^

consider adding counters for number of dots inside & outside circle ^

have the applet keep track of the dots in instance variables

^

have the applet keep track of the dots in instance variables

1.^

call method to draw all dots, then JavaScript accesses counts & display ^

fast, but only see counts when done

2.^

could return more control to the page, applet draws one dot at a time ^

repetition is handled by JavaScript, can update boxes after each dot

^

slower, but more flexible (and can see counts change)

3.^

could have applet update the HTML text boxes itself ^

tricky, ties the applet to the page

JavaScript in Control

import

java.awt.*; import

java.applet.*; import

java.util.Random; public

class

Monte

extends Applet

{^

..^

public

int

numInside,

numOutside;

public

void

drawCircle()

{^

numInside

=^ 0;

numOutside

=^ 0;

.^..

} public

void

drawDots(int

numDots)

{^

.^..

have applet keep track ofnumber inside & out^ • instance variables

numInside

and

numOutside

are

initialized in

.^.. for (int

i^ = 0;

i^ <

numPoints;

i++) {

int^

x^ = randomInRange(0,

SIZE);

int^

y^ = randomInRange(0,

SIZE);

if^ (distance(x,

y,^

SIZE/2,

SIZE/2)

<^ SIZE/2)

offScreenGraphics.setColor(Color.white);g.setColor(Color.white); numInside++; } else^ { offScreenGraphics.setColor(Color.black);g.setColor(Color.black); numOutside++; } }.^.. }..^

initialized in drawCircle

, updated

in^ drawDots

since they are public,these instance variablescan be accessed in thepage