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
| 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);
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) ;
sgain.addChangeListener(new AuditeurSliderGain()) ;
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);
}
}
}
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());
out.write (+a);
System.out.println(+a);
out.close();
serialPort.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}//fin constructeur
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