NetBeans

Transcript

NetBeans
NetBeans - Esempi
Giovanni Torrero
[email protected]
Indice
Capitolo 1. Applicazioni
1.1. File Chooser
1.2. Grafica 2D
5
5
24
Indice analitico
49
Bibliografia
51
Bibliografia
53
3
CAPITOLO 1
Applicazioni
1.1. File Chooser
Come primo esempio costruiremo una semplice applicazione che
consente di aprire un file contenente una stringa la scrive in un componente tipo jText cioè un campo di testo. Diamo per scontato che si
sappia costruire un progetto del tipo Java->Java Desktop Application ,
inoltre diamo per scontato che il lettore abbia già una certa dimestichezza con l’IDE di NetBeans e che quindi sappia usare i vari ammennicoli che in esso compaiono, chi non ha questa dimestichezza
smanetti un po’ e veda di acquisirla.
Chiamiamo la nostra applicazione SceltaFile , costruiamo il relativo progetto e successivamente apriamo il file SceltaFileView.java ,
passiamo alla vista Design e dalla palette trasciniamo una tool bar
,che si trova negli Swing Containers , e poniamola nella parte alta del form. Inseriamo nella tool bar due botton , togliamo ad entrambi il testo e inseriamo due icone che abbiamo precedentemente
preparato, in formato 16x16 ed estensione png , Infine inseriamo un
campo di testo e il form dell’applicazione dovrebbe apparire come
quello riportato nella figura sottostante.
5
i
le
file
contenenti
icone
vanno
memorizzati
nella
directory del progetto
secondo
questo
percorso ..\SceltaFile\
src\sceltafile\resources
6
1. APPLICAZIONI
F IGURA 1.1.1. Form principale
Aggiungiamo due voci al menu File scegliendo la Palette poi Swing Menus
ed infine Menu Item , trasciniamo sul menu File e posizioniamo come
indicato nella figura sottostante
1.1. FILE CHOOSER
7
F IGURA 1.1.2. Menu File
Scegliendo la voce del menu che ci interessa e facendo click con il
tasto destro otteniamo un pop-menu al fondo del quale c’è la voce
Properties selezionandola apriamo la relativa finestra nella quale
modifichiamo la voce toolTipText , la voce text
8
1. APPLICAZIONI
F IGURA 1.1.3. La finestra Properties
Mentre la maggior parte dei cambiamenti proposti sono molto intuitivi action è leggermente più complesso, facendo click sul tastino
inizialmente nella riga
di action non compare
Save ma null
con i tre puntini
relativo alla riga in cui si trova action compare la
seguente finestra di dialogo
1.1. FILE CHOOSER
9
F IGURA 1.1.4. Finestra di dialogo action prima delle modifiche
in questa finestra facciamo la scelta indicata nella figura poi completiamo i rimanenti campi come indicato nella figura sottostante:
10
1. APPLICAZIONI
action
F IGURA 1.1.5. Finestra di dialogo relativa a action
Una volta creata action può essere usata sia per la relativa voce
del menu che per il relativo tasto della tool bar, quindi per ogni tasto
della tool bar e per ogni voce del menu bisogna ripetere la procedura
di cui sopra.
Terminato quanto detto sopra, nel file SceltaFileView.java passiamo
alla modalità Source e ci accorgiamo che sono stati costruiti gli
scheletri di due nuovi metodi, con la notazione @Action , il metodo public void Salva() e il metodo public void ApriFile() , questi due
1.1. FILE CHOOSER
11
metodi sono quelli che gestiranno il click su una delle voci del menu
che abbiamo aggiunto oppure su uno dei tasti della tool bar.
Quando premiamo un pulsante della tool bar oppure una delle voci
aggiunte nel menu File vogliamo che appaia una finestra di dialogo
che ci permetta di scegliere la directory dalla quale leggere il file
oppure nella quale memorizzarlo. Per ottenere quanto detto bisogna
costruire una nuova classe che implementi una finestra di dialogo di
quel tipo, anzi bisogna costruire due classi: una per l’apertura del file
e una per la scrittura. Cominciamo a costruire quella per la scrittura
,
La finestra di dialogo che permette la scelta del file da gestire è quella che siamo abituati a vedere nei programmi di Window e di altri sistemi operativi e in Java è implementata nella classe JFileChooser ,
l’uso di questo componente è ben spiegato al seguente indirizzo:
http://java.sun.com/docs/books/tutorial/uiswing/components/
filechooser.html .
Nella palette esiste un componente di tipo JFileChooser , trascinandolo in un form otteniamo un componente che ci permette di
scegliere un file dal disco fisso ma poi non sono riuscito a ottenere
il nome del file e nemmeno a gestire l’azione dei pulsanti che compaiono nel componente e nè su Internet e nè sui testi in mio possesso sono riuscito a trovare un indizio che mi illuminasse sull’uso
di questo componente, sembra proprio che l’unico modo di risolvere
il problema sia quello indicato nell’url precedente. Nel tutorial prima
indicato si afferma che bastano le seguenti due righe di codice per
far apparire un’istanza di JFileChooser :
1
2
3
4
5
/ / Create a f i l e chooser f i n a l J F i l e C h o o s e r
f c = new J F i l e C h o o s e r ( ) ;
...
/ / I n response t o a b u t t o n c l i c k :
i n t r e t u r n V a l = f c . showOpenDialog ( aComponent ) ;
La seconda linea costruisce un’istanza della classe JFileChooser
mentre la quarta linea fa apparire l’istanza costruita e restituisce un
numero intero se questo intero coincide con APPROVE_OPTION
significa che è stato premuto il tasto Salva (oppure Apri). APPROVE_OPTION è una costante intera definita nella classe JFileChooser
12
1. APPLICAZIONI
, in questa classe sono definite altre costanti riportate nella sottostante tabella
F IGURA 1.1.6. Costanti della classe JFileChooser
Se alle righe di codice di cui sopra si aggiunge la seguente:
S t r i n g n o m e _ f i l e = f c . g e t S e l e c t e d F i l e ( ) . getPath ( ) ;
abbiamo in returnVal il codice del tasto premuto: APPROVE_OPTION
se abbiamo premuto Salva (oppure Apri) , CANCEL_OPTION se abbiamo premuto Annulla e in nome_file il nome, completo del suo
percorso, del file selezionato.
1.1. FILE CHOOSER
13
La pressione del tasto Annulla non è da gestire perché la sua gestione è automatica e provoca la chiusura della finestra di dialogo
senza salvare o aprire nulla, rimane da gestire solo la pressione del
tasto Salva (o Apri).
14
1. APPLICAZIONI
La pressione del tasto Salva viene gestita nella seguente procedura.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Action
public void Salva ( ) {
S t r i n g testo = j T e x t F i e l d 1 . getText ( ) ;
S t r i n g ch ;
JFrame mainFrame = S c e l t a F i l e A p p . g e t A p p l i c a t i o n ( ) .
getMainFrame ( ) ;
/ / Le due r i g h e s e g u e n t i svolgono i l r u o l o d e l l e
/ / i s t r u z i o n i precedentemente d i s c u s s e
J Fi l e C ho o s er chooser = new J F il e C ho o s e r ( ) ;
i n t r e t u r n V a l = chooser . showSaveDialog ( mainFrame ) ;
i n t opzione = JOptionPane . YES_OPTION ;
S t r i n g messaggio = " I l f i l e s c e l t o e s i s t e già , v u o i
sovrascriverlo?" ;
S t r i n g english_message = " The chosen f i l e a l r e a d y
e x i s t s , you want t o o v e r w r i t e i t ? " ;
String t i t o l o = " Attenzione ! ! ! ! !
Warning ! ! ! ! ! " ;
/ / I n i z i a q u i l a p a r t e d e l c o d i c e che g e s t i s c e
/ / l a p r e s s i o n e d e l t a s t o Salva
i f ( r e t u r n V a l == J F i le C h o os e r . APPROVE_OPTION) {
ch = chooser . g e t S e l e c t e d F i l e ( ) . getPath ( ) ;
F i l e f c h = new F i l e ( ch ) ;
/ / N e l l e due r i g h e p r e c e d e n t i v i e n e prima l e t t o i l nome completo
/ / d e l f i l e s e l e z i o n a t o p o i v i e n e c o s t r u i t o un o g g e t t o d i t i p o
/ / f i l e c o l l e g a t o a q u e l nome .
/ / N e l l a r i g a seguente s i c o n t r o l l a che i l f i l e s e l e z i o n a t o non
/ / s i a s i a p r e s e n t e n e l l a d i r e c t o r y s e l e z i o n a t a , se questo f i l e
/ / e s i s t e g i à a l l o r a v i e n e v i s u a l i z z a t a una f i n e s t r a che consente
/ / d i s c e g l i e r e se s o v r a s c r i v e r e i l f i l e oppure r i t o r n a r e
/ / a s c e g l i e r e un nome nuovo
i f ( fch . i s F i l e ( ) ) {
opzione = JOptionPane . showConfirmDialog (
mainFrame , messaggio + " \ n " +
english_message , t i t o l o , JOptionPane .
YES_NO_OPTION) ;
}
i f ( opzione == JOptionPane . YES_OPTION) {
/ / Se i l f i l e non e s i s t e ancora oppure vogliamo s o v r a s c r i v e r l o
procediamo
1.1. FILE CHOOSER
15
Preoccupiamoci ora dell’azione della voce del menu Apri e del corrispondente tasto della toolbar. Il metodo che gestisce questo evento
è quello riportato nel listato sottostante:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Action
public void ApriFile() {
String ch;
String lista = null;
JFrame mainFrame = SceltaFileApp.getApplication().getMainFrame();
//Le successive due righe definiscono un oggetto di tipo JFileChooser
//e lo visualizza in modalita OpenDialog
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(mainFrame);
//Quando viene premuto il pulsante Apri viene eseguito questo blocco di
codice
if (returnVal == JFileChooser.APPROVE_OPTION) {
ch = chooser.getSelectedFile().getPath();
try {
//Viene aperto un flusso per la lettura di oggetti di tipo String
FileInputStream f = new FileInputStream(new File(ch));
ObjectInputStream s = new ObjectInputStream(f);
try {
//Viene letto un oggetto di tipo String e messo nella variabile lista
lista = (String) s.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(SceltaFileView.class.getName()).log(
Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(mainPanel, "I dati
caricati non sono compatibili con il programma");
}
s.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(mainPanel, "Qualche cosa non
va nella scelta del file");
}
if (lista instanceof String) {
//Se la variabile lista contiene effettivamente un oggetto di tipo String
//questi viene visualizzato nel campo di testo
jTextField1.setText(lista);
} else {
JOptionPane.showMessageDialog(mainPanel, "Il file caricato
non è compatibile con il programma");
}
}
}
16
1. APPLICAZIONI
Qui di seguito è riportato il listato completo della classe SceltaFileView
, la maggior parte del codice è scritto automaticamente da NetBeans, in pratica manualmente sono stati scritti solamente i due
metodi riportati nel listato di cui sopra.
/∗
∗ SceltaFileView . java
∗/
package s c e l t a f i l e ;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
org . j d e s k t o p . a p p l i c a t i o n . A c t i o n ;
org . j d e s k t o p . a p p l i c a t i o n . ResourceMap ;
org . j d e s k t o p . a p p l i c a t i o n . S i n g l e F r a m e A p p l i c a t i o n ;
org . j d e s k t o p . a p p l i c a t i o n . FrameView ;
org . j d e s k t o p . a p p l i c a t i o n . TaskMonitor ;
j a v a . awt . event . A c t i o n E v e n t ;
j a v a . awt . event . A c t i o n L i s t e n e r ;
java . io . F i l e ;
java . io . FileInputStream ;
java . i o . FileOutputStream ;
j a v a . i o . IOException ;
java . i o . ObjectInputStream ;
j a v a . i o . ObjectOutputStream ;
java . u t i l . logging . Level ;
j a v a . u t i l . l o g g i n g . Logger ;
j a v a x . swing . Timer ;
j a v a x . swing . I c o n ;
j a v a x . swing . J D i a l o g ;
j a v a x . swing . J Fi l e Ch o o s er ;
j a v a x . swing . JFrame ;
j a v a x . swing . JOptionPane ;
/∗ ∗
∗ The a p p l i c a t i o n ’ s main frame .
∗/
public class S c e l t a F i l e V i e w extends FrameView {
public S c e l t a F i l e V i e w ( S i n g l e F r a m e A p p l i c a t i o n app ) {
super ( app ) ;
initComponents ( ) ;
/ / s t a t u s bar i n i t i a l i z a t i o n − message t i m e o u t , i d l e i c o n
and busy animation , e t c
ResourceMap resourceMap = getResourceMap ( ) ;
1.1. FILE CHOOSER
17
i n t messageTimeout = resourceMap . g e t I n t e g e r ( " S t a t u s B a r .
messageTimeout " ) ;
messageTimer = new Timer ( messageTimeout , new
ActionListener () {
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {
statusMessageLabel . s e t T e x t ( " " ) ;
}
}) ;
messageTimer . setRepeats ( f a l s e ) ;
i n t busyAnimationRate = resourceMap . g e t I n t e g e r ( " S t a t u s B ar .
busyAnimationRate " ) ;
f o r ( i n t i = 0 ; i < busyIcons . l e n g t h ; i ++) {
busyIcons [ i ] = resourceMap . g e t I c o n ( " S t a t u s B a r .
busyIcons [ " + i + " ] " ) ;
}
busyIconTimer = new Timer ( busyAnimationRate , new
ActionListener () {
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {
busyIconIndex = ( busyIconIndex + 1 ) % busyIcons .
length ;
s t a t u s A n i m a t i o n L a b e l . s e t I c o n ( busyIcons [
busyIconIndex ] ) ;
}
}) ;
i d l e I c o n = resourceMap . g e t I c o n ( " S t at u s B a r . i d l e I c o n " ) ;
statusAnimationLabel . setIcon ( i dl eI co n ) ;
progressBar . s e t V i s i b l e ( f a l s e ) ;
/ / c o n n e c t i n g a c t i o n t a s k s t o s t a t u s bar v i a TaskMonitor
TaskMonitor t a s k M o n i t o r = new TaskMonitor ( g e t A p p l i c a t i o n ( )
. getContext ( ) ) ;
t a s k M o n i t o r . addPropertyChangeListener (new j a v a . beans .
PropertyChangeListener ( ) {
public void propertyChange ( j a v a . beans .
PropertyChangeEvent e v t ) {
S t r i n g propertyName = e v t . getPropertyName ( ) ;
i f ( " s t a r t e d " . equals ( propertyName ) ) {
i f ( ! busyIconTimer . isRunning ( ) ) {
s t a t u s A n i m a t i o n L a b e l . s e t I c o n ( busyIcons [ 0 ] )
;
busyIconIndex = 0 ;
busyIconTimer . s t a r t ( ) ;
}
progressBar . s e t V i s i b l e ( t r u e ) ;
progressBar . s e t I n d e t e r m i n a t e ( t r u e ) ;
} else i f ( " done " . equals ( propertyName ) ) {
busyIconTimer . s t o p ( ) ;
statusAnimationLabel . setIcon ( i dl eI co n ) ;
progressBar . s e t V i s i b l e ( f a l s e ) ;
progressBar . s e t V a l u e ( 0 ) ;
} else i f ( " message " . equals ( propertyName ) ) {
18
1. APPLICAZIONI
S t r i n g t e x t = ( S t r i n g ) ( e v t . getNewValue ( ) ) ;
statusMessageLabel . s e t T e x t ( ( t e x t == n u l l ) ? " "
: text ) ;
messageTimer . r e s t a r t ( ) ;
} else i f ( " p r o g r e s s " . equals ( propertyName ) ) {
i n t v a l u e = ( I n t e g e r ) ( e v t . getNewValue ( ) ) ;
progressBar . s e t V i s i b l e ( t r u e ) ;
progressBar . s e t I n d e t e r m i n a t e ( f a l s e ) ;
progressBar . s e t V a l u e ( v a l u e ) ;
}
}
}) ;
}
@Action
public void showAboutBox ( ) {
i f ( aboutBox == n u l l ) {
JFrame mainFrame = S c e l t a F i l e A p p . g e t A p p l i c a t i o n ( ) .
getMainFrame ( ) ;
aboutBox = new S c e l t a F i l e A b o u t B o x ( mainFrame ) ;
aboutBox . s e t L o c a t i o n R e l a t i v e T o ( mainFrame ) ;
}
S c e l t a F i l e A p p . g e t A p p l i c a t i o n ( ) . show ( aboutBox ) ;
}
/ ∗ ∗ T h i s method i s c a l l e d from w i t h i n t h e c o n s t r u c t o r t o
∗ i n i t i a l i z e t h e form .
∗ WARNING: Do NOT modify t h i s code . The c o n t e n t o f t h i s
method i s
∗ always r eg e ne r at e d by t h e Form E d i t o r .
∗/
@SuppressWarnings ( " unchecked " )
/ / < e d i t o r −f o l d d e f a u l t s t a t e =" c o l l a p s e d " desc =" Generated Code
">
p r i v a t e void initComponents ( ) {
mainPanel = new j a v a x . swing . JPanel ( ) ;
j T o o l B a r 1 = new j a v a x . swing . JToolBar ( ) ;
j B u t t o n 1 = new j a v a x . swing . J B u t t o n ( ) ;
j B u t t o n 2 = new j a v a x . swing . J B u t t o n ( ) ;
j T e x t F i e l d 1 = new j a v a x . swing . J T e x t F i e l d ( ) ;
menuBar = new j a v a x . swing . JMenuBar ( ) ;
1.1. FILE CHOOSER
19
j a v a x . swing . JMenu f i l e M e n u = new j a v a x . swing . JMenu ( ) ;
jMenuItem1 = new j a v a x . swing . JMenuItem ( ) ;
jMenuItem2 = new j a v a x . swing . JMenuItem ( ) ;
j a v a x . swing . JMenuItem exitMenuItem = new j a v a x . swing .
JMenuItem ( ) ;
j a v a x . swing . JMenu helpMenu = new j a v a x . swing . JMenu ( ) ;
j a v a x . swing . JMenuItem aboutMenuItem = new j a v a x . swing .
JMenuItem ( ) ;
s t a t u s P a n e l = new j a v a x . swing . JPanel ( ) ;
j a v a x . swing . J S ep a r a to r s t a t u s P a n e l S e p a r a t o r = new j a v a x .
swing . J S ep a r a to r ( ) ;
statusMessageLabel = new j a v a x . swing . JLabel ( ) ;
s t a t u s A n i m a t i o n L a b e l = new j a v a x . swing . JLabel ( ) ;
progressBar = new j a v a x . swing . JProgressBar ( ) ;
mainPanel . setName ( " mainPanel " ) ; / / NOI18N
jToolBar1 . s e t R o l l o v e r ( true ) ;
j T o o l B a r 1 . setName ( " j T o o l B a r 1 " ) ; / / NOI18N
j a v a x . swing . ActionMap actionMap = org . j d e s k t o p . a p p l i c a t i o n
. Application . getInstance ( s c e l t a f i l e . SceltaFileApp .
class ) . g e t C o n t e x t ( ) . getActionMap ( S c e l t a F i l e V i e w . class ,
this ) ;
j B u t t o n 1 . s e t A c t i o n ( actionMap . g e t ( " Salva " ) ) ; / / NOI18N
org . j d e s k t o p . a p p l i c a t i o n . ResourceMap resourceMap = org .
jdesktop . application . Application . getInstance (
s c e l t a f i l e . S c e l t a F i l e A p p . class ) . g e t C o n t e x t ( ) .
getResourceMap ( S c e l t a F i l e V i e w . class ) ;
j B u t t o n 1 . s e t I c o n ( resourceMap . g e t I c o n ( " j B u t t o n 1 . i c o n " ) ) ; / /
NOI18N
j B u t t o n 1 . s e t T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 1 . t e x t " ) ) ;
/ / NOI18N
j B u t t o n 1 . s e t T o o l T i p T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 1 .
t o o l T i p T e x t " ) ) ; / / NOI18N
j B u t t o n 1 . setFocusable ( f a l s e ) ;
j B u t t o n 1 . s e t H o r i z o n t a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .CENTER) ;
j B u t t o n 1 . setName ( " j B u t t o n 1 " ) ; / / NOI18N
j B u t t o n 1 . s e t V e r t i c a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .BOTTOM) ;
j T o o l B a r 1 . add ( j B u t t o n 1 ) ;
j B u t t o n 2 . s e t A c t i o n ( actionMap . g e t ( " A p r i F i l e " ) ) ; / / NOI18N
j B u t t o n 2 . s e t I c o n ( resourceMap . g e t I c o n ( " j B u t t o n 2 . i c o n " ) ) ; / /
NOI18N
j B u t t o n 2 . s e t T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 2 . t e x t " ) ) ;
/ / NOI18N
j B u t t o n 2 . setFocusable ( f a l s e ) ;
j B u t t o n 2 . s e t H o r i z o n t a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .CENTER) ;
j B u t t o n 2 . setName ( " j B u t t o n 2 " ) ; / / NOI18N
jButton2 . setVerifyInputWhenFocusTarget ( false ) ;
j B u t t o n 2 . s e t V e r t i c a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .BOTTOM) ;
j T o o l B a r 1 . add ( j B u t t o n 2 ) ;
20
1. APPLICAZIONI
j T e x t F i e l d 1 . setName ( " j T e x t F i e l d 1 " ) ; / / NOI18N
org . j d e s k t o p . l a y o u t . GroupLayout mainPanelLayout = new org .
j d e s k t o p . l a y o u t . GroupLayout ( mainPanel ) ;
mainPanel . s e t L a y o u t ( mainPanelLayout ) ;
mainPanelLayout . s e t H o r i z o n t a l G r o u p (
mainPanelLayout . c r e a t e P a r a l l e l G r o u p ( org . j d e s k t o p .
l a y o u t . GroupLayout . LEADING )
. add ( j T o o l B a r 1 , org . j d e s k t o p . l a y o u t . GroupLayout .
DEFAULT_SIZE , 400 , S h o r t . MAX_VALUE)
. add ( mainPanelLayout . c r e a t e S e q u e n t i a l G r o u p ( )
. add ( 4 3 , 43 , 43)
. add ( j T e x t F i e l d 1 , org . j d e s k t o p . l a y o u t . GroupLayout .
PREFERRED_SIZE, 311 , org . j d e s k t o p . l a y o u t .
GroupLayout . PREFERRED_SIZE)
. addContainerGap ( ) )
);
mainPanelLayout . s e t V e r t i c a l G r o u p (
mainPanelLayout . c r e a t e P a r a l l e l G r o u p ( org . j d e s k t o p .
l a y o u t . GroupLayout . LEADING )
. add ( mainPanelLayout . c r e a t e S e q u e n t i a l G r o u p ( )
. add ( j T o o l B a r 1 , org . j d e s k t o p . l a y o u t . GroupLayout .
PREFERRED_SIZE, 29 , org . j d e s k t o p . l a y o u t .
GroupLayout . PREFERRED_SIZE)
. add ( 6 3 , 63 , 63)
. add ( j T e x t F i e l d 1 , org . j d e s k t o p . l a y o u t . GroupLayout .
PREFERRED_SIZE, org . j d e s k t o p . l a y o u t .
GroupLayout . DEFAULT_SIZE , org . j d e s k t o p . l a y o u t .
GroupLayout . PREFERRED_SIZE)
. addContainerGap ( 1 8 6 , S h o r t . MAX_VALUE) )
);
menuBar . setName ( " menuBar " ) ; / / NOI18N
f i l e M e n u . s e t T e x t ( resourceMap . g e t S t r i n g ( " f i l e M e n u . t e x t " ) ) ;
/ / NOI18N
fileMenu . s e t A u t o s c r o l l s ( true ) ;
f i l e M e n u . setName ( " f i l e M e n u " ) ; / / NOI18N
jMenuItem1 . s e t A c t i o n ( actionMap . g e t ( " Salva " ) ) ; / / NOI18N
jMenuItem1 . s e t A c c e l e r a t o r ( j a v a x . swing . KeyStroke .
getKeyStroke ( j a v a . awt . event . KeyEvent . VK_S , j a v a . awt .
event . I n p u t E v e n t . CTRL_MASK) ) ;
jMenuItem1 . s e t I c o n ( resourceMap . g e t I c o n ( " jMenuItem1 . i c o n " ) )
; / / NOI18N
jMenuItem1 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem1 . t e x t "
) ) ; / / NOI18N
jMenuItem1 . setActionCommand ( resourceMap . g e t S t r i n g ( "
jMenuItem1 . actionCommand " ) ) ; / / NOI18N
jMenuItem1 . setName ( " jMenuItem1 " ) ; / / NOI18N
f i l e M e n u . add ( jMenuItem1 ) ;
jMenuItem2 . s e t A c t i o n ( actionMap . g e t ( " A p r i F i l e " ) ) ; / / NOI18N
jMenuItem2 . s e t I c o n ( resourceMap . g e t I c o n ( " jMenuItem2 . i c o n " ) )
; / / NOI18N
jMenuItem2 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem2 . t e x t "
1.1. FILE CHOOSER
21
f i l e M e n u . add ( jMenuItem2 ) ;
exitMenuItem . s e t A c t i o n ( actionMap . g e t ( " q u i t " ) ) ; / / NOI18N
exitMenuItem . setName ( " exitMenuItem " ) ; / / NOI18N
f i l e M e n u . add ( exitMenuItem ) ;
menuBar . add ( f i l e M e n u ) ;
helpMenu . s e t T e x t ( resourceMap . g e t S t r i n g ( " helpMenu . t e x t " ) ) ;
/ / NOI18N
helpMenu . setName ( " helpMenu " ) ; / / NOI18N
aboutMenuItem . s e t A c t i o n ( actionMap . g e t ( " showAboutBox " ) ) ; / /
NOI18N
aboutMenuItem . setActionCommand ( resourceMap . g e t S t r i n g ( "
aboutMenuItem . actionCommand " ) ) ; / / NOI18N
aboutMenuItem . setName ( " aboutMenuItem " ) ; / / NOI18N
helpMenu . add ( aboutMenuItem ) ;
menuBar . add ( helpMenu ) ;
s t a t u s P a n e l . setName ( " s t a t u s P a n e l " ) ; / / NOI18N
s t a t u s P a n e l S e p a r a t o r . setName ( " s t a t u s P a n e l S e p a r a t o r " ) ; / /
NOI18N
statusMessageLabel . setName ( " statusMessageLabel " ) ; / /
NOI18N
s t a t u s A n i m a t i o n L a b e l . s e t H o r i z o n t a l A l i g n m e n t ( j a v a x . swing .
SwingConstants . LEFT ) ;
s t a t u s A n i m a t i o n L a b e l . setName ( " s t a t u s A n i m a t i o n L a b e l " ) ; / /
NOI18N
progressBar . setName ( " progressBar " ) ; / / NOI18N
org . j d e s k t o p . l a y o u t . GroupLayout s t a t u s P a n e l L a y o u t = new
org . j d e s k t o p . l a y o u t . GroupLayout ( s t a t u s P a n e l ) ;
statusPanel . setLayout ( statusPanelLayout ) ;
statusPanelLayout . setHorizontalGroup (
s t a t u s P a n e l L a y o u t . c r e a t e P a r a l l e l G r o u p ( org . j d e s k t o p .
l a y o u t . GroupLayout . LEADING )
. add ( s t a t u s P a n e l S e p a r a t o r , org . j d e s k t o p . l a y o u t .
GroupLayout . DEFAULT_SIZE , 400 , S h o r t . MAX_VALUE)
. add ( s t a t u s P a n e l L a y o u t . c r e a t e S e q u e n t i a l G r o u p ( )
. addContainerGap ( )
. add ( statusMessageLabel )
. addPreferredGap ( org . j d e s k t o p . l a y o u t . L a y o u t S t y l e .
RELATED, 230 , S h o r t . MAX_VALUE)
. add ( progressBar , org . j d e s k t o p . l a y o u t . GroupLayout .
PREFERRED_SIZE, org . j d e s k t o p . l a y o u t .
GroupLayout . DEFAULT_SIZE , org . j d e s k t o p . l a y o u t .
GroupLayout . PREFERRED_SIZE)
. addPreferredGap ( org . j d e s k t o p . l a y o u t . L a y o u t S t y l e .
RELATED)
22
1. APPLICAZIONI
. add ( s t a t u s A n i m a t i o n L a b e l )
. addContainerGap ( ) )
);
statusPanelLayout . setVerticalGroup (
s t a t u s P a n e l L a y o u t . c r e a t e P a r a l l e l G r o u p ( org . j d e s k t o p .
l a y o u t . GroupLayout . LEADING )
. add ( s t a t u s P a n e l L a y o u t . c r e a t e S e q u e n t i a l G r o u p ( )
. add ( s t a t u s P a n e l S e p a r a t o r , org . j d e s k t o p . l a y o u t .
GroupLayout . PREFERRED_SIZE, 2 , org . j d e s k t o p .
l a y o u t . GroupLayout . PREFERRED_SIZE)
. addPreferredGap ( org . j d e s k t o p . l a y o u t . L a y o u t S t y l e .
RELATED, org . j d e s k t o p . l a y o u t . GroupLayout .
DEFAULT_SIZE , S h o r t . MAX_VALUE)
. add ( s t a t u s P a n e l L a y o u t . c r e a t e P a r a l l e l G r o u p ( org .
j d e s k t o p . l a y o u t . GroupLayout . BASELINE )
. add ( statusMessageLabel )
. add ( s t a t u s A n i m a t i o n L a b e l )
. add ( progressBar , org . j d e s k t o p . l a y o u t .
GroupLayout . PREFERRED_SIZE, org . j d e s k t o p .
l a y o u t . GroupLayout . DEFAULT_SIZE , org .
j d e s k t o p . l a y o u t . GroupLayout . PREFERRED_SIZE
))
. add ( 3 , 3 , 3 ) )
);
setComponent ( mainPanel ) ;
setMenuBar ( menuBar ) ;
setStatusBar ( statusPanel ) ;
} / / </ e d i t o r −f o l d >
@Action
public void Salva ( ) {
S t r i n g testo = j T e x t F i e l d 1 . getText ( ) ;
S t r i n g ch ;
JFrame mainFrame = S c e l t a F i l e A p p . g e t A p p l i c a t i o n ( ) .
getMainFrame ( ) ;
J F il e C ho o s e r chooser = new J F il e C ho o s e r ( ) ;
i n t r e t u r n V a l = chooser . showSaveDialog ( mainFrame ) ;
i n t opzione = JOptionPane . YES_OPTION ;
S t r i n g messaggio = " I l f i l e s c e l t o e s i s t e già , v u o i
sovrascriverlo?" ;
S t r i n g english_message = " The chosen f i l e a l r e a d y
e x i s t s , you want t o o v e r w r i t e i t ? " ;
String t i t o l o = " Attenzione ! ! ! ! !
Warning ! ! ! ! ! " ;
i f ( r e t u r n V a l == J F i le C h o os e r . APPROVE_OPTION) {
ch = chooser . g e t S e l e c t e d F i l e ( ) . getPath ( ) ;
F i l e f c h = new F i l e ( ch ) ;
i f ( fch . i s F i l e ( ) ) {
opzione = JOptionPane . showConfirmDialog (
mainFrame , messaggio + " \ n " +
english_message , t i t o l o , JOptionPane .
YES_NO_OPTION) ;
}
i f ( opzione == JOptionPane . YES_OPTION) {
1.1. FILE CHOOSER
23
try {
F i l e O u t p u t S t r e a m f = new F i l e O u t p u t S t r e a m (
fch ) ;
ObjectOutputStream s = new
ObjectOutputStream ( f ) ;
s . writeObject ( testo ) ;
s . close ( ) ;
} catch ( IOException e ) {
JOptionPane . showMessageDialog ( mainPanel , "
Non è s t a t o p o s s i b i l e s a l v a r e " ) ;
}
}
}
i f ( opzione == JOptionPane . NO_OPTION) {
Salva ( ) ;
}
}
@Action
public void A p r i F i l e ( ) {
S t r i n g ch ;
String l i s t a = null ;
JFrame mainFrame = S c e l t a F i l e A p p . g e t A p p l i c a t i o n ( ) .
getMainFrame ( ) ;
J F il e C ho o s e r chooser = new J F il e C ho o s e r ( ) ;
i n t r e t u r n V a l = chooser . showOpenDialog ( mainFrame ) ;
i f ( r e t u r n V a l == J F i le C h o os e r . APPROVE_OPTION) {
ch = chooser . g e t S e l e c t e d F i l e ( ) . getPath ( ) ;
try {
F i l e I n p u t S t r e a m f = new F i l e I n p u t S t r e a m (new F i l e (
ch ) ) ;
O b j e c t I n p u t S t r e a m s = new O b j e c t I n p u t S t r e a m ( f ) ;
try {
l i s t a = ( S t r i n g ) s . r e a d Ob j e c t ( ) ;
} catch ( ClassNotFoundException ex ) {
Logger . getLogger ( S c e l t a F i l e V i e w . class . getName
( ) ) . l o g ( L e v e l . SEVERE, n u l l , ex ) ;
JOptionPane . showMessageDialog ( mainPanel , " I
d a t i c a r i c a t i non sono c o m p a t i b i l i con i l
programma " ) ;
}
s . close ( ) ;
} catch ( IOException e ) {
JOptionPane . showMessageDialog ( mainPanel , " Qualche
cosa non va n e l l a s c e l t a d e l f i l e " ) ;
}
i f ( l i s t a instanceof S t r i n g ) {
24
1. APPLICAZIONI
1.2. Grafica 2D
In questo esempio vogliamo illustrare un’applicazione che consente
di disegnare sullo schermo rette, ellissi, poligoni, immagini e testo
visto come elemento grafico. Cominciamo con impostare un progetto
usando la solita sequenza File -> New Project... -> Java -> JavaApplication
chiamiamo questo progetto JavaGrafica .
Il file più interessante sarà ovviamente JavaGraficaView.java andando nella modalità Design allarghiamo un po’ la finestra della
nostra applicazione , inseriamo una toolbar nella quale mettiamo
cinque pulsanti, quattro dei quali li dotiamo di icona e togliamo il
testo, mentre il quinto lasciamo solo il testo perché la sua azione
sarà quella di far apparire del testo sullo schermo.
Nella parte rimanente della finestra inseriamo un contenitore di tipo
JScrollPane , al suo interno inseriremo l’area su cui disegnare , che
va opportunamente preparata, quest’area sarà molto più grande di
JScrollPane1 , questo è il nome che abbiamo dato a questo contenitore, ma le sbarre di scorrimento del suo contenitore ci consentiranno di esplorarla tutta.
Il passo successivo è quello di preparare l’area su cui disegnare, ma
prima diamo un’occhiata a una finestra di NetBeans che ho appena
scoperto, si tratta della finestra Inspector , se non è già presente
sul desktop potete raggiungerla seguendo il percorso indicato nella
figura seguente:
1.2. GRAFICA 2D
25
F IGURA 1.2.1. Window->Navigating->Inspector
In questa finestra è riportato l’albero di tutti i contenitori del progetto
con i componenti contenuti in essi, è così facile vedere dove sono
finiti i componenti che avete immesso perché dalla vista Design non
sempre si ha la percezione precisa dove essi siano finiti. Nella figura
sotto riportata è rappresentato l’albero dei contenitori con i relativi
componenti usato nel nostro progetto.
26
1. APPLICAZIONI
F IGURA 1.2.2. Albero
Il prossimo compito è quello di dare funzionalità a ciascuno dei bottoni introdotti. Il metodo classico per dare una funzionalità a un bottone è quello di selezionare il componente, fare click con il tasto
destro, nel pop up che appare selezionare Properties , selezionare
la scheda Event e successivamente selezionare actionPerformed ,
viene costruito il metodo actionPerformed relativo a quel pulsante
passando al metodo il parametro Event e che rapp resenta l’evento
da gestire, ogni volta che si preme il bottone viene richiamato questo
metodo ed eseguito il codice in esso contenuto.
Un secondo metodo per gestire la pressione è quello di impostare
la voce action della finestra Properties del bottone, vedi la figura
sottostante
F IGURA 1.2.3. Action
1.2. GRAFICA 2D
27
Facendo click sul tastino
si apre la seguente finestra di dialogo :
F IGURA 1.2.4. Action - 2
Alla voce Action facciamo click sull’apposita linguetta e si apre un
menu a discesa nel quale scegliamo Create New Action , come
indicato in figura, date un nome alla nuova azione , stabilite una
sequenza di tasti per richiamare l’azione e se volete associatele
un’icona 1
1
Per disegnare le icone e altri elementi grafici, come ad esempio molte delle
figure del presente articolo, ho usato un ottimo programma di grafica vettoriale
28
1. APPLICAZIONI
Alla fine di tutte queste scelte l’aspetto della finestra di dialogo sarà
il seguente:
F IGURA 1.2.5. Finestra di dialogo per la scelta dell’azione
L’azione appena costruita si può associare ad altri componenti come
ad esempio una voce del menù. Ripetiamo questa procedura per
completamente libero , rilasciato con licenza GNU GENERAL PUBLIC LICENSE,
si tratta di Inkscape [Inkscape - grafica vettoriale], si può scaricare dal seguente
URL http://inkscape.org/download/?lang=it
1.2. GRAFICA 2D
29
ognuno dei pulsanti inseriti, alla fine avremo cinque pulsanti a ciascuna dei quali corrisponderà una azione, come possiamo vedere
nel codice sorgente della classe, per ora queste azioni sono vuote,
prima di riempirle replichiamo queste cinque azioni in un menu a
discesa come rappresentato nella figura seguente:
F IGURA 1.2.6. Menu forme
Diamo per scontato che si sappia inserire un menu e le sue voci,
l’unica cosa che sottolineiamo è che per ciascuna voce, dopo averla inserita, si deve andare sulla corrispondente finestra properties
scegliere action , facendo click su
settare sulla corrispondente
azione scelta dal menu a discesa che compare (vedere la figura
seguente) , automaticamente sia il nome che la combinazione di tasti
di scelta rapida e l’icona vengono impostati.
F IGURA 1.2.7. Scelta dell’azione
Il passo successivo è quello di programmare le azioni dei vari pulsanti, essi devono disegnare qualche cosa sullo schermo, per disegnare
bisogna creare una nuova classe che estenda la classe JPanel ,
30
1. APPLICAZIONI
questa nuova classe l’abbiamo chiamata FoglioDisegno , per costruirla usiamo la solita tecnica cioè mediante la sequenza :
File -> New File -> Java -> Java Class
Dopo aver costruito lo scheletro della classe facciamo l’override del
metodo paintComponent 2
F IGURA 1.2.8. Override di PaintComponent della
classe JPanel
La riga di codice
super.paintComponent(g);
viene aggiunta automaticamente e richiama l’analogo metodo della
classe super di quella attuale. Molto importante è la seconda riga di
codice:
Graphics2D gg = (Graphics2D) g;
questa riga effettua il cast dell’oggetto g, da Graphics a Graphics2D. La classe Graphics2D è una classe che estende la classe
Graphics che è la classe utilizzata per disegnare su un pannello.
L’oggetto g, passato come parametro nel metodo paintComponent
è già un oggetto Graphics2D, ma viene passato come oggetto Graphics quindi è necessario convertirlo. La classe offre un mucchio di
opportunità (vedere la relativa documentazione [Documentazione Java]
) noi useremo e descriveremo solo alcune di queste proprietà.
Le tre righe successive:
2
Per ottenere l’override di un metodo bisogna fare click con il taso destro e poi
scegliere Insert Code
1.2. GRAFICA 2D
31
F IGURA 1.2.9. Rendering Hints
sono quelle che ci consentono di disegnare con la procedura antialiasing
impostata, quindi le figure avranno dei contorni più definiti, l’oggetto rh servirà poi in seguito per dire all’oggetto gg (oggetto di tipo
Graphics2D) come comportarsi.
Successivamente abbiamo la riga di codice
BasicStroke tratto = new BasicStroke(8);
che costruisce l’oggetto tratto di tipo BasicStroke , questa classe
definisce come l’oggetto gg di tipo graphics2D deve disegnare le linee, tratto per ora cambia solo lo spessore della linea tutte le altre caratteristiche saranno standard, Per saperne di più sulla classe
BasicStroke vedere [Documentazione Java].
Le linee successive definiscono un oggetto di tipo TextLayout che
sarà stampato sul pannello e riprodurrà una frase stampata con
caratteri definiti dall’oggetto font
F IGURA 1.2.10. Oggetto di tipo TextLayout
32
1. APPLICAZIONI
La successiva parte di codice :
Font f = new Font ( " Verdana " , Font . ITALIC+Font . BOLD, 24) ;
gg . s e t R e n d e r i n g H i n t s ( r t ) ;
FontRenderContext f r c = gg . getFontRenderContext ( ) ;
TextLayout s = new TextLayout ( " Frase d i c i r c o s t a n z a " , f ,
frc ) ;
E l l i p s e 2 D . Double c e r c h i o = new E l l i p s e 2 D . Double ( 1 0 0 , 10 ,
1000 , 100) ;
Rectangle2D . Double r e t t a n g o l o = new Rectangle2D . Double
( 1 0 0 , 100 , 310 , 310) ;
G r a d i e n t P a i n t g r a d i e n t e = new G r a d i e n t P a i n t ( 5 0 . 0 f , 180.0 f ,
C o l o r . red ,
400.0 f , 180.0 f , C o l o r . green ) ;
i n t x 1 P o i n t s [ ] = { 0 , 100 , 0 , 1 0 0 } ;
i n t y 1 P o i n t s [ ] = { 0 , 50 , 50 , 0 } ;
GeneralPath polygon =
new GeneralPath ( GeneralPath .WIND_EVEN_ODD,
x1Points . length ) ;
polygon . moveTo ( x 1 P o i n t s [ 0 ] , y 1 P o i n t s [ 0 ] ) ;
f o r ( i n t i n d e x = 1 ; i n d e x < x 1 P o i n t s . l e n g t h ; i n d e x ++) {
polygon . l i n e T o ( x 1 P o i n t s [ i n d e x ] , y 1 P o i n t s [ i n d e x ] ) ;
}
polygon . c l os e P a t h ( ) ;
definisce delle figure che verranno poi stampate, per saperne di più
su queste classi vedere la documentazione in linea [Documentazione Java].
Infine vi è la parte di codice che effettua effettivamente le operazioni
di disegno in base al tasto premuto, è stata introdotta la proprietà
scelta che assume valori diversi a seconda dei tasti premuti ed è impostata dalle varie azioni definite nella classe JavaGraficaView le
quali non faranno altro che impostare questa proprietà e richiamare
il metodo updateUI() della classe FoglioDisegno .
Seguono i listati completi della classe FoglioDisegno e della classe
JavaGraficaView .
1.2. GRAFICA 2D
1.2.1. FoglioDisegno.java.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/∗
∗ To change t h i s template , choose Tools | Templates
∗ and open t h e t e m p l a t e i n t h e e d i t o r .
∗/
package j a v a g r a f i c a ;
import j a v a . awt . B a s i c S t r o k e ;
import j a v a . awt . Graphics ;
/∗ ∗
∗
∗ @author p a p i
∗/
import
import
import
import
import
import
import
import
import
import
import
import
j a v a . awt . C o l o r ;
j a v a . awt . Font ;
j a v a . awt . G r a d i e n t P a i n t ;
j a v a . awt . Graphics ;
j a v a . awt . Graphics2D ;
j a v a . awt . Image ;
j a v a . awt . geom . E l l i p s e 2 D ;
j a v a . awt . R e nd er i ng Hi n ts ;
j a v a . awt . T o o l k i t ;
j a v a . awt . geom . GeneralPath ;
j a v a . awt . geom . Rectangle2D ;
j a v a . awt . f o n t . ∗ ;
public class F og l io D is e gn o extends j a v a x . swing . JPanel {
@Override
protected void paintComponent ( Graphics g ) {
super . paintComponent ( g ) ;
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1. APPLICAZIONI
Graphics2D gg = ( Graphics2D ) g ;
Re n de ri n gH in t s r h = new R en de r in gH i nt s (
Re n de ri n gH in t s . KEY_ANTIALIASING ,
Re n de ri n gH in t s . VALUE_ANTIALIAS_ON ) ;
Re n de ri n gH in t s r t = new R en de r in gH i nt s (
Re n de ri n gH in t s . KEY_TEXT_ANTIALIASING ,
Re n de ri n gH in t s . VALUE_TEXT_ANTIALIAS_ON ) ;
B a s i c S t r o k e t r a t t o = new B a s i c S t r o k e ( 8 ) ;
Font f = new Font ( " Verdana " , Font . ITALIC+Font . BOLD, 24) ;
gg . s e t R e n d e r i n g H i n t s ( r t ) ;
FontRenderContext f r c = gg . getFontRenderContext ( ) ;
TextLayout s = new TextLayout ( " Frase d i c i r c o s t a n z a " , f ,
frc ) ;
E l l i p s e 2 D . Double c e r c h i o = new E l l i p s e 2 D . Double ( 1 0 0 , 10 ,
1000 , 100) ;
Rectangle2D . Double r e t t a n g o l o = new Rectangle2D . Double
( 1 0 0 , 100 , 310 , 310) ;
G r a d i e n t P a i n t g r a d i e n t e = new G r a d i e n t P a i n t ( 5 0 . 0 f , 180.0 f ,
C o l o r . red ,
400.0 f , 180.0 f , C o l o r . green ) ;
i n t x 1 P o i n t s [ ] = { 0 , 100 , 0 , 1 0 0 } ;
i n t y 1 P o i n t s [ ] = { 0 , 50 , 50 , 0 } ;
GeneralPath polygon =
new GeneralPath ( GeneralPath .WIND_EVEN_ODD,
x1Points . length ) ;
polygon . moveTo ( x 1 P o i n t s [ 0 ] , y 1 P o i n t s [ 0 ] ) ;
f o r ( i n t i n d e x = 1 ; i n d e x < x 1 P o i n t s . l e n g t h ; i n d e x ++) {
polygon . l i n e T o ( x 1 P o i n t s [ i n d e x ] , y 1 P o i n t s [ i n d e x ] ) ;
}
1.2. GRAFICA 2D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
polygon . c l o s e Pa t h ( ) ;
//
//
//
//
//
//
gg . s e t C o l o r ( C o l o r . red ) ;
gg . draw ( c e r c h i o ) ;
gg . s e t C o l o r ( C o l o r .GREEN) ;
gg . draw ( polygon ) ;
gg . s e t C o l o r ( C o l o r . BLUE) ;
gg . draw ( r e t t a n g o l o ) ;
switch ( s c e l t a ) {
case 1 : {
gg . s e t R e n d e r i n g H i n t s ( r h ) ;
gg . s e t C o l o r ( C o l o r . red ) ;
gg . s e t S t r o k e ( t r a t t o ) ;
gg . draw ( c e r c h i o ) ;
gg . s e t C o l o r ( C o l o r . y e l l o w ) ;
gg . f i l l ( c e r c h i o ) ;
}
break ;
case 2 : {
gg . s e t R e n d e r i n g H i n t s ( r h ) ;
gg . s e t C o l o r ( C o l o r .GREEN) ;
gg . draw ( polygon ) ;
}
break ;
case 3 : {
gg . s e t R e n d e r i n g H i n t s ( r h ) ;
gg . s e t C o l o r ( C o l o r . BLUE) ;
gg . draw ( r e t t a n g o l o ) ;
gg . s e t P a i n t ( g r a d i e n t e ) ;
gg . f i l l ( r e t t a n g o l o ) ;
35
36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1. APPLICAZIONI
}
break ;
case 4 : {
gg . s e t R e n d e r i n g H i n t s ( r t ) ;
s . draw ( gg , 150 , 50) ;
}
break ;
case 5 : {
Toolkit t o o l k i t = Toolkit . getDefaultToolkit () ;
Image immagine = t o o l k i t . getImage ( img ) ;
gg . drawImage ( immagine , 100 , 100 , t h i s ) ;
}
break ;
default : {
gg . s e t R e n d e r i n g H i n t s ( r t ) ;
gg . d r a w S t r i n g ( " Nessuna s e l e z i o n e " , 20 , 20) ;
}
}
}
private int scelta = 0;
/∗ ∗
∗ Get t h e
∗
∗ @return
∗/
public i n t
return
}
value of s c e l t a
the value of s c e l t a
getScelta ( ) {
scelta ;
1.2. GRAFICA 2D
1.2.2. JavaGraficaView.java.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/∗
∗ JavaGraficaView . j a v a
∗/
package j a v a g r a f i c a ;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
org . j d e s k t o p . a p p l i c a t i o n . A c t i o n ;
org . j d e s k t o p . a p p l i c a t i o n . ResourceMap ;
org . j d e s k t o p . a p p l i c a t i o n . S i n g l e F r a m e A p p l i c a t i o n ;
org . j d e s k t o p . a p p l i c a t i o n . FrameView ;
org . j d e s k t o p . a p p l i c a t i o n . TaskMonitor ;
j a v a . awt . event . A c t i o n E v e n t ;
j a v a . awt . event . A c t i o n L i s t e n e r ;
j a v a x . swing . Timer ;
j a v a x . swing . I c o n ;
j a v a x . swing . J D i a l o g ;
j a v a x . swing . J Fi l e Ch o o s er ;
j a v a x . swing . JFrame ;
j a v a x . swing . JOptionPane ;
j a v a x . swing . f i l e c h o o s e r . F i l e N a m e E x t e n s i o n F i l t e r ;
/∗ ∗
∗ The a p p l i c a t i o n ’ s main frame .
∗/
public class JavaGraficaView extends FrameView {
public JavaGraficaView ( S i n g l e F r a m e A p p l i c a t i o n app ) {
super ( app ) ;
initComponents ( ) ;
/ / s t a t u s bar i n i t i a l i z a t i o n − message t i m e o u t , i d l e i c o n
and busy animation , e t c
ResourceMap resourceMap = getResourceMap ( ) ;
i n t messageTimeout = resourceMap . g e t I n t e g e r ( " S t a t u s B a r .
messageTimeout " ) ;
messageTimer = new Timer ( messageTimeout , new
ActionListener () {
37
38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1. APPLICAZIONI
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {
statusMessageLabel . s e t T e x t ( " " ) ;
}
}) ;
messageTimer . setRepeats ( f a l s e ) ;
i n t busyAnimationRate = resourceMap . g e t I n t e g e r ( " S t a t u s Ba r .
busyAnimationRate " ) ;
f o r ( i n t i = 0 ; i < busyIcons . l e n g t h ; i ++) {
busyIcons [ i ] = resourceMap . g e t I c o n ( " S t a t u s B ar .
busyIcons [ " + i + " ] " ) ;
}
busyIconTimer = new Timer ( busyAnimationRate , new
ActionListener () {
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {
busyIconIndex = ( busyIconIndex + 1 ) % busyIcons .
length ;
s t a t u s A n i m a t i o n L a b e l . s e t I c o n ( busyIcons [
busyIconIndex ] ) ;
}
}) ;
i d l e I c o n = resourceMap . g e t I c o n ( " S t a t u s B a r . i d l e I c o n " ) ;
statusAnimationLabel . setIcon ( id le Ico n ) ;
progressBar . s e t V i s i b l e ( f a l s e ) ;
/ / c o n n e c t i n g a c t i o n t a s k s t o s t a t u s bar v i a TaskMonitor
TaskMonitor t a s k M o n i t o r = new TaskMonitor ( g e t A p p l i c a t i o n ( )
. getContext ( ) ) ;
t a s k M o n i t o r . addPropertyChangeListener (new j a v a . beans .
PropertyChangeListener ( ) {
public void propertyChange ( j a v a . beans .
PropertyChangeEvent e v t ) {
S t r i n g propertyName = e v t . getPropertyName ( ) ;
i f ( " s t a r t e d " . equals ( propertyName ) ) {
i f ( ! busyIconTimer . isRunning ( ) ) {
s t a t u s A n i m a t i o n L a b e l . s e t I c o n ( busyIcons [ 0 ] )
;
busyIconIndex = 0 ;
busyIconTimer . s t a r t ( ) ;
}
progressBar . s e t V i s i b l e ( t r u e ) ;
progressBar . s e t I n d e t e r m i n a t e ( t r u e ) ;
} else i f ( " done " . equals ( propertyName ) ) {
busyIconTimer . s t o p ( ) ;
statusAnimationLabel . setIcon ( id le Ic on ) ;
1.2. GRAFICA 2D
progressBar . s e t V i s i b l e ( f a l s e ) ;
progressBar . s e t V a l u e ( 0 ) ;
} else i f ( " message " . equals ( propertyName ) ) {
S t r i n g t e x t = ( S t r i n g ) ( e v t . getNewValue ( ) ) ;
statusMessageLabel . s e t T e x t ( ( t e x t == n u l l ) ? " "
: text ) ;
messageTimer . r e s t a r t ( ) ;
} else i f ( " p r o g r e s s " . equals ( propertyName ) ) {
i n t v a l u e = ( I n t e g e r ) ( e v t . getNewValue ( ) ) ;
progressBar . s e t V i s i b l e ( t r u e ) ;
progressBar . s e t I n d e t e r m i n a t e ( f a l s e ) ;
progressBar . s e t V a l u e ( v a l u e ) ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
39
}
}) ;
}
@Action
public void showAboutBox ( ) {
i f ( aboutBox == n u l l ) {
JFrame mainFrame = JavaGraficaApp . g e t A p p l i c a t i o n ( ) .
getMainFrame ( ) ;
aboutBox = new JavaGraficaAboutBox ( mainFrame ) ;
aboutBox . s e t L o c a t i o n R e l a t i v e T o ( mainFrame ) ;
}
JavaGraficaApp . g e t A p p l i c a t i o n ( ) . show ( aboutBox ) ;
}
/ ∗ ∗ T h i s method i s c a l l e d from w i t h i n t h e c o n s t r u c t o r t o
∗ i n i t i a l i z e t h e form .
∗ WARNING: Do NOT modify t h i s code . The c o n t e n t o f t h i s
method i s
∗ always r eg e ne r at e d by t h e Form E d i t o r .
∗/
@SuppressWarnings ( " unchecked " )
/ / < e d i t o r −f o l d d e f a u l t s t a t e =" c o l l a p s e d " desc =" Generated Code
">
p r i v a t e void initComponents ( ) {
40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1. APPLICAZIONI
mainPanel = new j a v a x . swing . JPanel ( ) ;
j T o o l B a r 1 = new j a v a x . swing . JToolBar ( ) ;
j B u t t o n 1 = new j a v a x . swing . J B u t t o n ( ) ;
j B u t t o n 2 = new j a v a x . swing . J B u t t o n ( ) ;
j B u t t o n 3 = new j a v a x . swing . J B u t t o n ( ) ;
j B u t t o n 4 = new j a v a x . swing . J B u t t o n ( ) ;
j B u t t o n 5 = new j a v a x . swing . J B u t t o n ( ) ;
j S c r o l l P a n e 1 = new j a v a x . swing . J S c r o l l P a n e ( ) ;
f o g l i o D i s e g n o 1 = new j a v a g r a f i c a . Fo g li o Di se g no ( ) ;
menuBar = new j a v a x . swing . JMenuBar ( ) ;
j a v a x . swing . JMenu f i l e M e n u = new j a v a x . swing . JMenu ( ) ;
j a v a x . swing . JMenuItem exitMenuItem = new j a v a x . swing .
JMenuItem ( ) ;
jMenu1 = new j a v a x . swing . JMenu ( ) ;
jMenuItem1 = new j a v a x . swing . JMenuItem ( ) ;
jMenuItem2 = new j a v a x . swing . JMenuItem ( ) ;
jMenuItem3 = new j a v a x . swing . JMenuItem ( ) ;
jMenuItem4 = new j a v a x . swing . JMenuItem ( ) ;
jMenuItem5 = new j a v a x . swing . JMenuItem ( ) ;
j a v a x . swing . JMenu helpMenu = new j a v a x . swing . JMenu ( ) ;
j a v a x . swing . JMenuItem aboutMenuItem = new j a v a x . swing .
JMenuItem ( ) ;
s t a t u s P a n e l = new j a v a x . swing . JPanel ( ) ;
j a v a x . swing . J S e pa r a t or s t a t u s P a n e l S e p a r a t o r = new j a v a x .
swing . J S ep a r a to r ( ) ;
statusMessageLabel = new j a v a x . swing . JLabel ( ) ;
s t a t u s A n i m a t i o n L a b e l = new j a v a x . swing . JLabel ( ) ;
progressBar = new j a v a x . swing . JProgressBar ( ) ;
mainPanel . setName ( " mainPanel " ) ; / / NOI18N
jToolBar1 . s e t R o l l o v e r ( true ) ;
j T o o l B a r 1 . setName ( " j T o o l B a r 1 " ) ; / / NOI18N
j a v a x . swing . ActionMap actionMap = org . j d e s k t o p . a p p l i c a t i o n
. A p p l i c a t i o n . g e t I n s t a n c e ( j a v a g r a f i c a . JavaGraficaApp .
class ) . g e t C o n t e x t ( ) . getActionMap ( JavaGraficaView . class
, this ) ;
j B u t t o n 1 . s e t A c t i o n ( actionMap . g e t ( " e l l i s s e " ) ) ; / / NOI18N
org . j d e s k t o p . a p p l i c a t i o n . ResourceMap resourceMap = org .
jdesktop . application . Application . getInstance (
j a v a g r a f i c a . JavaGraficaApp . class ) . g e t C o n t e x t ( ) .
getResourceMap ( JavaGraficaView . class ) ;
j B u t t o n 1 . s e t T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 1 . t e x t " ) ) ;
/ / NOI18N
1.2. GRAFICA 2D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
41
j B u t t o n 1 . setFocusable ( f a l s e ) ;
j B u t t o n 1 . s e t H o r i z o n t a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .CENTER) ;
j B u t t o n 1 . setName ( " j B u t t o n 1 " ) ; / / NOI18N
j B u t t o n 1 . s e t V e r t i c a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .BOTTOM) ;
j T o o l B a r 1 . add ( j B u t t o n 1 ) ;
j B u t t o n 2 . s e t A c t i o n ( actionMap . g e t ( " t r a i e t t o r i a " ) ) ; / /
NOI18N
j B u t t o n 2 . s e t T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 2 . t e x t " ) ) ;
/ / NOI18N
j B u t t o n 2 . setFocusable ( f a l s e ) ;
j B u t t o n 2 . s e t H o r i z o n t a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .CENTER) ;
j B u t t o n 2 . setName ( " j B u t t o n 2 " ) ; / / NOI18N
j B u t t o n 2 . s e t V e r t i c a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .BOTTOM) ;
j T o o l B a r 1 . add ( j B u t t o n 2 ) ;
j B u t t o n 3 . s e t A c t i o n ( actionMap . g e t ( " r e t t a n g o l o " ) ) ; / / NOI18N
j B u t t o n 3 . s e t T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 3 . t e x t " ) ) ;
/ / NOI18N
j B u t t o n 3 . setFocusable ( f a l s e ) ;
j B u t t o n 3 . s e t H o r i z o n t a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .CENTER) ;
j B u t t o n 3 . setName ( " j B u t t o n 3 " ) ; / / NOI18N
j B u t t o n 3 . s e t V e r t i c a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .BOTTOM) ;
j T o o l B a r 1 . add ( j B u t t o n 3 ) ;
j B u t t o n 4 . s e t A c t i o n ( actionMap . g e t ( " t e s t o " ) ) ; / / NOI18N
j B u t t o n 4 . setFocusable ( f a l s e ) ;
j B u t t o n 4 . s e t H o r i z o n t a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .CENTER) ;
j B u t t o n 4 . setName ( " j B u t t o n 4 " ) ; / / NOI18N
j B u t t o n 4 . s e t V e r t i c a l T e x t P o s i t i o n ( j a v a x . swing .
SwingConstants .BOTTOM) ;
j T o o l B a r 1 . add ( j B u t t o n 4 ) ;
j B u t t o n 5 . s e t A c t i o n ( actionMap . g e t ( " immagine " ) ) ; / / NOI18N
j B u t t o n 5 . s e t T e x t ( resourceMap . g e t S t r i n g ( " j B u t t o n 5 . t e x t " ) ) ;
/ / NOI18N
j B u t t o n 5 . setName ( " j B u t t o n 5 " ) ; / / NOI18N
j T o o l B a r 1 . add ( j B u t t o n 5 ) ;
j S c r o l l P a n e 1 . setName ( " j S c r o l l P a n e 1 " ) ; / / NOI18N
42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1. APPLICAZIONI
j S c r o l l P a n e 1 . s e t P r e f e r r e d S i z e (new j a v a . awt . Dimension (2000 ,
2000) ) ;
f o g l i o D i s e g n o 1 . setName ( " f o g l i o D i s e g n o 1 " ) ; / / NOI18N
f o g l i o D i s e g n o 1 . s e t P r e f e r r e d S i z e (new j a v a . awt . Dimension
(2000 , 2000) ) ;
j a v a x . swing . GroupLayout f o g l i o D i s e g n o 1 L a y o u t = new j a v a x .
swing . GroupLayout ( f o g l i o D i s e g n o 1 ) ;
foglioDisegno1 . setLayout ( foglioDisegno1Layout ) ;
foglioDisegno1Layout . setHorizontalGroup (
f o g l i o D i s e g n o 1 L a y o u t . c r e a t e P a r a l l e l G r o u p ( j a v a x . swing .
GroupLayout . Alignment . LEADING )
. addGap ( 0 , 2000 , S h o r t . MAX_VALUE)
);
foglioDisegno1Layout . setVerticalGroup (
f o g l i o D i s e g n o 1 L a y o u t . c r e a t e P a r a l l e l G r o u p ( j a v a x . swing .
GroupLayout . Alignment . LEADING )
. addGap ( 0 , 2000 , S h o r t . MAX_VALUE)
);
j S c r o l l P a n e 1 . se t Vi ew po r tV ie w ( f o g l i o D i s e g n o 1 ) ;
j a v a x . swing . GroupLayout mainPanelLayout = new j a v a x . swing .
GroupLayout ( mainPanel ) ;
mainPanel . s e t L a y o u t ( mainPanelLayout ) ;
mainPanelLayout . s e t H o r i z o n t a l G r o u p (
mainPanelLayout . c r e a t e P a r a l l e l G r o u p ( j a v a x . swing .
GroupLayout . Alignment . LEADING )
. addGroup ( mainPanelLayout . c r e a t e S e q u e n t i a l G r o u p ( )
. addGroup ( mainPanelLayout . c r e a t e P a r a l l e l G r o u p (
j a v a x . swing . GroupLayout . Alignment . LEADING )
. addComponent ( j T o o l B a r 1 , j a v a x . swing .
GroupLayout . PREFERRED_SIZE, j a v a x . swing .
GroupLayout . DEFAULT_SIZE , j a v a x . swing .
GroupLayout . PREFERRED_SIZE)
. addGroup ( j a v a x . swing . GroupLayout . Alignment .
TRAILING , mainPanelLayout .
createSequentialGroup ( )
. addContainerGap ( )
. addComponent ( j S c r o l l P a n e 1 , j a v a x . swing .
GroupLayout . DEFAULT_SIZE , 685 , S h o r t .
MAX_VALUE) ) )
. addContainerGap ( ) )
);
mainPanelLayout . s e t V e r t i c a l G r o u p (
mainPanelLayout . c r e a t e P a r a l l e l G r o u p ( j a v a x . swing .
GroupLayout . Alignment . LEADING )
. addGroup ( mainPanelLayout . c r e a t e S e q u e n t i a l G r o u p ( )
. addComponent ( j T o o l B a r 1 , j a v a x . swing . GroupLayout .
PREFERRED_SIZE, j a v a x . swing . GroupLayout .
DEFAULT_SIZE , j a v a x . swing . GroupLayout .
PREFERRED_SIZE)
. addPreferredGap ( j a v a x . swing . L a y o u t S t y l e .
ComponentPlacement . RELATED)
1.2. GRAFICA 2D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
43
. addComponent ( j S c r o l l P a n e 1 , j a v a x . swing .
GroupLayout . DEFAULT_SIZE , 478 , S h o r t . MAX_VALUE
)
. addContainerGap ( ) )
);
menuBar . setName ( " menuBar " ) ; / / NOI18N
f i l e M e n u . s e t T e x t ( resourceMap . g e t S t r i n g ( " f i l e M e n u . t e x t " ) ) ;
/ / NOI18N
f i l e M e n u . setName ( " f i l e M e n u " ) ; / / NOI18N
exitMenuItem . s e t A c t i o n ( actionMap . g e t ( " q u i t " ) ) ; / / NOI18N
exitMenuItem . setName ( " exitMenuItem " ) ; / / NOI18N
f i l e M e n u . add ( exitMenuItem ) ;
menuBar . add ( f i l e M e n u ) ;
jMenu1 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenu1 . t e x t " ) ) ; / /
NOI18N
jMenu1 . setName ( " jMenu1 " ) ; / / NOI18N
jMenuItem1 . s e t A c t i o n ( actionMap . g e t ( " e l l i s s e " ) ) ; / / NOI18N
jMenuItem1 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem1 . t e x t "
) ) ; / / NOI18N
jMenuItem1 . setName ( " jMenuItem1 " ) ; / / NOI18N
jMenu1 . add ( jMenuItem1 ) ;
jMenuItem2 . s e t A c t i o n ( actionMap . g e t ( " t r a i e t t o r i a " ) ) ; / /
NOI18N
jMenuItem2 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem2 . t e x t "
) ) ; / / NOI18N
jMenuItem2 . setName ( " jMenuItem2 " ) ; / / NOI18N
jMenu1 . add ( jMenuItem2 ) ;
jMenuItem3 . s e t A c t i o n ( actionMap . g e t ( " r e t t a n g o l o " ) ) ; / /
NOI18N
jMenuItem3 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem3 . t e x t "
) ) ; / / NOI18N
jMenuItem3 . setName ( " jMenuItem3 " ) ; / / NOI18N
jMenu1 . add ( jMenuItem3 ) ;
jMenuItem4 . s e t A c t i o n ( actionMap . g e t ( " t e s t o " ) ) ; / / NOI18N
jMenuItem4 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem4 . t e x t "
) ) ; / / NOI18N
44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1. APPLICAZIONI
jMenuItem4 . setName ( " jMenuItem4 " ) ; / / NOI18N
jMenu1 . add ( jMenuItem4 ) ;
jMenuItem5 . s e t A c t i o n ( actionMap . g e t ( " immagine " ) ) ; / / NOI18N
jMenuItem5 . s e t T e x t ( resourceMap . g e t S t r i n g ( " jMenuItem5 . t e x t "
) ) ; / / NOI18N
jMenuItem5 . setName ( " jMenuItem5 " ) ; / / NOI18N
jMenu1 . add ( jMenuItem5 ) ;
menuBar . add ( jMenu1 ) ;
helpMenu . s e t T e x t ( resourceMap . g e t S t r i n g ( " helpMenu . t e x t " ) ) ;
/ / NOI18N
helpMenu . setName ( " helpMenu " ) ; / / NOI18N
aboutMenuItem . s e t A c t i o n ( actionMap . g e t ( " showAboutBox " ) ) ; / /
NOI18N
aboutMenuItem . setName ( " aboutMenuItem " ) ; / / NOI18N
helpMenu . add ( aboutMenuItem ) ;
menuBar . add ( helpMenu ) ;
s t a t u s P a n e l . setName ( " s t a t u s P a n e l " ) ; / / NOI18N
statusMessageLabel . setName ( " statusMessageLabel " ) ; / /
NOI18N
s t a t u s A n i m a t i o n L a b e l . s e t H o r i z o n t a l A l i g n m e n t ( j a v a x . swing .
SwingConstants . LEFT ) ;
s t a t u s A n i m a t i o n L a b e l . setName ( " s t a t u s A n i m a t i o n L a b e l " ) ; / /
NOI18N
progressBar . setName ( " progressBar " ) ; / / NOI18N
j a v a x . swing . GroupLayout s t a t u s P a n e l L a y o u t = new j a v a x .
swing . GroupLayout ( s t a t u s P a n e l ) ;
statusPanel . setLayout ( statusPanelLayout ) ;
statusPanelLayout . setHorizontalGroup (
s t a t u s P a n e l L a y o u t . c r e a t e P a r a l l e l G r o u p ( j a v a x . swing .
GroupLayout . Alignment . LEADING )
. addComponent ( s t a t u s P a n e l S e p a r a t o r , j a v a x . swing .
GroupLayout . DEFAULT_SIZE , 705 , S h o r t . MAX_VALUE)
. addGroup ( s t a t u s P a n e l L a y o u t . c r e a t e S e q u e n t i a l G r o u p ( )
. addContainerGap ( )
1.2. GRAFICA 2D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
45
. addComponent ( statusMessageLabel )
. addPreferredGap ( j a v a x . swing . L a y o u t S t y l e .
ComponentPlacement . RELATED, 535 , S h o r t .
MAX_VALUE)
. addComponent ( progressBar , j a v a x . swing . GroupLayout
. PREFERRED_SIZE, j a v a x . swing . GroupLayout .
DEFAULT_SIZE , j a v a x . swing . GroupLayout .
PREFERRED_SIZE)
. addPreferredGap ( j a v a x . swing . L a y o u t S t y l e .
ComponentPlacement . RELATED)
. addComponent ( s t a t u s A n i m a t i o n L a b e l )
. addContainerGap ( ) )
);
statusPanelLayout . setVerticalGroup (
s t a t u s P a n e l L a y o u t . c r e a t e P a r a l l e l G r o u p ( j a v a x . swing .
GroupLayout . Alignment . LEADING )
. addGroup ( s t a t u s P a n e l L a y o u t . c r e a t e S e q u e n t i a l G r o u p ( )
. addComponent ( s t a t u s P a n e l S e p a r a t o r , j a v a x . swing .
GroupLayout . PREFERRED_SIZE, 2 , j a v a x . swing .
GroupLayout . PREFERRED_SIZE)
. addPreferredGap ( j a v a x . swing . L a y o u t S t y l e .
ComponentPlacement . RELATED, j a v a x . swing .
GroupLayout . DEFAULT_SIZE , S h o r t . MAX_VALUE)
. addGroup ( s t a t u s P a n e l L a y o u t . c r e a t e P a r a l l e l G r o u p (
j a v a x . swing . GroupLayout . Alignment . BASELINE )
. addComponent ( statusMessageLabel )
. addComponent ( s t a t u s A n i m a t i o n L a b e l )
. addComponent ( progressBar , j a v a x . swing .
GroupLayout . PREFERRED_SIZE, j a v a x . swing .
GroupLayout . DEFAULT_SIZE , j a v a x . swing .
GroupLayout . PREFERRED_SIZE) )
. addGap ( 3 , 3 , 3 ) )
);
setComponent ( mainPanel ) ;
setMenuBar ( menuBar ) ;
setStatusBar ( statusPanel ) ;
} / / </ e d i t o r −f o l d >
@Action
public void e l l i s s e ( ) {
foglioDisegno1 . setScelta (1) ;
f o g l i o D i s e g n o 1 . updateUI ( ) ;
}
@Action
public void t r a i e t t o r i a ( ) {
foglioDisegno1 . setScelta (2) ;
f o g l i o D i s e g n o 1 . updateUI ( ) ;
46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1. APPLICAZIONI
}
@Action
public void r e t t a n g o l o ( ) {
foglioDisegno1 . setScelta (3) ;
f o g l i o D i s e g n o 1 . updateUI ( ) ;
}
@Action
public void t e s t o ( ) {
foglioDisegno1 . setScelta (4) ;
f o g l i o D i s e g n o 1 . updateUI ( ) ;
}
@Action
public void immagine ( ) {
S t r i n g ch ;
J F il e C ho o s er chooser =new J F il e C ho o s e r ( ) ;
F i l e N a m e E x t e n s i o n F i l t e r f i l t e r = new
FileNameExtensionFilter (
"JPG & GIF Images " , " j p g " , " g i f " ) ;
chooser . s e t F i l e F i l t e r ( f i l t e r ) ;
i n t r e t u r n V a l =chooser . showOpenDialog ( mainPanel ) ;
i f ( r e t u r n V a l == J F i le C h o os e r . APPROVE_OPTION) {
ch = chooser . g e t S e l e c t e d F i l e ( ) . getPath ( ) ;
f o g l i o D i s e g n o 1 . setImg ( ch ) ;
foglioDisegno1 . setScelta (5) ;
f o g l i o D i s e g n o 1 . updateUI ( ) ;
} else {
JOptionPane . showMessageDialog ( mainPanel , " Qualche
cosa è andato s t o r t o " ) ;
}
}
1.2. GRAFICA 2D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/ / V a r i a b l e s d e c l a r a t i o n − do n o t modify
p r i v a t e j a v a g r a f i c a . Fo g li o Di se g no f o g l i o D i s e g n o 1 ;
p r i v a t e j a v a x . swing . J B u t t o n j B u t t o n 1 ;
p r i v a t e j a v a x . swing . J B u t t o n j B u t t o n 2 ;
p r i v a t e j a v a x . swing . J B u t t o n j B u t t o n 3 ;
p r i v a t e j a v a x . swing . J B u t t o n j B u t t o n 4 ;
p r i v a t e j a v a x . swing . J B u t t o n j B u t t o n 5 ;
p r i v a t e j a v a x . swing . JMenu jMenu1 ;
p r i v a t e j a v a x . swing . JMenuItem jMenuItem1 ;
p r i v a t e j a v a x . swing . JMenuItem jMenuItem2 ;
p r i v a t e j a v a x . swing . JMenuItem jMenuItem3 ;
p r i v a t e j a v a x . swing . JMenuItem jMenuItem4 ;
p r i v a t e j a v a x . swing . JMenuItem jMenuItem5 ;
p r i v a t e j a v a x . swing . J S c r o l l P a n e j S c r o l l P a n e 1 ;
p r i v a t e j a v a x . swing . JToolBar j T o o l B a r 1 ;
p r i v a t e j a v a x . swing . JPanel mainPanel ;
p r i v a t e j a v a x . swing . JMenuBar menuBar ;
p r i v a t e j a v a x . swing . JProgressBar progressBar ;
p r i v a t e j a v a x . swing . JLabel s t a t u s A n i m a t i o n L a b e l ;
p r i v a t e j a v a x . swing . JLabel statusMessageLabel ;
p r i v a t e j a v a x . swing . JPanel s t a t u s P a n e l ;
/ / End o f v a r i a b l e s d e c l a r a t i o n
private
private
private
private
private
f i n a l Timer messageTimer ;
f i n a l Timer busyIconTimer ;
f i n a l Icon i d l e I c o n ;
f i n a l I c o n [ ] busyIcons = new I c o n [ 1 5 ] ;
i n t busyIconIndex = 0 ;
p r i v a t e J D i a l o g aboutBox ;
}
47
Indice analitico
action, 26
Antialiasing, 31
APPROVE_OPTION, 11
Apri, 15
cast, 30
FileChooser, 11
Graphics2D, 30
Inspector, 24
JScrollPane, 24
paintComponent, 30
Properties, 26
49
Bibliografia
51
Bibliografia
[IDE - NetBeans]
NetBeans è un IDE particolarmente adatto per scrivere programmi in Java si può liberamente scaricare al
seguente indirizzo http://www.netbeans.org
[Inkscape - grafica vettoriale] Inkscape è un ottimo programma di grafica vettoriale liberamente scaricabile da http://inkscape.
org/download/?lang=it
[Documentazione Java] Documentazione ufficiale Java http://java.sun.
com/javase/6/docs/api/
53