Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare la validate(). Ancora su repaint… p.addButton(); p.validate(); p.repaint(); Animazione in Java Problemi che possono nascere, e loro soluzione Animazione in Java Application MyFrame (JFrame) ClockPanel (JPanel) public class ClockPanel extends JPanel { … public void paintComponent(Graphics g) { Date d=new Date(); int sec=d.getSeconds(); OBSOLETO, ma funziona double angle=sec*Math.PI/30; int w=this.getWidth(); int h=this.getHeight(); g.setColor(Color.YELLOW); g.fillRect(0,0,w,h); g.setColor(Color.GREEN); g.fillOval(0,0,w,h); g.setColor(Color.BLACK); g.drawLine(w/2,h/2, (int)(w/2*(1+Math.cos(angle))) ,(int)(h/2*(1+Math.sin(angle)))); } … ClockPanel - paintComponent public class MyFrame extends JFrame { //Component initialization private void jbInit() throws Exception { contentPane=new ClockPanel(); this.setContentPane(contentPane); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); while (true) { try { Thread.sleep(1000); } catch (InterruptedException ex) { } contentPane.repaint(); } } ... MyFrame – version 1 Animazione e Threads in Java Application Runner (Runnable) MyFrame (JFrame) ClockPanel (JPanel) public class Runner implements Runnable { ClockPanel cp; public Runner(ClockPanel cp) { this.cp=cp; } public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException ex) { } cp.repaint(); } } Runner } MyFrame – version 2 public class MyFrame extends JFrame { //Component initialization private void jbInit() throws Exception { contentPane=new ClockPanel(); this.setContentPane(contentPane); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); Runner r=new Runner(contentPane); new Thread(r).start(); } ... Applicativi in Java Come distribuire un applicativo fatto in Java? Vedi anche http://www.excelsior-usa.com/articles/java-to-exe.html JRE? What is that? What version? the author of a Java program has to ensure that the proper version of the JRE is installed on an end user system. In a general case you may not expect that your end users will know what a JRE is, how to check its version, and how to download and install it. A .bat file (or a .sh file) Even if you can make sure the right version of the JRE is properly installed on enduser systems, which is quite possible in a classroom or enterprise environment, the command line required to launch your Java application can be quite long: java -Xmx200m -cp whatever.jar -Dsome.property MyApp Yes, you may put that line into a batch file and call it runme.bat, but it looks so much easier to give your program to a friend, teacher or colleague as a single file that can be run by a double-click. Or, even better, enable it to be installed and uninstalled in a native manner without affecting other applications. executable jar you can make your Java application runnable via a double-click by packaging it into a so called executable jar You do that by specifying the main class of your application, any extra jar files it may require and so on in the jar's manifest file MANIFEST.MF Manifest-Version: 1.0 Main-Class: animation.Application1 executable jar Crea un jar: jar cvfm MyApp.jar MyApp.mf *.class *.gif Esegui un jar: java -jar MyApp.jar the Java launcher will read the manifest from MyApp.jar and invoke the main method from the class specified in the Manifest. If you double-click that jar file on a system that has JRE installed, the java launcher will be invoked automatically. JAR problems The major problem with executable jars is compatibility. The default JRE may be of an older version than is required by your application or may not have the necessary Java Optional Packages installed. JWS - Java Web Start Using Java Web Start technology, standalone Java software applications can be deployed with a single click over the network. Java Web Start ensures the most current version of the application will be deployed, as well as the correct version of the Java Runtime Environment (JRE). Java Web Start downloads, installs, and manages one or more Java Runtime Environments as required by the applications the user runs. JWS - Java Web Start After running the application for the first time from the web page as described above, the application is cached. This both improves application performance and enables the user to access it without returning to the web page. The application is added to the Java Application Cache Viewer. Users can simplify running a Java Web Start application further by adding a shortcut to the desktop, by selecting Install Shortcuts from the Application menu in the Java Application Cache Viewer. JWS - Java Web Start Developer responsibilities: - Crea un JAR file - Crea un JNLP file - Crea una Web Page - Metti il tutto su un Web Server JWS - JNLP file <?xml version="1.0" encoding="utf-8"?> <jnlp spec="1.0“ codebase="URL of application on your Web server" href="Notepad.jnlp"> <information> <title>Notepad Demo</title> <vendor>Sun Microsystems, Inc.</vendor> <offline-allowed/> </information> <resources> <jar href="Notepad.jar"/> <j2se version="1.3+" href="http://java.sun.com/products/autodl/j2se"/> </resources> <application-desc main-class="Notepad"/> </jnlp> JWS - Java Web Start Where Do You Get Java Web Start? Java Web Start is included in the Java Runtime Environment (JRE) as part of J2SE 5.0. Vedi http://java.sun.com/products/javawebstart/ Custom wrappers When a Java program is invoked the operating system runs a Java launcher from the JRE. The Windows version of the JRE has separate launchers for command-line and GUI apps, called java.exe and javaw.exe respectively. As a result, all running Java applications have the same Taskbar/AltTab icons and appear in the Windows Task Manager as either java.exe or javaw.exe. If you have two or more Java apps running, you have no means to distinguish between multiple instances of the standard Java launcher in the Task Manager. Custom wrappers A Java wrapper in essentially a custom Java launcher that is also a self-extracting archive containing all the application's classes, jars and auxiliary files. Some wrapper provide additional features such as instant splash screen, stdout and stderr redirection. A wrapper normally looks up the JRE upon startup. If the JRE is not present or its version does not match the application's compatibility requirements, some wrappers may install the JRE (if you have included it when wrapping your application) and/or download and install the required version of the JRE. The most sophisticated wrappers may also setup file associations and create shortcuts on first run. Custom wrappers Pro JRE version check JRE download or bundling Unique process name and icon No end-user training Contro Platform specific Desktop integration capabilities absent or very limited Per una lista di Wrappers (free o commerciali) vedi http://www.excelsior-usa.com/articles/java-to-exe.html Installers Installer tools may give you the following benefits: * Install-time JRE detection and download * Generation of native launchers * User-editable JVM parameter files * Redirection of stderr and stdout for saving logs and exception stack traces. * Registration of Java applications as Windows services and Unix daemons Platform-centric, Multi-Platform, Cross-Platform free: IzPack, Advanced Installer for Java Ahead-Of-Time Compilers AOT compilers are known also as "static compilers" and "native code compilers". The latter term is the most used and, as it often happens, the least correct from the technical standpoint, because JIT compilers also produce native code. An Ahead-Of-Time (AOT) compiler takes as input your jars and class files and produces a conventional native executable for the target platform, such as Windows EXE or Linux ELF binary. NON SERVE LA JRE! Ahead-Of-Time Compilers Pro Peformance increase IP protection Better user perception Contro Disk footprint increase Limited applicability 1 prodotto commerciale, 1 open source (immaturo) Vedi: http://www.excelsior-usa.com/articles/java-to-exe.html