THE INTERNET,mapped on the opposite page, is a scalefree network in that Java Appletek II. dis.'~tj port,from BYALBERTU\SZLOBARABASI ANDERICBONABEAU THE INTERNET,mapped on the opposite page, is a scalefree network in that BYALBERTU\SZLOBARABASI ANDERICBONABEAU 2 Applet GUI Az appletek a böngész! ablakában jelennek meg háttér szín paraméter meghatározott méret platformok layout manager Az applet osztályok letöltése a hálózaton alosztályok
THE INTERNET,mapped on the opposite page, is a scalefree network in that BYALBERTU\SZLOBARABASI ANDERICBONABEAU 3 Standard out Diagnosztikai, debug üzenetek standard out, standard error System.out, System.err Appletviewer consol ablak shell ablak Célszer"en kikapcsolható kiírás THE INTERNET,mapped on the opposite page, is a scalefree network in that 4 Rendszer tulajdonságok M"ködési környezet tulajdonságai java.util.properties System.getProperty BYALBERTU\SZLOBARABASI ANDERICBONABEAU "file.separator" File separator (for example, "/") "java.class.version" Java class version number "java.vendor" Java vendorspecific string "java.vendor.url" Java vendor URL "java.version" Java version number "line.separator" Line separator "os.arch" Operating system architecture "os.name" Operating system name "path.separator" Path separator (for example, ":")
THE INTERNET,mapped on the opposite page, is a scalefree network in that 5 import java.awt.*; import java.awt.event.*; import javax.swing.*; Rendszer tulajdonságok (pl.) public class GetOpenProperties extends JApplet { private String[] propertynames = {"file.separator", "line.separator", "path.separator", "java.class.version", "java.vendor", "java.vendor.url", "java.version", "os.name", "os.arch", "os.version"; private final int numproperties = propertynames.length; private JLabel[] values; private javax.swing.timer timer; private int currentpropnum = 0; public void init() { ;i~e~tu"eg~ye'l"rne(;lb9.$ha redorganili //Execute ngprincipies. a job on the eventdispatching thread: //creating this applet's GUI. SwingUtilities.invokeAndWait(new Runnable() { BYALBERTU\SZLOBARABASI ANDERICBONABEAU public void run() { creategui(); ); catch (Exception e) { System.err.println("createGUI didn't successfully complete"); public void start() { //Update the GUI every 1/4 second or so. timer = new javax.swing.timer(250, new PropertyUpdater()); timer.setcoalesce(false); timer.start(); public void stop() { if (timer!= null) { timer.stop(); public void destroy() { //Execute a job on the eventdispatching thread: //destroying this applet's GUI. SwingUtilities.invokeAndWait(new Runnable() { public void run() { remove(getcontentpane()); ); catch (Exception e) { THE INTERNET,mapped on the opposite page, is a scalefree network in that 6 Rendszer tulajdonságok (pl.) private void creategui() { JPanel contentpane = new JPanel(new GridBagLayout()); GridBagConstraints labelconstraints = new GridBagConstraints(); GridBagConstraints valueconstraints = new GridBagConstraints(); labelconstraints.anchor = GridBagConstraints.WEST; labelconstraints.ipadx = 10; valueconstraints.fill = GridBagConstraints.HORIZONTAL; valueconstraints.gridwidth = GridBagConstraints.REMAINDER; valueconstraints.weightx = 1.0; //Extra space to values column. for (int i = 0; i < numproperties; i++) { names[i] = new JLabel(propertyNames[i]); names[i].setfont(propertyfont); contentpane.add(names[i], labelconstraints); values[i] = new JLabel(firstValue); values[i].setfont(valuefont); contentpane.add(values[i], valueconstraints); names[i].setlabelfor(values[i]); BYALBERTU\SZLOBARABASI ANDERICBONABEAU //Set up the Label arrays. JLabel[] names = new JLabel[numProperties]; values = new JLabel[numProperties]; String firstvalue = "<not read yet>"; //Fonts Font headingfont = new Font("SansSerif", Font.BOLD, 14); Font propertyfont = new Font("SansSerif", Font.BOLD, 12); Font valuefont = new Font("SansSerif", Font.PLAIN, 12); //Add headings. contentpane.add(createheading("property Name", headingfont), labelconstraints); contentpane.add(createheading("value", headingfont), valueconstraints); contentpane.setborder(borderfactory.createcompoundborder( BorderFactory.createLineBorder(Color.black), BorderFactory.createEmptyBorder(5,20,5,10))); setcontentpane(contentpane); private JLabel createheading(string text, Font font) { JLabel l = new JLabel(text); l.setfont(font); l.setborder(borderfactory.createcompoundborder( BorderFactory.createEmptyBorder(0,0,5,0), BorderFactory.createMatteBorder(0,0,1,0,Color.black))); return l;
THE INTERNET,mapped on the opposite page, is a scalefree network in that 7 Rendszer tulajdonságok (pl.) private class PropertyUpdater implements ActionListener { private String value; public void actionperformed(actionevent e) { if (currentpropnum < numproperties) { value = System.getProperty(propertyNames[currentPropNum]); if (value == null) { value = "<null value!>"; values[currentpropnum].settext(value); catch (SecurityException exc) { values[currentpropnum].settext("could not read: SECURITY EXCEPTION!"); currentpropnum++; else { timer.stop(); BYALBERTU\SZLOBARABASI ANDERICBONABEAU THE INTERNET,mapped on the opposite page, is a scalefree network in that 8 Többszálúság BYALBERTU\SZLOBARABASI ANDERICBONABEAU Id!igényes taszk végrehajtása a eseménykezelés felfüggesztése nélkül Periódikusan ismétl!d! tevékenységek végrehajtása Szerver alkalmazások
THE INTERNET,mapped on the opposite page, is a scalefree network in that BYALBERTU\SZLOBARABASI ANDERICBONABEAU 9 Swing és a szálak Swing komponensek általában nem támogatják a többszálon való hozzáférés (nem thread safeek) hozzáférés csak az eseménykezel! szálon komponens realizálás >eseménykezel! szál Swing komponens megrajzolása, eseménykezelés paint(), ActionPerformed() THE INTERNET,mapped on the opposite page, is a scalefree network in that 10 Swing és a szálak (folyt.) BYALBERTU\SZLOBARABASI ANDERICBONABEAU public class MyApplication { public static void main(string[] args) { JFrame f = new JFrame("Labels"); // Add components to // the frame here... f.pack(); realizálás f.show(); // Don't do any more GUI work here... biztonságos
THE INTERNET,mapped on the opposite page, is a scalefree network in that 11 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Swing és a szálak (folyt.) Applet GUI létrehozható az init() metódusban realizálás start()kor ne legyen id!igényes init() külön szál indítása Thread safe bárhonnan repaint(), revalidate() add...listener(), remove...listener() THE INTERNET,mapped on the opposite page, is a scalefree network in that 12 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Swing és a szálak (folyt.) Realizált GUI UIevent (AWT event) driven m"ködes GUI update igény más forrásból (nem AWT event) invokeandwait(), invokelater()
THE INTERNET,mapped on the opposite page, is a scalefree network in that 13 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Swing és a szálak (folyt.) Szálak Swing barát létrehozása Timer class periódikus tevékenységekhez actionperformed() metódus végrehajtása az eseménykezel! szálon THE INTERNET,mapped on the opposite page, is a scalefree network in that 14 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Többszálú appletek Az appletnek lehet több szála GUI létrehozása az eseménykezel! szálból init, start, stop, destroy nem kerül meghívásra innen Szálak használata Id!igényes inicializálás Animáció
THE INTERNET,mapped on the opposite page, is a scalefree network in that 15 using import like colors forjavax.swing.*; similar Webaddresses. import java.awt.*; public class PrintThread extends JApplet { Szálak példa 1. public void start() { additem("start: " + threadinfo(thread.currentthread()), false); //not on eventdispatching thread JTextArea display; public void init() { //Execute a job on the eventdispatching thread: //creating this applet's GUI. SwingUtilities.invokeAndWait(new Runnable() { public void run() { creategui(); ); catch (Exception e) { System.err.println("createGUI didn't successfully complete"); additem("init: " + threadinfo(thread.currentthread()), false); //not on eventdispatching thread private void creategui() { //Create the text area and make it uneditable. display = new JTextArea(1, 80); display.seteditable(false); BYALBERTU\SZLOBARABASI ANDERICBONABEAU //Set the layout manager so that the text area //will be as wide as possible. getcontentpane().setlayout(new GridLayout(1,0)); public void stop() { additem("stop: " + threadinfo(thread.currentthread()), false); //not on eventdispatching thread public void destroy() { additem("destroy: " + threadinfo(thread.currentthread()), false); //not on eventdispatching thread //Execute a job on the eventdispatching thread: //destroying this applet's GUI. SwingUtilities.invokeAndWait(new Runnable() { public void run() { getcontentpane().removeall(); additem("doing removeall: " + threadinfo(thread.currentthread()), true); //on eventdispatching thread ); catch (Exception e) { String threadinfo(thread t) { return "thread=" + t.getname() + ", " + "thread group=" + t.getthreadgroup().getname(); //Add the text area (in a scroll pane) to the applet. getcontentpane().add(new JScrollPane(display)); additem("creategui: " + threadinfo(thread.currentthread()), true); //on eventdispatching thread THE INTERNET,mapped on the opposite page, is a scalefree network in that 16 Szálak példa 1. (folyt.) void additem(string newword, boolean onedt) { final String s = newword + "\n"; System.out.println(newWord); if (onedt) { display.append(s); else { //Execute a job on the eventdispatching thread: //updating this applet's GUI. SwingUtilities.invokeAndWait(new Runnable() { public void run() { display.append(s); ); catch (Exception e) { BYALBERTU\SZLOBARABASI ANDERICBONABEAU
THE INTERNET,mapped on the opposite page, is a scalefree network in that 17 Szálak példa 2. import java.awt.*; import java.applet.applet; /* * Based on Arthur van Hoff's animation examples, this applet * can serve as a template for all animation applets. */ public class AnimatorApplet extends Applet implements Runnable { int framenumber = 1; int delay; Thread animatorthread; boolean frozen = false; public void init() { String str; int fps = 10; public void start() { if (frozen) { //Do nothing. The user has requested that we //stop changing the image. else { //Start animating! if (animatorthread == null) { animatorthread = new Thread(this); animatorthread.start(); public void stop() { //Stop the animating thread. animatorthread = null; BYALBERTU\SZLOBARABASI ANDERICBONABEAU //How many milliseconds between frames? str = getparameter("fps"); if (str!= null) { fps = Integer.parseInt(str); catch (Exception e) { delay = (fps > 0)? (1000 / fps) : 100; public boolean mousedown(event e, int x, int y) { if (frozen) { frozen = false; start(); else { frozen = true; stop(); return true; THE INTERNET,mapped on the opposite page, is a scalefree network in that 18 Szálak példa 2. (folyt.) public void run() { //Just to be nice, lower this thread's priority //so it can't interfere with other processing going on. Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //Remember the starting time. long starttime = System.currentTimeMillis(); //This is the animation loop. while (Thread.currentThread() == animatorthread) { //Advance the animation frame. framenumber++; //Display it. repaint(); //Delay depending on how far we are behind. starttime += delay; Thread.sleep(Math.max(0, starttimesystem.currenttimemillis())); catch (InterruptedException e) { break; BYALBERTU\SZLOBARABASI ANDERICBONABEAU //Draw the current frame of animation. public void paint(graphics g) { g.drawstring("frame " + framenumber, 0, 30);
THE INTERNET,mapped on the opposite page, is a scalefree network in that 19 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Szálak példa 3. getaudioclip csak miután végzett tér vissza betöltés különszálon termel!/fogyasztó felállás SoundLoader/SoundExample SoundList THE INTERNET,mapped on the opposite page, is a scalefree network in that 20 Együttm!ködés szerveroldali alkalmzásokkal BYALBERTU\SZLOBARABASI ANDERICBONABEAU java.net csomag használható csak azzal a hoszttal, melyr!l az applet jött getcodebase().gethost() t"zfalak?
THE INTERNET,mapped on the opposite page, is a scalefree network in that 21 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Biztonsági korlátok megkerülése szerverrel Applet nem írhat és olvashat fájlokat a helyi fájlrendszeren adatok tárolása szerver alk. segítségével Hálózati kapcsolat csak a származási hoszttal szerver alk. építi ki a hálózati kapcsolatot más hosztokhoz Applet nem indíthat alkalmazásokat a futtató gépen szerveroldalon futhatnak alkalmazások THE INTERNET,mapped on the opposite page, is a scalefree network in that 22 BYALBERTU\SZLOBARABASI ANDERICBONABEAU A kész applet Debug konzol üzenetek kikapcsolva O# screen nem fogyaszt CPUt stop() metódus hang kikapcsolható getparameterinfo() getappletinfo()
THE INTERNET,mapped on the opposite page, is a scalefree network in that JNLP és Java Web Start dis.'~tj port,from BYALBERTU\SZLOBARABASI ANDERICBONABEAU THE INTERNET,mapped on the opposite page, is a scalefree network in that 24 BYALBERTU\SZLOBARABASI ANDERICBONABEAU Java Web Start Java alkalmazások elérése Web szerveren keresztül böngész! kontextuson kívüli végrehajtás végrahajtás homokozóban hozzáférési kérések kliens oldalon Java Web Start szerveroldal megfelel!en csomagolt alkalmazás Java Network Launch Protocol
THE INTERNET,mapped on the opposite page, is a scalefree network in that 25 Java Web Start példa import java.awt.*; import javax.swing.*; import java.io.*; import java.net.*; public class TheTime { public static void main(string args[]) { JFrame frame = new JFrame("Time Check"); frame.setdefaultcloseoperation(jframe.exit_on_close); JLabel label = new JLabel(); Container content = frame.getcontentpane(); content.add(label, BorderLayout.CENTER); String message = "missing"; BufferedReader reader = null; Socket socket = new Socket("time.nist.gov", 13); InputStream is = socket.getinputstream(); InputStreamReader isr = new InputStreamReader(is); reader = new BufferedReader(isr); reader.readline(); // skip blank line message = reader.readline(); catch (MalformedURLException e) { System.err.println("Malformed: " + e); catch (IOException e) { System.err.println("I/O Exception: " + e); finally { BYALBERTU\SZLOBARABASI ANDERICBONABEAU if (reader!= null) { reader.close(); catch (IOException ignored) { label.settext(message); frame.pack(); frame.show(); TheTime.java THE INTERNET,mapped on the opposite page, is a scalefree network in that 26 Java Web Start példa (folyt.) <?xml version="1.0" encoding="utf8"?> <jnlp spec="1.0+" codebase="file:///c:/jdc/jnlp/" > <information> <title>time Check</title> <vendor>java Developer Connection</vendor> <homepage href="/jdc" /> <description>demonstration of JNLP</description> </information> <offlineallowed/> <security> <j2eeapplicationclientpermissions/> </security> <resources> <j2se version="1.2+" /> <jar href="/developer/technicalarticles/programming/jnlp/jnlptime.jar"/> </resources> <applicationdesc mainclass="thetime" /> </jnlp> BYALBERTU\SZLOBARABASI ANDERICBONABEAU time.jnlp
a 27 THE INTERNET,mapped on the opposite page, is a scalefree network in that traces the shortest routes from a test Web sinho about 100,000 others, Java Web Start példa (folyt.) using like colors for similar Web addresses. s J;i~e~tu"eg~Ye'l"rne(;lb9.$ha red organili ng principies. t m a BYALBERTU\SZLO BARABASI ANDERICBONABEAU 28 THE INTERNET,mapped on the opposite page, is a scalefree network in that traces the shortest routes from a test Web sinho about 100,000 others, using like colors for similar Web addresses. s J;i~e~tu"eg~Ye'l"rne(;lb9.$ha red organili ng principies. t m BYALBERTU\SZLO BARABASI ANDERICBONABEAU Java Web Start példa (folyt.)