Le Immaggini

Ci sto lavorando

 

Passo1 Passo 2 Passo3

 

Obiettivo: Variante n. 2 Etichetta rossa al centro

 

 

Ciò che differenzia questa versione dalla precedente è soltanto il fatto che l'etichetta che indica i gradi (quella rossa) è ora posizionata al centro della manopola anziché sotto al centro. La differenza del codice è qui:

 

Sotto:

137          //centra l'etichetta
138          lw=200;
139          lx=(cox-lw)/2;
140          ly=(coy-60); 
141          jv.setBounds(lx, ly, 200, 40);

 

Centrata:

137          //centra l'etichetta
138          lw=200;
139          lx=(cox-lw)/2;
140          ly=(coy-60)/2; 
141          jv.setBounds(lx, ly, 200, 40);

 

Come si nota la differenza è un (\2) ly=(coy-60); ly=(coy-60)/2;

 

Riporto comunque il codice con la piccolamodifica. L'altra classe MyIcon è identica e la potrete scaricare in ManoV1.

Classe: ManoV2

  1 package manov2;
  2 
  3   import java.awt.*;
  4   import java.awt.event.ActionEvent;
  5   import java.awt.event.ActionListener;
  6   import java.awt.event.ItemEvent;
  7   import java.awt.event.ItemListener;
  8   import javax.swing.event.ChangeListener;
  9   import javax.swing.event.ChangeEvent; 
 10   import java.util.Locale;
 11   import javax.swing.*;
 12   
 13   public class ManoV2 extends JFrame{
 14       private JButton jb,jx;
 15       private JToggleButton jg;
 16       private JSpinner jt,js;
 17       private JLabel jv,jgra,jval;
 18       private int flg=0,lx,ly,lw,mx,my,cox,coy,vel=10;
 19       private double gra=0,inc=0.5;
 20       private Timer tm;
 21       private String val;
 22       private MyIcon mn;
 23       
 24       public ManoV2() {
 25           Locale.setDefault(Locale.Category.FORMAT, Locale.ENGLISH); 
 26           
 27           setTitle("Visualizzazione Immagine");
 28           setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
 29           getContentPane().setBackground(Color.white); 
 30           setLayout(null);
 31           setSize(550, 500);
 32           setLocationRelativeTo(null);
 33   
 34           //Avanza Passo Passo       
 35           jb = new JButton("Passo");
 36           jb.addActionListener(new ActionListener() {
 37               public void actionPerformed(ActionEvent evt) {                              
 38                   tm.stop();
 39                   jg.setText("Start");
 40                   jg.setSelected(false);
 41                   varia();
 42                   mn.setGra(gra); 
 43               }
 44           });
 45           jb.setBounds(15, 10, 80, 23);
 46           add(jb);//--------
 47 
 48           //Cambio di direzione              
 49           jx = new JButton("Direzione");
 50           jx.addActionListener(new ActionListener() {
 51               public void actionPerformed(ActionEvent evt) {                              
 52                   if(flg==1)flg=0; else flg=1;                
 53               }
 54           });
 55           jx.setBounds(100, 10, 80, 23);
 56           add(jx);//--------
 57 
 58           //Spinner Incrementa il passo in gradi
 59           js = new JSpinner();
 60           js.setLocale(Locale.ENGLISH);
 61           js.setModel(new SpinnerNumberModel(inc,0.0d,360.0d,0.1d));        
 62           js.setFont(new java.awt.Font("Tahoma", 1, 14)); 
 63           js.addChangeListener(new ChangeListener() {
 64               public void stateChanged(ChangeEvent evt) {
 65                   inc = Double.parseDouble (""+js.getValue());              
 66               }
 67           });
 68           js.setBounds(240, 9, 60, 25);
 69           add(js);//--------
 70 
 71            //Spinner incrementa la velocità in millisecondi
 72           jt = new JSpinner(new SpinnerNumberModel(vel,1,2000,5));
 73           jt.setFont(new java.awt.Font("Tahoma", 1, 14)); 
 74           jt.addChangeListener(new ChangeListener() {
 75               public void stateChanged(ChangeEvent evt) {
 76                   vel = Integer.parseInt(""+jt.getValue());
 77                   tm.setDelay(vel);
 78                   tm.start(); 
 79               }
 80           });
 81           jt.setBounds(360, 9,75, 25);
 82           add(jt);//--------                         
 83 
 84           //Start/Stop  
 85           jg = new JToggleButton("Start");
 86           jg.addItemListener(new ItemListener() {
 87               public void itemStateChanged(ItemEvent evt) {
 88                  int status = evt.getStateChange();
 89                   if(status == ItemEvent.SELECTED){ 
 90                       jg.setText("Stop");
 91                       tm.start();
 92                   }else{
 93                       jg.setText("Start");
 94                       tm.stop();
 95                   }              
 96               }
 97           });
 98           jg.setBounds(440, 10, 80, 23);
 99          add(jg);//--------
100 
101          //etichetta inc-gra 
102          jgra = new JLabel("inc-gra");
103          jgra.setFont(new Font("Tahoma", 1, 14)); // NOI18N
104          jgra.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
105          add(jgra);
106          jgra.setBounds(188, 10, 50, 20);
107 
108          //etichetta inc-val
109          jval = new JLabel("inc-val");
110          jval.setFont(new Font("Tahoma", 1, 14)); // NOI18N
111          jval.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
112          add(jval);
113          jval.setBounds(308, 10, 50, 20);
114          
115          //etichetta che visualizza i gradi
116          jv = new JLabel("0");
117          jv.setFont(new Font("Digital-7Mono", 1, 50));
118          jv.setForeground(Color.RED);
119          jv.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
120          jv.setText("0");
121          add(jv);
122                     
123          //Disegna la manopola         
124          mn = new MyIcon();
125          mn.setLocation(103, 86);
126          add(mn);
127          
128          setVisible(true);
129          //centra la manopola
130          cox=getContentPane().getSize().width;
131          coy=getContentPane().getSize().height;
132          mx=(cox-(mn.getWp()))/2;
133          my=(coy-(mn.getHp()))/2;
134          mn.setLocation(mx, my);
135          mn.setLocation(mx, my);
136          
137          //centra l'etichetta
138          lw=200;
139          lx=(cox-lw)/2;
140          ly=(coy-60)/2; 
141          jv.setBounds(lx, ly, 200, 40);   
142          
143          //Imposta il Timer        
144          tm = new Timer(vel,new ActionListener() {
145              public void actionPerformed(ActionEvent evt) {
146                  varia();
147              }
148          });
149      }
150      
151      //controllo della posizione
152      private void varia(){        
153          if(flg==0){          
154              gra=gra+inc;
155              if(gra>360){
156                 gra=gra-360;                    
157              }                                  
158          }else{
159              if(gra==0){
160                  gra=360-inc;
161              }else{
162                  gra=gra-inc;
163                  if(gra<0){
164                  gra=gra+360;                    
165                  } 
166              }
167          }
168         
169          mn.setGra(gra);
170          val=String.format(new Locale("en"),"%1$.2f",gra);
171          jv.setText(val);    
172      }
173      
174      public static void main(String [] args){
175          
176          new ManoV2();
177      }
178   }

 

 

Classe esterna che disegna la manopola

 3 import java.awt.Dimension;
 4 import java.awt.Graphics;
 5 import java.awt.Graphics2D;
 6 import java.net.*;
 7 import javax.swing.*;
 8 
 9 class MyIcon extends JComponent{
10     private double gra=0;
11     private ImageIcon icon;
12     
13     public MyIcon(){        
14         icon = createImageIcon("/image/mano02.png");
15     }
16     public void paintComponent(Graphics g) {
17         super.paintComponent(g);
18         Graphics2D g2d =(Graphics2D)g;
19 
20         int h = icon.getIconHeight();
21         int w = icon.getIconWidth();
22         
23         Dimension sz=this.getParent().getSize();
24         int x=(sz.width-w)/2;
25         int y=(sz.height-w)/2;
26         
27         int xC =x+w/2;
28         int yC =y+h/2;
29         
30         g2d.translate(xC,yC); 
31         g2d.rotate(Math.toRadians(gra)); 
32         g2d.translate(-xC,-yC);
33         
34         icon.paintIcon(this, g2d, x, y);
35     }
36     public void setGra(double gr){
37         gra=gr;
38         repaint();
39     }
40     private ImageIcon createImageIcon(String path) {
41         URL imgURL = getClass().getResource(path);
42         if (imgURL != null) {
43             return new ImageIcon(imgURL);
44         } else {
45             System.err.println("Non è possibile trovare il file: " + path);
46             return null;
47         }
48     }
49 }