Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni...

26
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().

Transcript of Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni...

Page 1: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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

Page 2: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Ancora su repaint…

p.addButton(); p.validate();p.repaint();

Page 3: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Animazione in Java

Problemi che possono nascere,e loro soluzione

Page 4: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Animazione in Java

Application MyFrame(JFrame)

ClockPanel(JPanel)

Page 5: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

ClockPanel - paintComponentpublic class ClockPanel extends JPanel { …

public void paintComponent(Graphics g) {

Date d=new Date();

int sec=d.getSeconds();

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))));

} …

OBSOLETO, ma funziona

Page 6: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

MyFrame – version 1public 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();

}

}

...

Sembra ok,

ma NON FUNZIONA!

Page 7: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Animazione e Threads in Java

Application MyFrame(JFrame)

ClockPanel(JPanel)

Runner(Runnable)

Page 8: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Runnerpublic 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();

}

}

}

Page 9: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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();

}

...

Page 10: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Applicativi in Java

Come distribuire un applicativo fatto in Java?

Vedi anchehttp://www.excelsior-usa.com/articles/java-to-

exe.html

Page 11: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 12: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 13: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

executable jar

you can make your Java application runnable via a double-click by packaging it into a so called

executable jarYou 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.0Main-Class: animation.Application1

Page 14: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 15: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 16: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 17: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 18: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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

Page 19: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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>

Page 20: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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/

Page 21: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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/Alt-Tab 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.

Page 22: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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.

Page 23: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Custom wrappersPro JRE version check JRE download or bundling Unique process name and icon No end-user trainingContro Platform specific Desktop integration capabilities absent or very

limited

Per una lista di Wrappers (free o commerciali) vedihttp://www.excelsior-usa.com/articles/java-to-exe.html

Page 24: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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

Page 25: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

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!

Page 26: Ancora su repaint… Quando si modifica la composizione di un container, per mostrare le variazioni NON BASTA chiamare la repaint(), prima occorre chiamare.

Ahead-Of-Time Compilers

Pro Peformance increase IP protection Better user perceptionContro Disk footprint increase Limited applicability

Vedi: http://www.excelsior-usa.com/articles/java-to-exe.html

1 prodotto commerciale, 1 open source (immaturo)