Created
June 20, 2014 08:47
-
-
Save settra/3dd2c3bbd5a430b32b49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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