Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View MAINclass.
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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
package helloworld;
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.*;
 
 
 
 
 
public class Swing extends JFrame {
public static int LW=0;
public static int RW=0;
public static int AC=0;
public static int BLOWERLVL=0;
public static int RECYCLE=0;
public static int AIRFLOW=0;
public static int TEMP=25;
public static String Data= "sex";
static public JFrame frame = new JFrame("tittle");
public static JButton LWDbutton = new JButton("LWD");
public static JButton LWUbutton = new JButton("LWU");
public static JButton RWDbutton = new JButton("RWD");
public static JButton RWUbutton = new JButton("RWU");
public static JButton ACbutton = new JButton("AC");
public static JButton RECbutton = new JButton("REC");
 
public static JButton BLOWERpluss = new JButton("blowerpluss");
public static JButton BLOWERminus = new JButton("blowerminus");
public static JButton TEMPpluss = new JButton("tempplus");
public static JButton TEMPminus = new JButton("tempminus");
public static JButton AIR1 = new JButton("AIR1");
public static JButton AIR2 = new JButton("AIR2");
public static JButton AIR3 = new JButton("AIR3");
 
public static void main (String[] args){
JPanel panel = new JPanel();
 
panel.setLayout(null);
JLabel background = new JLabel(new ImageIcon("background.jpg"));
ImageIcon imageIcon = new ImageIcon("blower.png"); // load the image to a imageIcon
Image image = imageIcon.getImage(); // transform it
Image newimg = image.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
imageIcon = new ImageIcon(newimg); // transform it back
JLabel blower = new JLabel(imageIcon);
 
// --- TAMPELES GIA THERMOKRASIA KAI BLOWER LVL //
JLabel blowerlvllabel = new JLabel (Integer.toString(BLOWERLVL));
blowerlvllabel.setForeground(new Color(153,217,234));
blowerlvllabel.setHorizontalAlignment(SwingConstants.CENTER);
blowerlvllabel.setFont(new Font("Agency FB", Font.ITALIC+Font.BOLD, 40));
JLabel templabel = new JLabel (Integer.toString(TEMP));
templabel.setForeground(new Color(153,217,234));
blowerlvllabel.setHorizontalAlignment(SwingConstants.CENTER);
templabel.setFont(new Font("Agency FB", Font.ITALIC+Font.BOLD, 40));
//------------------------------//
// BUTTON DECLERATIONS //
LWDbutton.setActionCommand("LWD");
LWUbutton.setActionCommand("LWU");
RWDbutton.setActionCommand("RWD");
RWUbutton.setActionCommand("RWU");
ACbutton.setActionCommand("AC");
RECbutton.setActionCommand("REC");
BLOWERpluss.setActionCommand("BLW+");
BLOWERminus.setActionCommand("BLW-");
TEMPpluss.setActionCommand("TEMP+");
TEMPminus.setActionCommand("TEMP-");
AIR1.setActionCommand("AIR1");
AIR2.setActionCommand("AIR2");
AIR3.setActionCommand("AIR3");
//-------------------------//
//BUTTON IMAGES //
LWUbutton.setIcon(new ImageIcon("Window_up.jpg"));
LWDbutton.setIcon(new ImageIcon("Window_down.jpg"));
RWUbutton.setIcon(new ImageIcon("Window_up.jpg"));
RWDbutton.setIcon(new ImageIcon("Window_down.jpg"));
LWUbutton.setRolloverIcon(new ImageIcon("Window_rolling_up.gif"));
LWDbutton.setRolloverIcon(new ImageIcon("Window_rolling_down.gif"));
RWUbutton.setRolloverIcon(new ImageIcon("Window_rolling_up.gif"));
RWDbutton.setRolloverIcon(new ImageIcon("Window_rolling_down.gif"));
ACbutton.setIcon(new ImageIcon("AC_button.jpg"));
RECbutton.setIcon(new ImageIcon("Recycle_button.jpg"));
ACbutton.setPressedIcon(new ImageIcon("AC_button_pressed.jpg"));
RECbutton.setPressedIcon(new ImageIcon("Recycle_button_pressed.jpg"));
BLOWERpluss.setIcon(new ImageIcon("pluss_button.jpg"));
BLOWERminus.setIcon(new ImageIcon("minus_button.jpg"));
TEMPpluss.setIcon(new ImageIcon("pluss_button.jpg"));
TEMPminus.setIcon(new ImageIcon("minus_button.jpg"));
AIR1.setIcon(new ImageIcon("air_bot.jpg"));
AIR2.setIcon(new ImageIcon("air_middle.jpg"));
AIR3.setIcon(new ImageIcon("air_defrost.jpg"));
AIR1.setPressedIcon(new ImageIcon("air_bot_pressed.jpg"));
AIR2.setPressedIcon(new ImageIcon("air_middle_pressed.jpg"));
AIR3.setPressedIcon(new ImageIcon("air_defrost_pressed.jpg"));
//----------------------------------//
// BUTTON POTITIONING
LWUbutton.setBounds(20,10,60,80);
LWDbutton.setBounds(20,90,60,80);
RWUbutton.setBounds(85,10,60,80);
RWDbutton.setBounds(85,90,60,80);
ACbutton.setBounds(190,30,130,57);
RECbutton.setBounds(190,100,130,57);
BLOWERpluss.setBounds(470,40,40,40);
BLOWERminus.setBounds(370,40,40,40);
TEMPpluss.setBounds(470,110,40,40);
TEMPminus.setBounds(370,110,40,40);
AIR1.setBounds(590,10,40,160);
AIR2.setBounds(640,10,40,160);
AIR3.setBounds(690,10,40,160);
blowerlvllabel.setBounds(395,45,80,30);
templabel.setBounds(417,105,50,50);
 
blower.setBounds(510,35,50,50);
background.setBounds(0,0,768,220);
//MALAKIA FOR TEST PROPOSES
 
JLabel TESTDATA = new JLabel (Data);
TESTDATA.setBounds(0,80,768,220);
TESTDATA.setOpaque(false);
TESTDATA.setForeground(Color.red);
//ADD SHIT YO
background.add(blower);
background.add(TESTDATA);
background.add(blowerlvllabel);
background.add(templabel);
 
 
panel.add(LWUbutton);
panel.add(LWDbutton);
panel.add(RWUbutton);
panel.add(RWDbutton);
panel.add(ACbutton);
panel.add(RECbutton);
panel.add(BLOWERpluss);
panel.add(BLOWERminus);
panel.add(TEMPpluss);
panel.add(TEMPminus);
panel.add(AIR1);
panel.add(AIR2);
panel.add(AIR3);
panel.add(background);
frame.setSize(768,220);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
frame.setAlwaysOnTop( true );
//ACTION LISTENER REGISTER
LWUbutton.addActionListener(new Action() );
LWDbutton.addActionListener(new Action() );
RWUbutton.addActionListener(new Action() );
RWDbutton.addActionListener(new Action() );
ACbutton.addActionListener(new Action() );
RECbutton.addActionListener(new Action() );
BLOWERpluss.addActionListener(new Action() );
BLOWERminus.addActionListener(new Action() );
TEMPpluss.addActionListener(new Action() );
TEMPminus.addActionListener(new Action() );
AIR1.addActionListener(new Action() );
AIR2.addActionListener(new Action() );
AIR3.addActionListener(new Action() );
ArduinoTest1 connection = new ArduinoTest1();
Data = connection.Data_tester() ;
TESTDATA.setText(Data);
System.out.println( "kanw connection" );
// MAIM PROGRAM
if ( connection.initialize() ) {
 
while (true){
Data = LW + "," + RW + ","+BLOWERLVL+","+AC+","+ RECYCLE + "," + AIRFLOW + "," + TEMP;
TESTDATA.setText(Data);
blowerlvllabel.setText(Integer.toString(BLOWERLVL));
templabel.setText(Integer.toString(TEMP));
connection.sendData(Data);
try { Thread.sleep(200); } catch (InterruptedException ie) {}
//connection.close();
}
 
}
else {
Data = "pare ta arxidia mou" ;
TESTDATA.setText(Data);
}
 
 
 
}
static class Action implements ActionListener {
 
public void actionPerformed (ActionEvent e){
String action = e.getActionCommand();
// POWER WINDOW BUTTON HANDLERS
if (action=="LWU") {
if (LW==0){
LW= 1;
LWUbutton.setIcon(new ImageIcon("Window_rolling_up.gif"));
}
else if (LW==1){
LW=0;
LWUbutton.setIcon(new ImageIcon("Window_up.jpg"));
}
}
else if (action=="LWD") {
if (LW==0){
LW= 2;
LWDbutton.setIcon(new ImageIcon("Window_rolling_down.gif")); }
else if (LW==2){
LW=0;
LWDbutton.setIcon(new ImageIcon("Window_down.jpg"));
}
}
if (action=="RWU") {
if (RW==0){
RW= 1;
RWUbutton.setIcon(new ImageIcon("Window_rolling_up.gif"));
}
else if (RW==1){
RW=0;
RWUbutton.setIcon(new ImageIcon("Window_up.jpg"));
}
}
else if (action=="RWD") {
if (RW==0){
RW= 2;
RWDbutton.setIcon(new ImageIcon("Window_rolling_down.gif")); }
else if (RW==2){
RW=0;
RWDbutton.setIcon(new ImageIcon("Window_down.jpg"));
}
}
// AC - REC HANDLERS
if (action=="AC") {
if (AC==0){
AC=1;
ACbutton.setIcon(new ImageIcon("AC_button_on.jpg"));
}
else if(AC==1) {
AC=0;
ACbutton.setIcon(new ImageIcon("AC_button.jpg"));
}
}
if (action=="REC") {
if (RECYCLE==0){
RECYCLE=1;
RECbutton.setIcon(new ImageIcon("Recycle_button_on.jpg"));
}
else if(RECYCLE==1) {
RECYCLE=0;
RECbutton.setIcon(new ImageIcon("Recycle_button.jpg"));
}
}
//BLOWER
if (action=="BLW+") {
BLOWERLVL++;
}
else if(action=="BLW-"){
BLOWERLVL--;
}
if (action=="TEMP+"){
TEMP++;
}
else if (action=="TEMP-"){
TEMP--;
}
//AIRFLOW
if (action=="AIR1") {
 
if (AIRFLOW==0) {
AIRFLOW =1;
AIR1.setIcon(new ImageIcon("air_bot_active.jpg"));
}
else if (AIRFLOW==2) {
AIR1.setIcon(new ImageIcon("air_bot_active.jpg"));
AIRFLOW=3;
}
else if ( (AIRFLOW==1)||(AIRFLOW==3) ) {
AIR1.setIcon(new ImageIcon("air_bot.jpg"));
AIRFLOW--;
}
else if (AIRFLOW==4) {
AIRFLOW=1;
AIR3.setIcon(new ImageIcon("air_defrost.jpg"));
AIR1.setIcon(new ImageIcon("air_bot_active.jpg"));
}
}
if (action=="AIR2") {
 
if (AIRFLOW==0) {
AIR2.setIcon(new ImageIcon("air_middle_active.jpg"));
AIRFLOW=2;
}
else if (AIRFLOW==1){
AIRFLOW=3;
AIR2.setIcon(new ImageIcon("air_middle_active.jpg"));
 
}
else if ( (AIRFLOW==2)||(AIRFLOW==3) ){
AIR2.setIcon(new ImageIcon("air_middle.jpg"));
AIRFLOW = AIRFLOW-2;
}
else if (AIRFLOW==4) {
AIRFLOW=2;
AIR3.setIcon(new ImageIcon("air_defrost.jpg"));
AIR2.setIcon(new ImageIcon("air_middle_active.jpg"));
}
}
if (action=="AIR3"){
 
if (AIRFLOW!=4){
AIRFLOW=4;
AIR1.setIcon(new ImageIcon("air_bot.jpg"));
AIR2.setIcon(new ImageIcon("air_middle.jpg"));
AIR3.setIcon(new ImageIcon("air_defrost_active.jpg"));
}
else if (AIRFLOW==4) {
AIRFLOW=0;
AIR3.setIcon(new ImageIcon("air_defrost.jpg"));
}
}
}
}
 
}
 
View MAINclass.
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
package helloworld;
 
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
 
/**
* @author ericjbruno
*/
public class ArduinoTest1 implements SerialPortEventListener {
SerialPort serialPort = null;
public String Data_tester() {
return "skata sto tafo tou kilafi" ;
}
 
private static final String PORT_NAMES[] = {
// "/dev/tty.usbmodem", // Mac OS X
// "/dev/usbdev", // Linux
// "/dev/tty", // Linux
// "/dev/serial", // Linux
"COM4", // Windows
};
private String appName;
private BufferedReader input;
private OutputStream output;
private static final int TIME_OUT = 1000; // Port open timeout
private static final int DATA_RATE = 9600; // Arduino serial port
String readFile(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
 
while (line != null) {
sb.append(line);
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
 
public boolean initialize() {
try {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
 
// Enumerate system ports and try connecting to Arduino over each
//
//System.out.println( "SKATA STON KILAFI");
while (portId == null && portEnum.hasMoreElements()) {
// Iterate through your host computer's serial port IDs
//
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
// System.out.println( " port" + currPortId.getName() );
String PORT_NAME[] = {readFile("PORT.txt") } ;
for (String portName : PORT_NAME) {
if ( currPortId.getName().equals(portName)
|| currPortId.getName().startsWith(portName)) {
 
// Try to connect to the Arduino on this port
//
// Open serial port
serialPort = (SerialPort)currPortId.open(appName, TIME_OUT);
portId = currPortId;
// System.out.println( "Connected on port" + currPortId.getName() );
break;
}
}
}
if (portId == null || serialPort == null) {
// System.out.println("Oops... Could not connect to Arduino");
return false;
}
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
 
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
 
// Give the Arduino some time
try { Thread.sleep(2000); } catch (InterruptedException ie) {}
return true;
}
catch ( Exception e ) {
e.printStackTrace();
}
return false;
}
void sendData(String data) {
try {
System.out.println("Sending data: '" + data +"'");
// open the streams and send the "y" character
output = serialPort.getOutputStream();
output.write( data.getBytes() );
}
catch (Exception e) {
System.err.println(e.toString());
System.exit(0);
}
}
 
//
// This should be called when you stop using the port
//
public synchronized void close() {
if ( serialPort != null ) {
serialPort.removeEventListener();
serialPort.close();
}
}
 
//
// Handle serial port event
//
public synchronized void serialEvent(SerialPortEvent oEvent) {
//System.out.println("Event received: " + oEvent.toString());
try {
switch (oEvent.getEventType() ) {
case SerialPortEvent.DATA_AVAILABLE:
if ( input == null ) {
input = new BufferedReader(
new InputStreamReader(
serialPort.getInputStream()));
}
String inputLine = input.readLine();
System.out.println(inputLine);
break;
 
default:
break;
}
}
catch (Exception e) {
System.err.println(e.toString());
}
}
 
public ArduinoTest1() {
appName = getClass().getName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.