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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
| package ControleActif_1_SLIDER;
import java.awt.GridBagConstraints ;
import java.awt.GridBagLayout ;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.* ;
import javax.swing.event.ChangeEvent ;
import javax.swing.event.ChangeListener ;
import javax.comm.*;
import java.io.*;
import java.util.*;
public class Main1Slider extends JPanel{
JLabel val_gain, val_puls, val_facteur, val_delay ;
JSlider sgain, spuls, sfacteur, sdelay ;
JLabel TexteGain, TextePuls, TexteFacteur ;
JButton boutonreset;
public Main1Slider() {
super() ;
sgain = new JSlider() ;
spuls = new JSlider() ;
sfacteur = new JSlider() ;
sdelay = new JSlider() ;
boutonreset = new JButton("Mise à zéro");
sgain.setMinimum(-50) ;
sgain.setMaximum(50) ;
sgain.setValue(0) ;
sgain.setMajorTickSpacing(10) ;
sgain.setMinorTickSpacing(5) ;
sgain.setPaintTicks(true) ;
sgain.setPaintLabels(true);
spuls.setMinimum(-50) ;
spuls.setMaximum(50) ;
spuls.setValue(0) ;
spuls.setMajorTickSpacing(10) ;
spuls.setMinorTickSpacing(5) ;
spuls.setPaintTicks(true) ;
spuls.setPaintLabels(true);
sfacteur.setMinimum(-50) ;
sfacteur.setMaximum(50) ;
sfacteur.setValue(0) ;
sfacteur.setMajorTickSpacing(10) ;
sfacteur.setMinorTickSpacing(5) ;
sfacteur.setPaintTicks(true) ;
sfacteur.setPaintLabels(true);
sdelay.setMinimum(-50) ;
sdelay.setMaximum(50) ;
sdelay.setValue(0) ;
sdelay.setMajorTickSpacing(10) ;
sdelay.setMinorTickSpacing(5) ;
sdelay.setPaintTicks(true) ;
sdelay.setPaintLabels(true);
val_gain = new JLabel("%"+sgain.getValue()) ;
val_puls = new JLabel("%"+spuls.getValue()) ;
val_facteur = new JLabel("%"+sfacteur.getValue()) ;
val_delay = new JLabel("%"+sdelay.getValue());
TexteGain = new JLabel("Modification du Gain :") ;
this.setLayout(new GridBagLayout()) ;
GridBagConstraints c = new GridBagConstraints() ;
c.fill = GridBagConstraints.BOTH;
c.anchor=GridBagConstraints.LINE_START ;
c.gridx=0 ;
c.gridy=0 ;
this.add(TexteGain,c) ;
c.gridx=1 ;
c.gridy=0 ;
c.anchor = GridBagConstraints.CENTER ;
this.add(sgain,c) ;
c.gridx=2 ;
c.gridy=0 ;
c.anchor=GridBagConstraints.LINE_END;
this.add(val_gain,c) ;
c.gridx=0 ;
c.gridy=1 ;
c.anchor=GridBagConstraints.LINE_START ;
this.add(new JLabel("Modification de la pulsation : "),c) ;
c.gridx=1 ;
c.gridy=1 ;
c.anchor = GridBagConstraints.CENTER ;
this.add(spuls,c) ;
c.gridx=2 ;
c.gridy=1 ;
c.anchor=GridBagConstraints.LINE_END;
this.add(val_puls,c) ;
c.gridx=0 ;
c.gridy=2 ;
c.anchor=GridBagConstraints.LINE_START ;
this.add(new JLabel("Modification du facteur de qualité : "),c) ;
c.gridx=1 ;
c.gridy=2 ;
c.anchor = GridBagConstraints.CENTER ;
this.add(sfacteur,c) ;
c.gridx=2 ;
c.gridy=2 ;
c.anchor=GridBagConstraints.LINE_END;
this.add(val_facteur,c) ;
c.gridx=0 ;
c.gridy=3 ;
c.anchor=GridBagConstraints.LINE_START ;
this.add(new JLabel("Modification du Delay : "),c) ;
c.gridx=1 ;
c.gridy=3 ;
c.anchor = GridBagConstraints.CENTER ;
this.add(sdelay,c) ;
c.gridx=2 ;
c.gridy=3 ;
c.anchor=GridBagConstraints.LINE_END;
this.add(val_delay,c) ;
c.gridx=1 ;
c.gridy=4 ;
c.anchor = GridBagConstraints.CENTER ;
this.add(boutonreset,c) ;
sgain.addChangeListener(new AuditeurSliderGain()) ;
spuls.addChangeListener(new AuditeurSliderPuls()) ;
sfacteur.addChangeListener(new AuditeurSliderFacteur()) ;
sdelay.addChangeListener(new AuditeurSliderDelay());
boutonreset.addActionListener(new ActionBoutonReset());
}
private class AuditeurSliderGain implements ChangeListener{
public void stateChanged(ChangeEvent arg0) {
if (sgain.isFocusOwner()){
int valeurGAIN = 0 ;
val_gain.setText("%"+sgain.getValue()) ;
valeurGAIN = sgain.getValue();
System.out.println("YO H0!"+valeurGAIN) ;
new TesteJavaComm("COM1",valeurGAIN);
}
}
}
private class AuditeurSliderPuls implements ChangeListener{
public void stateChanged(ChangeEvent arg0) {
if (spuls.isFocusOwner()){
int valeurPULS = 0 ;
val_puls.setText("%"+spuls.getValue()) ;
valeurPULS = spuls.getValue();
System.out.println("YO w0!"+valeurPULS) ;
new TesteJavaComm("COM1", valeurPULS);
}
}
}
private class AuditeurSliderFacteur implements ChangeListener{
public void stateChanged(ChangeEvent arg0) {
if (sfacteur.isFocusOwner()){
int valeurFACTEUR = 0 ;
val_facteur.setText("%"+sfacteur.getValue()) ;
valeurFACTEUR = (char) sfacteur.getValue();
System.out.println("YO Q!"+valeurFACTEUR) ;
new TesteJavaComm("COM1", valeurFACTEUR);
}
}
}
private class AuditeurSliderDelay implements ChangeListener{
public void stateChanged(ChangeEvent arg0) {
if (sdelay.isFocusOwner()){
int valeurDELAY = 0 ;
val_delay.setText("%"+sdelay.getValue()) ;
valeurDELAY = sdelay.getValue();
System.out.println("YO DELAY!"+valeurDELAY) ;
new TesteJavaComm("COM1", valeurDELAY);
}
}
}
private class ActionBoutonReset implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource()==boutonreset)
{
System.out.println("PAS YO!") ;
sdelay.setValue(0) ;
val_delay.setText("%"+sdelay.getValue()) ;
sgain.setValue(0) ;
val_gain.setText("%"+sgain.getValue()) ;
spuls.setValue(0) ;
val_puls.setText("%"+spuls.getValue()) ;
sfacteur.setValue(0) ;
val_facteur.setText("%"+sfacteur.getValue()) ;
}
}
}
/*
Cette classe permet de tester les ports séries disponibles
en les listant puis en créant un stream d'entrée et de sortie
*/
public class TesteJavaComm
{
CommPortIdentifier portId;
BufferedReader in;
PrintWriter out;
SerialPort serialPort;
/*
Constructeur de la classe TesteJavaComm qui prend en paramètre le port série à utiliser.
Dans un premier temps on liste les ports disponibles sur le pc
*/
public TesteJavaComm (String com, int a)
{
System.out.println("listage des ports série disponibles:");
//listePortsDispo();
try
{
//ouverture du port série:
portId=CommPortIdentifier.getPortIdentifier(com);
serialPort=(SerialPort)portId.open("Envoi",2000);
//on sélectionne tous les paramètres de la connexion série:
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
System.out.println("Ouverture du port"+com);
//pour lire et écrire avec des streams:
in=new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
out=new PrintWriter(serialPort.getOutputStream());
//Teste de lecture/ecriture sur le port série
/*int ASCII = (new Character(a)).hashCode();
String CHAINE = Integer.toBinaryString(ASCII);*/
//String originalText="une chaine de caractere";
//outputArray = originalText.getBytes( );
out.write (""+a);
//out.write ( outputArray, 0 , length);
//in.close();
out.close();
serialPort.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}//fin constructeur
/*public void listePortsDispo()
{
System.err.println("recherche...");
Enumeration portList=CommPortIdentifier.getPortIdentifiers();
if (portList==null)
{
System.err.println("Aucun port de communication détecté");
return;
}
while (portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
System.out.println("* "+portId.getName());
} // while
} //fin de la methode listePortsDispo()*/
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
IllegalAccessException, UnsupportedLookAndFeelException
{
// En decommantant la ligne suivante, linterface prend le look-and-feel
//du systeme dexploitation
//UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()) ;
JFrame fen = new JFrame("Contrôle Actif appliqué au chevalet de violon") ;
fen.setResizable(true) ;
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
fen.setContentPane(new Main1Slider()) ;
fen.pack() ;
fen.setVisible(true) ;
char lettre = 'a';
int monAsci = (new Character(lettre)).hashCode();
String binaryString = Integer.toBinaryString(monAsci);
System.out.println(binaryString);
}
} |
Partager