ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

基于单片机的4*4*4光立方取模软件及相应的源码

2020-08-22 22:01:33  阅读:280  来源: 互联网

标签:取模 java 单片机 i1 i3 i2 int 源码 new


总共3个版本

详细的代码注释有时间再写吧

对应的单片机电路图啥的有时间再整理

 

搬运的记得把我这文章地址打上

 

1.0版本(单面取模)

main.java

1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 
5 public class main {
6     public static void main(String args[]) throws IOException{
7         frame f = new frame();
8     }
9 }
1.0版本 main.java

frame.java

import java.awt.*;
import static java.awt.SystemColor.text;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.event.AncestorListener;

public class frame {
    String srcAdress;
    String code1,code2;
    int x = 0;
    int y = 0;
    String xHex;
    String yHex;
    Panel p;
    JFrame jf;
    JButton[][] jb = new JButton[4][4];
    boolean b[][] = new boolean[4][4];
    ArrayList<String> a1 = new ArrayList<String>();
    ArrayList<String> a2 = new ArrayList<String>();
    String fullString;
    Clipboard clipboard;
    JTextArea jt1,jt2;
    
    public frame() throws IOException {
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        int x = 700;
        int y = 600;
        jf = new JFrame("4*4*4光立方数组生成");
        jf.setVisible(true);
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int)screensize.getWidth();
        int screenHeight = (int)screensize.getHeight();
        p = new Panel();
        p.setLayout(new GridLayout(6,4));
        jf.add(p);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        button();
        for(int i = 0;i < 4;i++){
            for(int j = 0;j < 4;j++){
                b[i][j] = true;
            }
        }
        getHexSource();
        AddHexSourceUnclear();
        AddHexSourceClear();
        OutHexSource();
        textBox1();
        textBox2();
        jf.pack();
        jf.setBounds((screenWidth-x)/2,(screenHeight-y)/2,x,y);
    }
    
    public void button(){
        for(int i = 0;i < 4;i++){
            for(int j = 0;j < 4;j++){
                jb[i][j] = new JButton((i+1)+"-"+(j+1));
                p.add(jb[i][j]);
                jb[i][j].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String command = e.getActionCommand();
                        int commandx = Integer.parseInt(command.substring(0,1));
                        int commandy = Integer.parseInt(command.substring(2));
                        commandx--;
                        commandy--;
                        if(b[commandx][commandy]){
                            jb[commandx][commandy].setBackground(Color.blue);
                        }else{
                            jb[commandx][commandy].setBackground(Color.white);
                        }
                        b[commandx][commandy] = !b[commandx][commandy];
                    }
                });
            }
        }
    }
    
    public void getHexSource(){
        JButton jB = new JButton("重置");
        p.add(jB);
        jB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i = 0;i < 4;i++){
                    for(int j = 0;j < 4;j++){
                        jb[i][j].setBackground(Color.white);
                        b[i][j] = true;
                    }
                }
                a1.clear();
                a2.clear();
                jt1.setText("");
                jt2.setText("");
                code1 = "";
                code2 = "";
            }
        });
        
    }
    
    public void calSource(){
        x=0;
        y=0;
        for(int i = 0;i < 4;i++){
                    for(int j = 0;j < 4;j++){
                        if(i<2){
                            if(b[i][j]){
                                x += 1;
                                x = x << 1;
                            }else{
                                x = x << 1;
                            }
                        }else{
                            if(b[i][j]){
                                y += 1;
                                y = y << 1;
                            }else{
                                y = y << 1;
                            }
                        }
                    }
                }
                x = x>>1;
                y = y>>1;
    }
    
    public void AddHexSourceUnclear(){
        JButton jb = new JButton("添加代码(不清空)");
        p.add(jb);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                calSource();
                cheakAddHex();
                try {
                    getcode(false);
                } catch (IOException ex) {
                }
                jt1.setText(code1);
                jt2.setText(code2);
            }
        });
    }
    
    public void AddHexSourceClear(){
        JButton jB = new JButton("添加代码(清空)");
        p.add(jB);
        jB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                calSource();
                cheakAddHex();
                try {
                    getcode(false);
                } catch (IOException ex) {
                }
                jt1.setText(code1);
                jt2.setText(code2);
                for(int i = 0;i < 4;i++){
                    for(int j = 0;j < 4;j++){
                        jb[i][j].setBackground(Color.white);
                        b[i][j] = true;
                    }
                }
            }
        });
    }
    
    public void cheakAddHex(){
        if(x <= 16){
            xHex = "0x0" + Integer.toHexString(x);
        }else{
            xHex = "0x" + Integer.toHexString(x);
        }
        if(y <= 16){
            yHex = "0x0" + Integer.toHexString(y);
        }else{
            yHex = "0x" + Integer.toHexString(y);
        }
        a1.add(xHex);
        a2.add(yHex);
    }
    
    public void getcode(boolean b) throws IOException{
        code1 = "{";
        for(String s : a1){
            code1 += s + ",";
        }
        code1 += "0xff};";
        code2 = "{";
        for(String s : a2){
            code2 += s + ",";
        }
        code2 += "0xff};";
        fullString = code1+"\n"+code2;
        if(b){
            writeFile(fullString);
        }
    }
    
    public void OutHexSource() throws IOException{
        JButton jb = new JButton("输出代码");
        p.add(jb);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    fileInit();
                } catch (IOException ex) {
                    
                }
                System.out.print("{");
                try {
                    getcode(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                jt1.setText(code1);
                jt2.setText(code2);
            }
        });
    }
    public void fileInit() throws IOException{
        srcAdress = System.getProperty("user.dir");
        File f = new File(srcAdress+"\\代码.txt");
        if(!f.exists()){
            f.createNewFile();
        }
    }
    
    public void writeFile(String s) throws IOException{
        FileWriter fileWriter = new FileWriter(srcAdress+"\\代码.txt");
        fileWriter.write(s);
        fileWriter.close();
    }
    
    public void textBox1(){
        JButton jb = new JButton("复制a1代码");
        jt1 = new JTextArea();
        jt1.setToolTipText("a1代码");
        jt1.setLineWrap(true);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.selectAll();
                StringSelection selection = new StringSelection(jt1.getText());
                clipboard.setContents(selection, null);
            }
        });
        p.add(jt1);
        p.add(jb);
    }
    
    public void textBox2(){
        JButton jb = new JButton("复制a2代码");
        jt2 = new JTextArea();
        jt2.setToolTipText("a2代码");
        jt2.setLineWrap(true);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt2.selectAll();
                StringSelection selection = new StringSelection(code2);
                clipboard.setContents(selection, null);
            }
        });
        p.add(jt2);
        p.add(jb);
    }
    
}
1.0版本 frame.java

构建的jar下载地址(百度网盘)(提取码:0000)

实在难看的界面

 

2.0版本(伪立体取模)

main.java(直接写在一个里了)

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.datatransfer.Clipboard;
  4 import java.awt.datatransfer.StringSelection;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import static java.lang.Integer.toHexString;
  8 import java.util.ArrayList;
  9 import java.util.Scanner;
 10 
 11 
 12 public class main {
 13     public static void main(String args[]){
 14         new frame();
 15     }
 16 }
 17 
 18 
 19 class frame{
 20     
 21     int x = 1000;//屏幕长度
 22     int y = 650;//屏幕宽度
 23     int HexA;
 24     int HexB;
 25     String HexAChecked;
 26     String HexBChecked;
 27     String command1;
 28     String command2;
 29     
 30     boolean copyCode[] = new boolean[4];
 31     boolean bool[][][] = new boolean[4][4][4];
 32     String code1[] = new String[4];
 33     String code2[] = new String[4];
 34     String codeString[][] = new String[2][4];
 35     
 36     ArrayList<String> a1 = new ArrayList<String>();
 37     ArrayList<String> a2 = new ArrayList<String>();
 38     
 39     ArrayList<String> b1 = new ArrayList<String>();
 40     ArrayList<String> b2 = new ArrayList<String>();
 41     
 42     ArrayList<String> c1 = new ArrayList<String>();
 43     ArrayList<String> c2 = new ArrayList<String>();
 44     
 45     ArrayList<String> d1 = new ArrayList<String>();
 46     ArrayList<String> d2 = new ArrayList<String>();
 47     //存生成的代码
 48     
 49     Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();//剪切板
 50     
 51     JFrame jf = new JFrame("4*4*4光立方取模");
 52     Panel p = new Panel();
 53     Panel p1 = new Panel();//放灯
 54     Panel p2 = new Panel();//放按钮
 55     Panel p3 = new Panel();//放代码
 56     JButton jb[][][] = new JButton[4][4][4];//灯的按钮
 57     JButton jbS[][] = new JButton[4][4];//快捷按钮
 58     JButton jbD[] = new JButton[4];//基本按钮
 59     JButton jbC[] = new JButton[4];//复制按钮
 60     JTextArea jt[] = new JTextArea[4];//实时显示生成的代码
 61     
 62     StringSelection selection[] = new StringSelection[4];
 63     
 64     Color blue = new Color(51,204,255);
 65     Color flesh = new Color(255,175,122);
 66     
 67     
 68     public frame() {
 69         frameInit();
 70         lightButtonInit();
 71         shortcutButtonInit();
 72         buttonInit();
 73         textAreaInit();
 74                 
 75         jf.setVisible(true);
 76     }
 77     
 78     //重置数组
 79     public void resetBool(){
 80         for(int i1 = 0;i1 < 4;i1++){
 81             for(int i2 = 0;i2 < 4;i2++){
 82                 for(int i3 = 0;i3 < 4;i3++){
 83                     bool[i1][i2][i3] = false;
 84                 }
 85             }
 86         }
 87     }
 88     
 89     //重置按钮颜色和数组
 90     public void resetButton(){
 91         for(int i1 = 0;i1 < 4;i1++){
 92             for(int i2 = 0;i2 < 4;i2++){
 93                 for(int i3 = 0;i3 < 4;i3++){
 94                     if(bool[i1][i2][i3]){
 95                         jb[i1][i2][i3].setBackground(blue);
 96                     }else{
 97                         jb[i1][i2][i3].setBackground(flesh);
 98                     }
 99                     
100                 }
101             }
102         }
103     }
104     
105     //重置文本框
106     public void resetTextArea(){
107         for(int i1 = 0;i1 < 4;i1++){
108             jt[i1].setText("");
109         }
110     }
111     
112     //初始化框架
113     public void frameInit(){
114         
115         Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
116         int screenWidth = (int)screensize.getWidth();
117         int screenHeight = (int)screensize.getHeight();
118         jf.setBounds((screenWidth-x)/2,(screenHeight-y)/2,x,y);
119         jf.setDefaultCloseOperation(3);
120         jf.setLayout(null);
121         jf.setResizable(false);
122         
123         p.setBounds(0, 0, x, y);
124         jf.add(p);
125         p.setLayout(null);
126         p.setBackground(Color.DARK_GRAY);
127         
128         p1.setLayout(null);
129         //p1.setBackground(Color.red);
130         p1.setBounds(30, 30, 930, 250);
131         p.add(p1);
132         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
133         
134         
135         p2.setLayout(new GridLayout(2,8));
136         //p2.setBackground(Color.green);
137         p2.setBounds(30, 290, 930, 100);
138         p.add(p2);
139         
140         p3.setLayout(null);
141         //p3.setBackground(Color.blue);
142         p3.setBounds(30, 400, 930, 170);
143         p.add(p3);
144         jf.setResizable(false);
145     }
146     
147     //初始化灯按钮
148     public void lightButtonInit(){
149         for(int i1 = 0;i1 < 4;i1++){
150             for(int i2 = 0;i2 < 4;i2++){
151                 for(int i3 = 0;i3 < 4;i3++){
152                     jb[i1][i2][i3] = new JButton();
153                     jb[i1][i2][i3].setActionCommand("jb "+ (i1+1) + "-" + (i2+1) + "-" + (i3+1));
154                     jb[i1][i2][i3].setBackground(flesh);
155                     jb[i1][i2][i3].setBounds( 40+ (i1*4+i3)*50 + i1*20,210 - i2*50 - i3 * 20, 40, 40);
156                     p1.add(jb[i1][i2][i3]);
157                     addActionListener(jb[i1][i2][i3]);
158                 }
159             }
160         }
161     }
162     
163     //初始化快捷键按钮 全亮 , 全暗 , 复制到下一面,复制到上一面
164     public void shortcutButtonInit(){
165         for(int i1 = 0;i1 < 4;i1++){
166             jbS[i1][0] = new JButton("全亮");
167             jbS[i1][1] = new JButton("全灭");
168             jbS[i1][2] = new JButton("复制到上一面");
169             jbS[i1][3] = new JButton("复制到下一面");
170             p2.add(jbS[i1][0]);
171             p2.add(jbS[i1][1]);
172             for(int i2 = 0;i2<4;i2++){
173                 addActionListener(jbS[i1][i2]);
174                 jbS[i1][i2].setActionCommand("jbS " + i1 + "-" + i2);
175             }
176         }
177         for(int i1 = 0;i1 < 4;i1++){
178             p2.add(jbS[i1][2]);
179             p2.add(jbS[i1][3]);
180         }
181         jbS[0][2].setEnabled(false);
182         jbS[3][3].setEnabled(false);
183     }
184     
185     //初始化按钮 添加,取模,清空,重置
186     public void buttonInit(){
187         jbD[0] = new JButton("添加");
188         jbD[0].setBounds(695, 20, 100, 90);
189         
190         jbD[1] = new JButton("取模");
191         jbD[1].setBounds(805, 20, 100, 90);
192         
193         jbD[2] = new JButton("清空");
194         jbD[2].setBounds(698, 130, 100, 30);
195         
196         jbD[3] = new JButton("重置");
197         jbD[3].setBounds(805, 130, 100, 30);
198         for(int i1 = 0;i1 < 4;i1++){
199             jbD[i1].setActionCommand("jbD "+i1);
200             p3.add(jbD[i1]);
201             addActionListener(jbD[i1]);
202         }
203     }
204     
205     //初始化文字输出框 1, 2, 3, 4,复制按钮
206     public void textAreaInit(){
207         for(int i1 = 0;i1 < 4;i1++){
208             jt[i1] = new JTextArea();
209             jt[i1].setBounds(5, 8+ 40*i1, 645, 35);
210             p3.add(jt[i1]);
211         }
212         for(int i1 = 0;i1 < 4;i1++){
213             jbC[i1] = new JButton("复制");
214             jbC[i1].setActionCommand("jbC " + i1);
215             jbC[i1].setBounds(650, 8+ 40*i1, 35, 35);
216             p3.add(jbC[i1]);
217             addActionListener(jbC[i1]);
218         }
219     }
220     
221     //初始化剪切板
222     public void selectionInit(int i){
223         selection[i] = new StringSelection(jt[i].getText());
224     }
225     
226     //显示于文本框
227     public void display(){
228         addHexCode();
229         for(int i = 0;i<4;i++){
230             jt[i].setText(code1[i] + "\n" + code2[i]);
231         }
232     }
233     
234     //复制到下一面
235     public void copyDown(int n){
236         for(int i1 = 0;i1<4;i1++){
237             for(int i2 = 0;i2<4;i2++){
238                 bool[n+1][i1][i2] = bool[n][i1][i2];
239             }
240         }
241         resetButton();
242     }
243     
244     //复制到上一面
245     public void copyUp(int n){
246         for(int i1 = 0;i1<4;i1++){
247             for(int i2 = 0;i2<4;i2++){
248                 bool[n-1][i1][i2] = bool[n][i1][i2];
249             }
250         }
251         resetButton();
252     }
253     
254     //单面亮暗
255     public void lightOrDark(boolean b,int page){
256         for(int i1 = 0;i1<4;i1++){
257             for(int i2 = 0;i2<4;i2++){
258                 bool[page][i1][i2] = b;
259             }
260         }
261         resetButton();
262     }
263     
264     //按钮事件监听
265     public void addActionListener(JButton saveButton) {
266         
267         saveButton.addActionListener(new ActionListener() {
268             @Override
269             public void actionPerformed(ActionEvent e) {
270                 String command;
271                 command = e.getActionCommand();
272                 //System.out.println(command);
273                 Scanner sc = new Scanner(command);
274                 command1 = sc.next();
275                 command2 = sc.next();
276                 switch(command1){
277                     case "jb": 
278                         int a,b,c;
279                         a = Integer.parseInt(command2.substring(0,1));
280                         b = Integer.parseInt(command2.substring(2,3));
281                         c = Integer.parseInt(command2.substring(4));
282                         a--;
283                         b--;
284                         c--;
285                         if(bool[a][b][c]){
286                             jb[a][b][c].setBackground(flesh);
287                         }else{
288                             jb[a][b][c].setBackground(blue);
289                         }
290                         bool[a][b][c] = !bool[a][b][c];//变蓝色后存true;
291                         //System.out.println(bool[a][b][c]);
292                         break;
293                         
294                     case "jbS": 
295                         int page = Integer.parseInt(command2.substring(0,1));
296                         int id = Integer.parseInt(command2.substring(2));
297                         
298                         System.out.println(page + " " + id);
299                         switch(id){
300                             //全亮
301                             case 0:
302                                 lightOrDark(true,page);
303                                 break;
304                             //全灭
305                             case 1:
306                                 lightOrDark(false,page);
307                                 break;
308                             //复制到上一面
309                             case 2:
310                                 copyUp(page);
311                                 break;
312                             //复制到下一面
313                             case 3:
314                                 copyDown(page);
315                                 break;
316                         }
317                         break;
318                         
319                     case "jbD": 
320                         switch(Integer.parseInt(command2)){
321                             //添加
322                             case 0:
323                                 calAddHexCode();
324                                 display();
325                                 break;
326                             //取模
327                             case 1:
328                                 
329                                 break;
330                             //清空
331                             case 2:
332                                 resetBool();
333                                 resetButton();
334                                 break;
335                             //重置
336                             case 3:
337                                 resetTextArea();
338                                 resetBool();
339                                 resetButton();
340                                 a1.clear();
341                                 a2.clear();
342                                 b1.clear();
343                                 b2.clear();
344                                 c1.clear();
345                                 c2.clear();
346                                 d1.clear();
347                                 d2.clear();
348                                 break;
349                         }
350                         break;
351                         
352                     case "jbC": 
353                         int i = Integer.parseInt(command2);
354                         selectionInit(i);
355                         clipboard.setContents(selection[i], null);
356                         break;
357                         
358                 }
359                 
360                 sc.close();
361                 
362             }
363         });
364     }
365     
366     //计算所有的Hex代码
367     public void calAddHexCode(){
368         for(int i2 = 0;i2 < 4;i2++){
369             HexA = 0;
370             HexB = 0;
371             for(int i1 = 0;i1 < 4;i1++){
372                 for(int i3 = 0;i3 < 4;i3++){
373                     if(i1<2){
374                         if(!bool[i1][i2][i3]){
375                             HexA += 1;
376                             HexA = HexA << 1;
377                         }else{
378                             HexA = HexA << 1;
379                         }
380                     }else{
381                         if(!bool[i1][i2][i3]){
382                             HexB += 1;
383                             HexB = HexB << 1;
384                         }else{
385                             HexB = HexB << 1;
386                         }
387                     }
388                 }
389             }
390             HexA = HexA >> 1;
391             HexB = HexB >> 1;
392             //System.out.println(HexA + " " + HexB);
393             //System.out.println(toHexString(HexA) + " " + toHexString(HexB) );
394             checkHexCode();
395             switch(i2){
396                 case 0: 
397                     a1.add(HexAChecked);
398                     a2.add(HexBChecked);
399                     break;
400                     
401                 case 1: 
402                     b1.add(HexAChecked);
403                     b2.add(HexBChecked);
404                     break;
405                     
406                 case 2: 
407                     c1.add(HexAChecked);
408                     c2.add(HexBChecked);
409                     break;
410                     
411                 case 3: 
412                     d1.add(HexAChecked);
413                     d2.add(HexBChecked);
414                     break;
415             }
416         }
417     }
418     
419     //核对Hex代码规范格式
420     public void checkHexCode(){
421         if(HexA <= 16){
422             HexAChecked = "0x0" + Integer.toHexString(HexA);
423         }else{
424             HexAChecked = "0x" + Integer.toHexString(HexA);
425         }
426         if(HexB <= 16){
427             HexBChecked = "0x0" + Integer.toHexString(HexB);
428         }else{
429             HexBChecked = "0x" + Integer.toHexString(HexB);
430         }
431         System.out.println(HexAChecked + " " + HexBChecked);
432     }
433     
434     //叠加所有的Hex代码
435     public void addHexCode(){
436         for(int i = 0;i < 4;i++){
437             code1[i] = "{";
438         }
439         for(int i = 0;i < 4;i++){
440             code2[i] = "{";
441         }
442         
443         for(String s : a1){
444             code1[0] += s + ",";
445         }
446         for(String s : a2){
447             code2[0] += s + ",";
448         }
449         for(String s : b1){
450             code1[1] += s + ",";
451         }
452         for(String s : b2){
453             code2[1] += s + ",";
454         }
455         for(String s : c1){
456             code1[2] += s + ",";
457         }
458         for(String s : c2){
459             code2[2] += s + ",";
460         }
461         for(String s : d1){
462             code1[3] += s + ",";
463         }
464         for(String s : d2){
465             code2[3] += s + ",";
466         }
467         
468         for(int i = 0;i < 4;i++){
469             code1[i] += "0xff};";
470             
471         }
472         for(int i = 0;i < 4;i++){
473             code2[i] += "0xff};";
474         }
475     }
476 }
View Code

构建的jar下载地址(百度网盘)(提取码:0000)

不那么难看的界面

 

 2.1版本(伪立体取模)(更改了生成的代码,keil里仅需写好2个循环)

main.java

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.datatransfer.Clipboard;
  4 import java.awt.datatransfer.StringSelection;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.awt.event.KeyAdapter;
  8 import java.awt.event.KeyEvent;
  9 import java.awt.event.KeyListener;
 10 import static java.lang.Integer.toHexString;
 11 import java.util.ArrayList;
 12 import java.util.Scanner;
 13 
 14 
 15 public class main {
 16     public static void main(String args[]){
 17         new frame();
 18     }
 19 }
 20 
 21 
 22 class frame{
 23     
 24     int x = 1000;//屏幕长度
 25     int y = 650;//屏幕宽度
 26     int HexA;
 27     int HexB;
 28     String HexAChecked;
 29     String HexBChecked;
 30     String command1;
 31     String command2;
 32     String AStart = "code uint lightA[]=";
 33     String BStart = "code uint lightB[]=";
 34     
 35     boolean copyCode[] = new boolean[4];
 36     boolean bool[][][] = new boolean[4][4][4];
 37     String codeA;
 38     String codeB;
 39     String codeString[][] = new String[2][4];
 40     
 41     ArrayList<String> lightA = new ArrayList<String>();
 42     ArrayList<String> lightB = new ArrayList<String>();
 43     
 44     //存生成的代码
 45     
 46     Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();//剪切板
 47     
 48     JFrame jf = new JFrame("4*4*4光立方取模");
 49     Panel p = new Panel();
 50     Panel p1 = new Panel();//放灯
 51     Panel p2 = new Panel();//放按钮
 52     Panel p3 = new Panel();//放代码
 53     JButton jb[][][] = new JButton[4][4][4];//灯的按钮
 54     JButton jbS[][] = new JButton[4][4];//快捷按钮
 55     JButton jbD[] = new JButton[4];//基本按钮
 56     JTextArea jt[] = new JTextArea[2];//实时显示生成的代码
 57     
 58     StringSelection selection;
 59     
 60     Color blue = new Color(51,204,255);
 61     Color flesh = new Color(255,175,122);
 62     Color lightCoral = new Color(240,128,128);
 63     
 64     
 65     public frame() {
 66         frameInit();
 67         lightButtonInit();
 68         shortcutButtonInit();
 69         buttonInit();
 70         textAreaInit();
 71         
 72         jf.setVisible(true);
 73     }
 74     
 75     //重置数组
 76     public void resetBool(){
 77         for(int i1 = 0;i1 < 4;i1++){
 78             for(int i2 = 0;i2 < 4;i2++){
 79                 for(int i3 = 0;i3 < 4;i3++){
 80                     bool[i1][i2][i3] = false;
 81                 }
 82             }
 83         }
 84     }
 85     
 86     //重置按钮颜色和数组
 87     public void resetButton(){
 88         for(int i1 = 0;i1 < 4;i1++){
 89             for(int i2 = 0;i2 < 4;i2++){
 90                 for(int i3 = 0;i3 < 4;i3++){
 91                     if(bool[i1][i2][i3]){
 92                         jb[i1][i2][i3].setBackground(blue);
 93                     }else{
 94                         jb[i1][i2][i3].setBackground(flesh);
 95                     }
 96                     
 97                 }
 98             }
 99         }
100     }
101     
102     //重置文本框
103     public void resetTextArea(){
104         for(int i1 = 0;i1 < 2;i1++){
105             jt[i1].setText("");
106         }
107     }
108     
109     //初始化框架
110     public void frameInit(){
111         
112         Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
113         int screenWidth = (int)screensize.getWidth();
114         int screenHeight = (int)screensize.getHeight();
115         jf.setBounds((screenWidth-x)/2,(screenHeight-y)/2,x,y);
116         jf.setDefaultCloseOperation(3);
117         jf.setLayout(null);
118         jf.setResizable(false);
119         
120         p.setBounds(0, 0, x, y);
121         jf.add(p);
122         p.setLayout(null);
123         p.setBackground(Color.DARK_GRAY);
124         
125         p1.setLayout(null);
126         //p1.setBackground(Color.red);
127         p1.setBounds(30, 30, 930, 250);
128         p.add(p1);
129         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
130         
131         
132         p2.setLayout(new GridLayout(2,8));
133         //p2.setBackground(Color.green);
134         p2.setBounds(30, 290, 930, 100);
135         p.add(p2);
136         
137         p3.setLayout(null);
138         //p3.setBackground(Color.blue);
139         p3.setBounds(30, 400, 930, 170);
140         p.add(p3);
141         jf.setResizable(false);
142     }
143     
144     //初始化灯按钮
145     public void lightButtonInit(){
146         for(int i1 = 0;i1 < 4;i1++){
147             for(int i2 = 0;i2 < 4;i2++){
148                 for(int i3 = 0;i3 < 4;i3++){
149                     jb[i1][i2][i3] = new JButton();
150                     jb[i1][i2][i3].setActionCommand("jb "+ (i1+1) + "-" + (i2+1) + "-" + (i3+1));
151                     jb[i1][i2][i3].setBackground(flesh);
152                     jb[i1][i2][i3].setBounds( 40+ (i1*4+i3)*50 + i1*20,210 - i2*50 - i3 * 20, 40, 40);
153                     p1.add(jb[i1][i2][i3]);
154                     addActionListener(jb[i1][i2][i3]);
155                 }
156             }
157         }
158     }
159     
160     //初始化快捷键按钮 全亮 , 全暗 , 复制到下一面,复制到上一面
161     public void shortcutButtonInit(){
162         for(int i1 = 0;i1 < 4;i1++){
163             jbS[i1][0] = new JButton("全亮");
164             jbS[i1][1] = new JButton("全灭");
165             jbS[i1][2] = new JButton("复制到上一面");
166             jbS[i1][3] = new JButton("复制到下一面");
167             p2.add(jbS[i1][0]);
168             p2.add(jbS[i1][1]);
169             for(int i2 = 0;i2<4;i2++){
170                 addActionListener(jbS[i1][i2]);
171                 jbS[i1][i2].setActionCommand("jbS " + i1 + "-" + i2);
172             }
173         }
174         for(int i1 = 0;i1 < 4;i1++){
175             p2.add(jbS[i1][2]);
176             p2.add(jbS[i1][3]);
177         }
178         //jbS[0][2].setEnabled(false);
179         //jbS[3][3].setEnabled(false);
180     }
181     
182     //初始化按钮 添加,取模,清空,重置
183     public void buttonInit(){
184         jbD[0] = new JButton("添加");
185         jbD[0].setBounds(695, 20, 100, 90);
186         
187         jbD[1] = new JButton("取模");
188         jbD[1].setBounds(805, 20, 100, 90);
189         
190         jbD[2] = new JButton("清空");
191         jbD[2].setBounds(695, 130, 100, 30);
192         
193         jbD[3] = new JButton("重置");
194         jbD[3].setBounds(805, 130, 100, 30);
195         for(int i1 = 0;i1 < 4;i1++){
196             jbD[i1].setActionCommand("jbD "+i1);
197             p3.add(jbD[i1]);
198             addActionListener(jbD[i1]);
199         }
200     }
201     
202     //初始化文字输出框 复制按钮
203     public void textAreaInit(){
204         for(int i1 = 0;i1 < 2;i1++){
205             jt[i1] = new JTextArea();
206             jt[i1].setBounds(5, 8+ 85*i1, 645, 80);
207             jt[i1].setLineWrap(true);
208             jt[i1].setAutoscrolls(true);
209             jt[i1].setBackground(lightCoral);
210             p3.add(jt[i1]);
211         }   
212     }
213     
214     //初始化剪切板
215     public void selectionInit(){
216         selection = new StringSelection(jt[0].getText() + "\n" + jt[1].getText());
217     }
218     
219     //显示于文本框
220     public void display(){
221         addHexCode();
222         jt[0].setText(codeA);
223         jt[1].setText(codeB);
224     }
225     
226     //复制到下一面
227     public void copyDown(int n){
228         if(n!=3){
229             for(int i1 = 0;i1<4;i1++){
230                 for(int i2 = 0;i2<4;i2++){
231                     bool[n+1][i1][i2] = bool[n][i1][i2];
232                 }
233             }
234         }else{
235             for(int i1 = 0;i1<4;i1++){
236                 for(int i2 = 0;i2<4;i2++){
237                     bool[0][i1][i2] = bool[n][i1][i2];
238                 }
239             }
240         }
241         
242         resetButton();
243     }
244     
245     //复制到上一面
246     public void copyUp(int n){
247         if(n!=0){
248             for(int i1 = 0;i1<4;i1++){
249                 for(int i2 = 0;i2<4;i2++){
250                     bool[n-1][i1][i2] = bool[n][i1][i2];
251                 }
252             }
253         }else{
254             for(int i1 = 0;i1<4;i1++){
255                 for(int i2 = 0;i2<4;i2++){
256                     bool[3][i1][i2] = bool[n][i1][i2];
257                 }
258             }
259         }
260         resetButton();
261     }
262     
263     //单面亮暗
264     public void lightOrDark(boolean b,int page){
265         for(int i1 = 0;i1<4;i1++){
266             for(int i2 = 0;i2<4;i2++){
267                 bool[page][i1][i2] = b;
268             }
269         }
270         resetButton();
271     }
272     
273     //按钮事件监听
274     public void addActionListener(JButton saveButton) {
275         
276         saveButton.addActionListener(new ActionListener() {
277             @Override
278             public void actionPerformed(ActionEvent e) {
279                 String command;
280                 command = e.getActionCommand();
281                 //System.out.println(command);
282                 Scanner sc = new Scanner(command);
283                 command1 = sc.next();
284                 command2 = sc.next();
285                 switch(command1){
286                     case "jb": 
287                         int a,b,c;
288                         a = Integer.parseInt(command2.substring(0,1));
289                         b = Integer.parseInt(command2.substring(2,3));
290                         c = Integer.parseInt(command2.substring(4));
291                         a--;
292                         b--;
293                         c--;
294                         if(bool[a][b][c]){
295                             jb[a][b][c].setBackground(flesh);
296                         }else{
297                             jb[a][b][c].setBackground(blue);
298                         }
299                         bool[a][b][c] = !bool[a][b][c];//变蓝色后存true;
300                         //System.out.println(bool[a][b][c]);
301                         break;
302                         
303                     case "jbS": 
304                         int page = Integer.parseInt(command2.substring(0,1));
305                         int id = Integer.parseInt(command2.substring(2));
306                         
307                         //System.out.println(page + " " + id);
308                         switch(id){
309                             //全亮
310                             case 0:
311                                 lightOrDark(true,page);
312                                 break;
313                             //全灭
314                             case 1:
315                                 lightOrDark(false,page);
316                                 break;
317                             //复制到上一面
318                             case 2:
319                                 copyUp(page);
320                                 break;
321                             //复制到下一面
322                             case 3:
323                                 copyDown(page);
324                                 break;
325                         }
326                         break;
327                         
328                     case "jbD": 
329                         switch(Integer.parseInt(command2)){
330                             //添加
331                             case 0:
332                                 calAddHexCode();
333                                 display();
334                                 break;
335                             //取模
336                             case 1:
337                                 selectionInit();
338                                 clipboard.setContents(selection, null);
339                                 break;
340                             //清空
341                             case 2:
342                                 resetBool();
343                                 resetButton();
344                                 break;
345                             //重置
346                             case 3:
347                                 resetTextArea();
348                                 resetBool();
349                                 resetButton();
350                                 lightA.clear();
351                                 lightB.clear();
352                                 break;
353                         }
354                         break;
355                 }
356                 
357                 sc.close();
358                 
359             }
360         });
361     }
362     
363     //计算所有的Hex代码
364     public void calAddHexCode(){
365         for(int i2 = 0;i2 < 4;i2++){
366             HexA = 0;
367             HexB = 0;
368             for(int i1 = 0;i1 < 4;i1++){
369                 for(int i3 = 0;i3 < 4;i3++){
370                     if(i1<2){
371                         if(!bool[i1][i2][i3]){
372                             HexA += 1;
373                             HexA = HexA << 1;
374                         }else{
375                             HexA = HexA << 1;
376                         }
377                     }else{
378                         if(!bool[i1][i2][i3]){
379                             HexB += 1;
380                             HexB = HexB << 1;
381                         }else{
382                             HexB = HexB << 1;
383                         }
384                     }
385                 }
386             }
387             HexA = HexA >> 1;
388             HexB = HexB >> 1;
389             //System.out.println(HexA + " " + HexB);
390             //System.out.println(toHexString(HexA) + " " + toHexString(HexB) );
391             checkHexCode();
392             lightA.add(HexAChecked);
393             lightB.add(HexBChecked);
394         }
395     }
396     
397     //核对Hex代码规范格式
398     public void checkHexCode(){
399         if(HexA <= 16){
400             HexAChecked = "0x0" + Integer.toHexString(HexA);
401         }else{
402             HexAChecked = "0x" + Integer.toHexString(HexA);
403         }
404         if(HexB <= 16){
405             HexBChecked = "0x0" + Integer.toHexString(HexB);
406         }else{
407             HexBChecked = "0x" + Integer.toHexString(HexB);
408         }
409         //System.out.println(HexAChecked + " " + HexBChecked);
410     }
411     
412     //叠加所有的Hex代码
413     public void addHexCode(){
414         codeA = AStart + " \n{";
415         codeB = BStart + " \n{";
416         
417         for(String s : lightA){
418             codeA += s + ",";
419         }
420         for(String s : lightB){
421             codeB += s + ",";
422         }
423         codeA += "0xff};";
424         codeB += "0xff};";
425     }
426 }
View Code

构建的jar下载地址(百度网盘)(提取码:0000)

也许更好看了点的界面

 

 

之后估计还会再再搞其他版本,觉得还行就点个赞吧ORZ,有啥意见评论下

标签:取模,java,单片机,i1,i3,i2,int,源码,new
来源: https://www.cnblogs.com/AnyWriter/p/13547350.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有