1 / 16

Java Applets

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 ?.

Download Presentation

Java Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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>

  2. 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

  3. 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

  4. 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); } }

  5. 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

  6. 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

  7. 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 ?

  8. 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

  9. 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

  10. 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

  11. Making things in parallelThe Thread Main Thread1 Thread2 Thread3

  12. 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

  13. The clock has a thread to advance the current time ActiveClock Two threads

  14. 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.

  15. 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

  16. 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)

More Related