~peta-power-group/robodj/RoboDJ

« back to all changes in this revision

Viewing changes to source/RoboDJ-clients/java/src/klijent/TCPklijent.java

  • Committer: mat
  • Date: 2010-09-22 22:17:22 UTC
  • Revision ID: matija_net@net.hr-20100922221722-lc8cbqoshpn39usw
Dodao android i java klijenta (GUI aplikacija). I python skriptu i C# gui za testiranje servera.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package klijent;
 
2
 
 
3
import java.awt.*;
 
4
import java.awt.event.*;
 
5
import java.io.BufferedReader;
 
6
import java.io.IOException;
 
7
import java.io.InputStreamReader;
 
8
import java.io.OutputStream;
 
9
import java.io.PrintWriter;
 
10
import java.net.InetAddress;
 
11
import java.net.Socket;
 
12
import java.net.UnknownHostException;
 
13
 
 
14
import java.awt.image.*;
 
15
import javax.swing.*;
 
16
 
 
17
public class TCPklijent  {
 
18
        
 
19
    
 
20
        public static void main(String [] args){
 
21
                Frame frame=new Frame("Agrokor klijent");
 
22
                frame.setBackground(Color.black);
 
23
                JButton button1 = new JButton ("+1");
 
24
                button1.setText("");
 
25
                button1.setActionCommand("+1");
 
26
                ImageIcon icon1 = new ImageIcon("slike\\up.jpg");
 
27
                button1.setIcon(icon1);
 
28
                button1.setPreferredSize(new Dimension(120,120));
 
29
                button1.addActionListener(new ButtonListener());
 
30
                JButton button2 = new JButton ("-1");
 
31
                button2.setText("");
 
32
                button2.setActionCommand("-1");
 
33
                ImageIcon icon2 = new ImageIcon("slike\\down.jpg");
 
34
                button2.setIcon(icon2);
 
35
                button2.setPreferredSize(new Dimension(120,120));
 
36
                button2.addActionListener(new ButtonListener());
 
37
                frame.add(button1);
 
38
                frame.add(button2);
 
39
                frame.setLayout(new FlowLayout());
 
40
                frame.setSize(300,200);
 
41
                frame.setResizable(false);
 
42
                frame.setVisible(true);
 
43
                frame.addWindowListener(new WindowAdapter() {
 
44
                        public void windowClosing(WindowEvent e) {
 
45
                                System.exit(0);
 
46
                        }
 
47
                });
 
48
                 
 
49
        }
 
50
}
 
51
 
 
52
class ButtonListener implements ActionListener {
 
53
        private String posalji_primi(String poruka) {
 
54
        //tcpclient tcp = new tcpclient();
 
55
           //Thread cThread = new Thread(new tcpclient());
 
56
           //cThread.start();
 
57
           //cThread.run();
 
58
           String stanje = "OK";
 
59
           try {
 
60
                   InetAddress serverAddr = InetAddress.getByName("131.130.32.51");
 
61
                //Socket s = new Socket("localhost",7777);
 
62
                Socket s = new Socket(serverAddr, 80);
 
63
                   
 
64
                //outgoing stream redirect to socket
 
65
                OutputStream out = s.getOutputStream();
 
66
               
 
67
                PrintWriter output = new PrintWriter(out);
 
68
                output.print(poruka);
 
69
                output.flush();
 
70
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
 
71
               
 
72
                //read line(s)
 
73
                String st = input.readLine();
 
74
                stanje = st;
 
75
                //Close connection
 
76
                s.close();
 
77
               
 
78
               
 
79
           } 
 
80
           catch (UnknownHostException e) {
 
81
                // TODO Auto-generated catch block
 
82
                e.printStackTrace();
 
83
                stanje = e.getMessage();
 
84
           }    
 
85
           catch (IOException e) {
 
86
                // TODO Auto-generated catch block
 
87
                e.printStackTrace();
 
88
                stanje = e.getMessage();
 
89
           }
 
90
           
 
91
           return stanje;
 
92
    }
 
93
        
 
94
        ButtonListener() {
 
95
                
 
96
        }
 
97
        public void actionPerformed(ActionEvent e) { 
 
98
                if (e.getActionCommand().equals("+1")) {
 
99
                        String rezultat = posalji_primi("+1");
 
100
                        System.out.println(rezultat);
 
101
                }
 
102
                if (e.getActionCommand().equals("-1")) {
 
103
                        String rezultat = posalji_primi("-1");
 
104
                        System.out.println(rezultat);
 
105
                }
 
106
        }
 
107
}