-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCostumCharacterMaker.java
153 lines (132 loc) · 5.08 KB
/
CostumCharacterMaker.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
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
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import jssc.SerialPort;
import jssc.SerialPortException;
class CostumCharacterMaker{
JFrame frame;
JPanel mainPanel;
JPanel topPanel;
JButton saveButton;
JButton clearButton;
GridLayout panelsGrid;
GridLayout checkBoxesGrid;
ArrayList<JPanel> panels;
ArrayList<JCheckBox> checkBoxes;
ArrayList<ArrayList> checkBoxesLists;
int[] pattern = new int[40];
int[] slaves;
int[] patternIndex;
String output;
static String input;
static SerialPort serialPort;
static Thread t = new Thread() {
@Override public void run(){
while(serialPort.isOpened()){
try {
input = serialPort.readString();
System.out.println(input == null? "" : input);
Thread.sleep(200);
}catch(SerialPortException e){e.printStackTrace();}
catch (InterruptedException e){e.printStackTrace();}
}
}
};
public static void main(String[] args){
CostumCharacterMaker ccm = new CostumCharacterMaker();
ccm.go();
ccm.initialize();
t.start();
}
private void initialize(){
serialPort = new SerialPort("COM9");
try{
serialPort.openPort();
serialPort.setParams(SerialPort. BAUDRATE_115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch(Exception e){e.printStackTrace();}
}
private void go(){
frame = new JFrame("Costum Character Maker");
panelsGrid = new GridLayout(2,4);
checkBoxesGrid = new GridLayout(8,5);
mainPanel = new JPanel(panelsGrid);
topPanel = new JPanel(new FlowLayout());
saveButton = new JButton("Send");
topPanel.add(saveButton);
saveButton.addActionListener(new BSaveActionListener());
clearButton = new JButton("Clear");
topPanel.add(clearButton);
clearButton.addActionListener(new BClearActionListener());
panels = new ArrayList<JPanel>();
checkBoxesLists = new ArrayList<ArrayList>();
for(int x=0;x<8;x++){
JPanel panel = new JPanel(checkBoxesGrid);
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
ArrayList<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
for(int y=0;y<40;y++){
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkBoxes.add(c);
panel.add(c);
}
panels.add(panel);
mainPanel.add(panel);
checkBoxesLists.add(checkBoxes);
}
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.getContentPane().add(BorderLayout.NORTH, topPanel);
}
private class BSaveActionListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
int index =0;
if(serialPort.isOpened()){
for(ArrayList<JCheckBox> array : checkBoxesLists){
output = "<";
for(JCheckBox c : array){
pattern[array.indexOf(c)] = c.isSelected()? 1: 0;
//System.out.print(pattern[array.indexOf(c)]);
output += pattern[array.indexOf(c)];
//System.out.print((array.indexOf(c)+1) % 5 == 0 ? ">"+ index :"");
output += (array.indexOf(c)+1) % 5 == 0 ? ">"+ index : "";
index = (array.indexOf(c)+1) % 5 == 0 ? index + 1: index + 0;
//System.out.print((array.indexOf(c)-4) % 5 == 0 && (array.indexOf(c)-4 != 35) ? "<":"");
output += (array.indexOf(c)-4) % 5 == 0 && (array.indexOf(c)-4 != 35) ? "<":"";
if((array.indexOf(c)+1)% 5 == 0){
// System.out.println(output);
try {
serialPort.writeString(output);
Thread.sleep(50);
}catch(SerialPortException e){e.printStackTrace();}
catch (InterruptedException e){e.printStackTrace();}
output = "";
}
}
// System.out.println("");
}
}
}
}
private class BClearActionListener implements ActionListener{
public void actionPerformed(ActionEvent ev) {
for(ArrayList<JCheckBox> array: checkBoxesLists) {
for(JCheckBox c:array) {
c.setSelected(false);
}
}
}
}
}