━━━━ ◇ ━━━━
Java/Java 프로젝트

Server and PieChart

1. Server


1.1 Server.java

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
// input your code herepackage server;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
 
public class Server extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/server/Server.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
 
}
 
cs

1.2 Server.fxml

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
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="server.ServerController">
   <children>
      <Label alignment="CENTER" layoutX="26.0" layoutY="28.0" prefHeight="35.0" prefWidth="452.0" text="Mobile OS 점유율">
         <font>
            <Font name="D2Coding" size="30.0" />
         </font>
      </Label>
      <Label layoutX="21.0" layoutY="128.0" text="Android">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label layoutX="41.0" layoutY="217.0" text="IOS">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label layoutX="31.0" layoutY="300.0" text="Linux">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <TextField fx:id="tf1" layoutX="132.0" layoutY="124.0" prefHeight="29.0" prefWidth="241.0" />
      <TextField fx:id="tf2" layoutX="132.0" layoutY="213.0" prefHeight="29.0" prefWidth="241.0" />
      <TextField fx:id="tf3" layoutX="132.0" layoutY="296.0" prefHeight="29.0" prefWidth="241.0" />
      <Button layoutX="259.0" layoutY="399.0" mnemonicParsing="false" onAction="#send" prefHeight="32.0" prefWidth="219.0" text="전송">
         <font>
            <Font name="D2Coding" size="18.0" />
         </font>
      </Button>
      <Button layoutX="21.0" layoutY="399.0" mnemonicParsing="false" onAction="#startServer" prefHeight="32.0" prefWidth="205.0" text="서버 시작">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
   </children>
</AnchorPane>
 
cs

1.3 OSData.java

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
package server;
 
import java.io.Serializable;
 
public class OSData implements Serializable{
    int android;
    int ios;
    int linux;
    public OSData(int android, int ios, int linux) {
        super();
        this.android = android;
        this.ios = ios;
        this.linux = linux;
    }
    public int getAndroid() {
        return android;
    }
    public void setAndroid(int android) {
        this.android = android;
    }
    public int getIos() {
        return ios;
    }
    public void setIos(int ios) {
        this.ios = ios;
    }
    public int getLinux() {
        return linux;
    }
    public void setLinux(int linux) {
        this.linux = linux;
    }
    
    
}
 
cs

1.4 ServerController.java

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
package server;
 
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
 
public class ServerController {
 
    @FXML private TextField tf1, tf2, tf3;
    
    ServerSocket server;
    Socket socket;
    
    
    class ServerThread implements Runnable{
 
        Socket socket;
        
        public ServerThread(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            
            int android = Integer.parseInt(tf1.getText());
            int ios = Integer.parseInt(tf2.getText());
            int linux = Integer.parseInt(tf3.getText());
            
            OSData osd = new OSData(android, ios, linux);
            
            try {
                OutputStream os = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(os);
                
                oos.writeObject(osd);
                oos.flush();
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }
    
    public void startServer(ActionEvent event){
        try {
            server = new ServerSocket(8888);
            System.out.println("서버가 시작되었습니다.");
            System.out.println("클라이언트 접속 대기중 입니다.");
            socket = server.accept();
            System.out.println("클라이언트("+socket.getInetAddress()+")가 접속 하였습니다.");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    public void send(ActionEvent event){
        Thread thread = new Thread (new ServerThread(socket));
        thread.start();
    }
 
}
 
 
cs

2. Client


2.1 Main.java

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
package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs

2.2 Main.fxml

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
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <Button layoutX="334.0" layoutY="441.0" mnemonicParsing="false" onAction="#btnClicked" text="결과 보기">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
      <PieChart fx:id="pieChart" layoutX="6.0" layoutY="7.0" prefHeight="358.0" prefWidth="486.0" />
      <Label fx:id="status" alignment="CENTER" layoutX="19.0" layoutY="386.0" prefHeight="33.0" prefWidth="460.0" textFill="RED">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Button layoutX="95.0" layoutY="441.0" mnemonicParsing="false" onAction="#acceptServer" text="서버 접속">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
   </children>
</AnchorPane>
 
cs

2.3 MainController.java

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
package application;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.UnknownHostException;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import server.OSData;
 
public class MainController {
    
    @FXML PieChart pieChart;
    @FXML Label status;
    
    Socket socket;
    int android;
    int ios;
    int linux;
    
    public void acceptServer(ActionEvent event){
        try {
            socket = new Socket("192.168.0.20"8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("서버에 접속합니다.");
    }
    
    public void btnClicked(ActionEvent event){
        
        try {
            
            
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            OSData osd = (OSData) ois.readObject();
            
            android = osd.getAndroid();
            ios = osd.getIos();
            linux = osd.getLinux();
            
            ObservableList<Data> list = FXCollections.observableArrayList(
                new PieChart.Data("Android", android),    
                new PieChart.Data("IOS", ios),    
                new PieChart.Data("Linux", linux)    
                );
            pieChart.setData(list);
            
            for (final PieChart.Data data : pieChart.getData()) {
                data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 
                    @Override
                    public void handle(MouseEvent event) {
                        status.setText(String.valueOf(data.getPieValue()) + "%");
                        
                    }
                });
            }
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}
 
cs


'Java > Java 프로젝트' 카테고리의 다른 글

Java 임금 계산기  (0) 2016.08.11
java swing 으로 music player 만들기  (0) 2016.08.10
COMMENT