Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- bem10jfx.blogspot.com
- */
- package jfxcontrolearduinopcvolume;
- import java.awt.AWTException;
- import java.awt.Robot;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.Label;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- import jssc.SerialPort;
- import jssc.SerialPortEvent;
- import jssc.SerialPortEventListener;
- import jssc.SerialPortException;
- import jssc.SerialPortList;
- /**
- *
- * @author kmls
- */
- public class JFXControleArduinoPCvolume extends Application {
- String vlCode;
- String vlactions;
- Label lbaction = new Label("Ação: " + vlactions);
- Text lbcode = new Text("Code IR:" + vlCode);
- SerialPort serialport;
- String input;
- ChoiceBox cbport = new ChoiceBox();
- Scene scene;
- Robot robot;
- //1024/2 de resoluçao
- int cima = 550;
- int direita = 550;
- public void arranqdeconexao() {
- final String[] serialPortasNomes = SerialPortList.getPortNames();
- for (String nomeportas : serialPortasNomes) {
- cbport.getItems().addAll(nomeportas);
- System.out.println("iniciando comunicaçaoa de portas\n" + nomeportas);
- }
- }
- @Override
- public void start(Stage stage) {
- Group gp = new Group();
- scene = new Scene(gp, 400, 400, Color.SILVER);
- stage.setTitle("" + vlCode);
- stage.setScene(scene);
- arranqdeconexao();
- lbaction.layoutXProperty().bind(scene.widthProperty().divide(2.5));
- lbaction.layoutYProperty().bind(scene.heightProperty().divide(2.5));
- lbaction.setText("Açao :" + vlactions);
- lbcode.layoutXProperty().bind(lbaction.layoutXProperty());
- lbcode.layoutYProperty().bind(lbaction.layoutYProperty().add(30));
- lbcode.setText("code IR:" + vlCode);
- Button btport = new Button("conectar");
- btport.layoutXProperty().bind(lbcode.layoutXProperty().subtract(1));
- btport.layoutYProperty().bind(lbcode.layoutYProperty().add(30));
- cbport.layoutXProperty().bind(btport.layoutXProperty().add(70));
- cbport.layoutYProperty().bind(btport.layoutYProperty());
- gp.getChildren().addAll(lbaction, lbcode, btport, cbport);
- btport.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- try {
- serialport = new SerialPort(cbport.getValue().toString());
- try {
- serialport.openPort();
- serialport.setParams(9600, 8, 1, 0);
- serialport.setEventsMask(SerialPort.MASK_RXCHAR);
- } catch (SerialPortException ex) {
- Logger.getLogger(jfxcontrolearduinopcvolume.JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- if (serialport.isOpened() == true) {
- stage.setTitle("Conectado");
- } else {
- stage.setTitle("Desconectando");
- }
- serialport.addEventListener(new SerialPortEventListener() {
- @Override
- public void serialEvent(SerialPortEvent serialPortEvent) {
- try {
- input = serialport.readString(serialPortEvent.getEventValue());
- if (serialPortEvent.isRXCHAR()) {
- if (input.endsWith("\r\n")) {
- System.out.print("Code:");
- System.out.println("" + input.substring(0, input.indexOf("\r\n")));
- vlCode = input.substring(0, input.indexOf("\r\n"));
- lbcode.setText("Code IR:" + vlCode);
- if (vlCode.toString().equals("551502015".toString())) {
- vlactions = "POWER OFF";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- try {
- robot = new Robot();
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- ///retorno de code ir
- if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Cima") {
- System.out.println("retorno de caracter:" + vlactions.toString());
- cima = cima - 15;
- robot = new Robot();
- robot.mouseMove(direita, cima);
- }
- if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Baixo") {
- System.out.println("retorno de caracter:" + vlactions.toString());
- cima = cima + 15;
- robot = new Robot();
- robot.mouseMove(direita, cima);
- }
- if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Esquerda") {
- System.out.println("retorno de caracter:" + vlactions.toString());
- direita = direita - 15;
- robot = new Robot();
- robot.mouseMove(direita, cima);
- }
- if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Direita") {
- System.out.println("retorno de caracter:" + vlactions.toString());
- direita = direita + 15;
- robot = new Robot();
- robot.mouseMove(direita, cima);
- }
- //////////fim de retorno
- if (vlCode.toString().equals("551502015".toString())) {
- vlactions = "Volume +";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- Robot robot;
- try {
- robot = new Robot();
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (vlCode.toString().equals("551534655".toString())) {
- vlactions = "Volume -";
- // lbaction.setText("volume"+vlactions);
- System.out.println("Action :" + vlactions);
- } else if (vlCode.toString().equals("551486205".toString())) {
- vlactions = "Cima";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- cima = cima - 10;
- try {
- Robot rb = new Robot();
- rb.mouseMove(direita, cima);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (vlCode.toString().equals("551518845".toString())) {
- vlactions = "Baixo";
- // lbaction.setText("Ação :"+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- cima = cima + 10;
- try {
- Robot rb = new Robot();
- rb.mouseMove(direita, cima);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (vlCode.toString().equals("551510175".toString())) {
- vlactions = "Direita";
- // lbaction.setText( "Ação :"+vlactions);
- System.out.println("Action :" + vlactions.toString());
- direita = direita + 10;
- try {
- Robot rb = new Robot();
- rb.mouseMove(direita, cima); //layouttx ,layoutY
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (vlCode.toString().equals("551542815".toString())) {
- vlactions = "Esquerda";
- System.out.println("Action :" + vlactions.toString());
- direita = direita - 10;
- try {
- Robot rb = new Robot();
- rb.mouseMove(direita, cima);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (vlCode.toString().equals("551494365".toString())) {
- vlactions = "Mouse click esquerda";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- //click =click +1 direita
- try {
- Robot rb = new Robot();
- rb.mousePress(InputEvent.BUTTON1_MASK);
- rb.mouseRelease(InputEvent.BUTTON1_MASK);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }else if (vlCode.toString().equals("551494365".toString())) {
- vlactions = "Mouse click esquerda";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- //click=clik+1 esquerda
- try {
- Robot rb = new Robot();
- rb.mousePress(InputEvent.BUTTON1_MASK);
- rb.mouseRelease(InputEvent.BUTTON1_MASK);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (vlCode.toString().equals("551535165".toString())) {
- vlactions = "Mouse Presscionado Esquerda";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- //prescionado direita ;pres=(pres 1x=1 or pres 2x=0)
- try {
- Robot rb = new Robot();
- rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);
- rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }else if (vlCode.toString().equals("551527005".toString())) {
- vlactions = "Mouse click direita";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- //click=clik+1 esquerda
- try {
- Robot rb = new Robot();
- rb.mousePress(InputEvent.BUTTON3_MASK);
- rb.mouseRelease(InputEvent.BUTTON3_MASK);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }else if (vlCode.toString().equals("551490795".toString())) {
- vlactions = "Windows Desktop";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- try {
- Robot rb = new Robot();
- rb.keyPress(KeyEvent.VK_WINDOWS);
- rb.keyPress(KeyEvent.VK_D);
- rb.keyRelease(KeyEvent.VK_D);
- rb.keyRelease(KeyEvent.VK_WINDOWS);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }else if (vlCode.toString().equals("551541285".toString())) {
- vlactions = "Exit";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- try {
- Robot rb = new Robot();
- rb.keyPress(KeyEvent.VK_ALT);
- rb.keyPress(KeyEvent.VK_F4);
- rb.keyRelease(KeyEvent.VK_F4);
- rb.keyRelease(KeyEvent.VK_ALT);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }else if (vlCode.toString().equals("551517825".toString())) {
- vlactions = "Exit";
- // lbaction.setText(""+vlactions.toString());
- System.out.println("Action :" + vlactions.toString());
- try {
- Robot rb = new Robot();
- rb.keyPress(KeyEvent.VK_WINDOWS);
- rb.keyPress(KeyEvent.VK_UP);
- rb.keyRelease(KeyEvent.VK_UP);
- rb.keyRelease(KeyEvent.VK_WINDOWS);
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- } catch (SerialPortException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- System.err.println("error : Sem porta de conexao escolhida");
- } catch (AWTException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- } catch (Exception e) {
- e.getMessage();
- }
- }
- });
- stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
- @Override
- public void handle(WindowEvent event) {
- try {
- if (serialport.isOpened()) {
- serialport.closePort();
- Platform.exit();
- } else if (serialport.closePort() == true) {
- Platform.exit();
- }
- } catch (SerialPortException ex) {
- Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- stage.show();
- }
- /**
- * bem10jfx.blogspot.com
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
- ///bem10jfx.blogspot.com
Advertisement
Add Comment
Please, Sign In to add comment