200 likes | 240 Views
Java Applets. Applets son programas java que se bajan y corren en el contexto de una página web. Request a page. Animator.class. Html. Animator.class. Java program running on the client. <applet code=Animator.class > </applet>. Cómo esplicar qué es realmente un applet ?.
E N D
Java Applets Applets son programas java que se bajan y corren en el contexto de una página web Request a page Animator.class Html Animator.class Java program running on the client <applet code=Animator.class > </applet>
Cómo esplicar qué es realmente un applet ? • Para personas expertas en Java es muy fácil: es una extensión de un Panel. Por lo tanto se puede hacer lo mismo Bla Bla Bla Bla The html viewer The applet Bla Bla Bla Bla
Cómo coloco un Applet en una página HTML ? <HTML> <HEAD> <TITLE> My first Applet </TITLE> </HEAD> <BODY> ....... Así se adhiere un applet a una página Web <APPLET CODE = “MyApplet0.class” WIDTH=150 HEIGTH=25> </APPLET> ..... </BODY> Debe existir un applet programado en un archivo con el Nombre MyApplet.java y complado
Programando Applets El Hello world applet import java.applet.*; import java.awt.*; public class MyApplet0 extends Applet { public void paint(Graphics g) { g.drawString(“Hello World”,50,25); } }
Ejemplos de Applets • Que dibuja este Applet firstApplet.html MyApplet1.java secondApplet.html 2. Un applet que reacciona a clicks del mouse MyApplet2.java El Clock como Applet ClockApplet.html ClockX4Applet.java
Un Applet especial siteTextApplet.html 4. Este applet cargará otra página web 5.un applet con un thread siteTextApplet.java clock2Applet.html ClockX2Move.java
Reflexion • Reflection is the ability to “explore” an unknown class • For example: • Which are the variables of a class ? • Which are the methods of a class ? • Which are the declared methods ? • Which are the declared variables ? • What do I get if I perform a certain method of the class ?
The Most Important Methods • Field [ ] getFields() • Field [ ] getDeclaredFields() • Method [ ] getMethods() • Method [ ] getDeclaredMethods() • Constructor [ ] getConstructors() • Constructor[ ] getDeclaredConstructors() • These are performed over an object of the class Class
How do I get a Class Object ? • Class c = Classname.class; • Console.class String.class etc.. • Class c = object.getClass(); • Console cs = new Console(); • Class c = cs.getClass(); • Class c = Class.forName(String); • Class c = Class.forName(“Console”); ReflectionTest ReflectionTest2 ReflectionTest3
Objects doing things by themselves • We will program a clock which updates itself (almost) every second • For this, the clock must be of the class thread • Every object of the thread class can start an independent execution line (“thread”) which will me executed in parallel to the programs of the main method
Making things in parallelThe Thread Main Thread1 Thread2 Thread3
Making things in parallelThe Thread • A thread is an independent, parallel line of statements execution • It consists basically of method (run) written in a special way which can be activated by another method • When a method activates a thread it continues executing its own statements while the thread starts executing the statements of the method called run() • There are different ways to write threads, we will see the most easy to write
The clock has a thread to advance the current time ActiveClock Two threads
Sometimes you cannot implement the your class as an extension of a thread • For example, if the program has to implement a graphical interface, it should be declared as an extension of a Frame, if it is programmed as an Applet it must extend the Applet class • The we can use the interface Runnable, which means the Server will also be seen as a Runnable class and has to implement the run method run(). • To initiate a parallel execution of the run method a Thread object must be created. In the creation a pointer to the server object (this) should be passed as parameter. With this, the Thread object will have access to the run method. • The run method implemented in the server class will be executed by performing start() on the new created Thread object.
Example with interface Runnable • See and execute the program NoSincron.java • Note that the Threads created in this way will have access to all resources of the server. • De la otra forma es más bien una opción, (pasar un puntero al servidor cuando se crea un objeto thread) • De cualquier forma, será frecuente el compartir recursos y por lo tanto la admistración de ellos es importante
Critical regions and semaphores in Java • Java provides basically two methods for controlling concurrent access to resources • You can declare a whole method as a critical region (see sincron1) • You can use the semaphore of any object (see sincron2)