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
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
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
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
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
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
102 jgra = new JLabel("inc-gra");
103 jgra.setFont(new Font("Tahoma", 1, 14));
104 jgra.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
105 add(jgra);
106 jgra.setBounds(188, 10, 50, 20);
107
108
109 jval = new JLabel("inc-val");
110 jval.setFont(new Font("Tahoma", 1, 14));
111 jval.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
112 add(jval);
113 jval.setBounds(308, 10, 50, 20);
114
115
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
124 mn = new MyIcon();
125 mn.setLocation(103, 86);
126 add(mn);
127
128 setVisible(true);
129
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
138 lw=200;
139 lx=(cox-lw)/2;
140 ly=(coy-60)/2;
141 jv.setBounds(lx, ly, 200, 40);
142
143
144 tm = new Timer(vel,new ActionListener() {
145 public void actionPerformed(ActionEvent evt) {
146 varia();
147 }
148 });
149 }
150
151
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 } |