Title: Ancora su repaint
1Ancora su repaint
- Quando si modifica la composizione di un
container, per mostrare le variazioni NON BASTA
chiamare la repaint(), prima occorre chiamare la
validate().
2Ancora su repaint
- p.addButton()
- p.validate()
- p.repaint()
3Animazione in Java
- Problemi che possono nascere,
- e loro soluzione
4Animazione in Java
Application
MyFrame (JFrame)
ClockPanel (JPanel)
5ClockPanel - paintComponent
public class ClockPanel extends JPanel
public void paintComponent(Graphics g) Date
dnew Date() int secd.getSeconds()
double anglesecMath.PI/30 int
wthis.getWidth() int hthis.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(1Math.cos(angle)))
,(int)(h/2(1Math.sin(angle))))
OBSOLETO, ma funziona
6MyFrame version 1
public class MyFrame extends JFrame
//Component initialization private void
jbInit() throws Exception contentPanenew
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!
7Animazione e Threads in Java
Application
MyFrame (JFrame)
ClockPanel (JPanel)
Runner (Runnable)
8Runner
public class Runner implements Runnable
ClockPanel cp public Runner(ClockPanel cp)
this.cpcp public void run() while
(true) try Thread.sleep(1000)
catch (InterruptedException ex)
cp.repaint()
9MyFrame version 2
public class MyFrame extends JFrame
//Component initialization private void
jbInit() throws Exception contentPanenew
ClockPanel() this.setContentPane(contentPane)
contentPane.setLayout(borderLayout1)
this.setSize(new Dimension(400, 300))
this.setTitle("Frame Title") Runner rnew
Runner(contentPane) new Thread(r).start()
...
10Applicativi in Java
- Come distribuire un applicativo fatto in Java?
- Vedi anche
- http//www.excelsior-usa.com/articles/java-to-exe.
html
11JRE? 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.
12A .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.
13executable 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
14executable 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.
15JAR 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.
16JWS - 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.
17JWS - 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.
18JWS - Java Web Start
- Developer responsibilities
- Crea un JAR file
- Crea un JNLP file
- Crea una Web Page
- Metti il tutto su un Web Server
19JWS - JNLP file
- lt?xml version"1.0" encoding"utf-8"?gt
- ltjnlp spec"1.0 codebase"URL of application on
your Web server" - href"Notepad.jnlp"gt
- ltinformationgt
- lttitlegtNotepad Demolt/titlegt
- ltvendorgtSun Microsystems, Inc.lt/vendorgt
- ltoffline-allowed/gt
- lt/informationgt
- ltresourcesgt
- ltjar href"Notepad.jar"/gt
- ltj2se version"1.3"
- href"http//java.sun.com/products/
autodl/j2se"/gt - lt/resourcesgt
- ltapplication-desc main-class"Notepad"/gt
- lt/jnlpgt
20JWS - 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/
21Custom 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.
22Custom 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.
23Custom 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
24Installers
- 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
25Ahead-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!
26Ahead-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-t
o-exe.html