|
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JSeparator;
- import javax.swing.JTabbedPane;
- import javax.swing.JTextArea;
- import javax.swing.SwingUtilities;
- import javax.swing.WindowConstants;
- import lejos.pc.comm.NXTConnector;
- import org.kociemba.twophase.Search;
- /**
- * This code was edited or generated using CloudGarden's Jigloo
- * SWT/Swing GUI Builder, which is free for non-commercial
- * use. If Jigloo is being used commercially (ie, by a corporation,
- * company or business for any purpose whatever) then you
- * should purchase a license for each developer using Jigloo.
- * Please visit www.cloudgarden.com for details.
- * Use of Jigloo implies acceptance of these licensing terms.
- * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
- * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
- * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
- */
- public class NewJFrame extends javax.swing.JFrame implements ActionListener{
- {
- //Set Look & Feel
- try {
- javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Simple JavaDoc
- * @author Michael Peng
- * Set the Version of the main program
- */
- private static final long serialVersionUID = 6338002822670303714L;
- // console string
- private static final String con = null;
- private JLabel copyright;
- private JButton dT;
- private JButton dU;
- private JButton dD;
- private JButton uT;
- private JButton uU;
- private JButton uD;
- private JButton rT;
- private JButton rU;
- private JButton rD;
- private JButton fT;
- private JButton fU;
- private JButton fD;
- private JButton lT;
- private JButton lU;
- private JButton lD;
- private JButton bT;
- private JButton bU;
- private JButton bD;
- private JPanel dmov;
- private JPanel umov;
- private JPanel rmov;
- private JPanel fmov;
- private JPanel lmov;
- private JPanel bmov;
- private JTabbedPane actionspane;
- private JPanel Actions;
- private JSeparator jSeparator1;
- private JLabel Note;
- private JTextArea Cube;
- private JPanel CubePanel;
- private JButton solve;
- private JPanel jPanel1;
- private JLabel debug;
- private JButton test;
- private JButton disconnect;
- private JPanel ConnectionDebug;
- private JPanel mainPanel;
- private JButton connect;
- private JTabbedPane ActionPane;
- private JTextArea Console;
- private static NXTConnector conn = new NXTConnector();
- private DataOutputStream dos;
- private DataInputStream dis;
- /**
- * Auto-generated main method to display this JFrame
- */
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- NewJFrame inst = new NewJFrame();
- inst.setLocationRelativeTo(null);
- inst.setVisible(true);
- }
- });
- }
-
- public NewJFrame() {
- super();
- initGUI();
- }
-
- private void initGUI() {
- try {
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- this.setTitle("MindCuber Console Pro v1.01");
- {
- mainPanel = new JPanel();
- getContentPane().add(mainPanel, BorderLayout.CENTER);
- mainPanel.setPreferredSize(new java.awt.Dimension(416, 330));
- {
- connect = new JButton();
- mainPanel.add(connect);
- connect.setText("Connect!");
- connect.setPreferredSize(new java.awt.Dimension(224, 37));
- connect.addActionListener(this);
- }
- {
- ActionPane = new JTabbedPane();
- mainPanel.add(ActionPane);
- mainPanel.add(getCopyright());
- ActionPane.setPreferredSize(new java.awt.Dimension(329, 255));
- {
- Console = new JTextArea(con);
- ActionPane.addTab("Console", null, Console, null);
- Console.setPreferredSize(new java.awt.Dimension(429, 226));
- ActionPane.addTab("Connection Debug", null, getConnectionDebug(), null);
- ActionPane.addTab("Cube", null, getJPanel1(), null);
- ActionPane.addTab("Actions", null, getActions(), null);
- }
- }
- }
- pack();
- this.setSize(432, 368);
- } catch (Exception e) {
- //add your error handling code here
- e.printStackTrace();
- }
- }
- private JPanel getConnectionDebug() {
- if(ConnectionDebug == null) {
- ConnectionDebug = new JPanel();
- ConnectionDebug.add(getDisconnect());
- ConnectionDebug.add(getTest());
- ConnectionDebug.add(getDebug());
- ConnectionDebug.add(getSolve());
- ConnectionDebug.add(getJSeparator1());
- }
- return ConnectionDebug;
- }
-
- private JButton getDisconnect() {
- if(disconnect == null) {
- disconnect = new JButton();
- disconnect.setText("Disconnect!");
- disconnect.setPreferredSize(new java.awt.Dimension(132, 71));
- disconnect.addActionListener(this);
- }
- return disconnect;
- }
-
- private JButton getTest() {
- if(test == null) {
- test = new JButton();
- test.setPreferredSize(new java.awt.Dimension(129, 71));
- test.setText("Test Connection!");
- test.addActionListener(this);
- }
- return test;
- }
-
- private JLabel getDebug() {
- if(debug == null) {
- debug = new JLabel();
- debug.setText("waiting...");
- debug.setPreferredSize(new java.awt.Dimension(234, 15));
- }
- return debug;
- }
- public void actionPerformed(ActionEvent e){
- Console.setLineWrap(true);
- if (e.getSource() == connect)
- {
- try {
- conn.connectTo();
- dos = new DataOutputStream(conn.getOutputStream());
- dis = new DataInputStream(conn.getInputStream());
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- Console.setText("connected");
- }
- else if (e.getSource() == disconnect)
- {
- try {
- conn.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- Console.setText("Disconnected");
- }
- else if (e.getSource() == solve)
- {
- CubeStr(Cube.getText());
- }
- else if (e.getSource() == test)
- {
- try {
- sendInt(100);
- receInt();
- } catch (IOException e1) {
- Console.setText("Error while Sending/Receiving Int");
- System.err.println("Problems occured while testing connection. Please try again.");
- System.exit(0);
- }
- }
- }
-
- private JPanel getJPanel1() {
- if(jPanel1 == null) {
- jPanel1 = new JPanel();
- GridLayout jPanel1Layout = new GridLayout(1, 1);
- jPanel1Layout.setHgap(5);
- jPanel1Layout.setVgap(5);
- jPanel1Layout.setColumns(1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1.add(getCubePanel());
- }
- return jPanel1;
- }
- private JButton getSolve() {
- if(solve == null) {
- solve = new JButton();
- solve.setText("Solve that Cube!");
- solve.setPreferredSize(new java.awt.Dimension(183, 56));
- solve.addActionListener(this);
- }
- return solve;
-
- }
-
- private JPanel getCubePanel() {
- if(CubePanel == null) {
- CubePanel = new JPanel();
- CubePanel.add(getCube());
- CubePanel.add(getNote());
- }
- return CubePanel;
- }
-
- private JTextArea getCube() {
- if(Cube == null) {
- Cube = new JTextArea();
- Cube.setPreferredSize(new java.awt.Dimension(305, 180));
- }
- return Cube;
- }
-
- private JLabel getNote() {
- if(Note == null) {
- Note = new JLabel();
- Note.setText("Remember, the cube must be white face and blue up!");
- }
- return Note;
- }
-
- private JSeparator getJSeparator1() {
- if(jSeparator1 == null) {
- jSeparator1 = new JSeparator();
- jSeparator1.setPreferredSize(new java.awt.Dimension(212, 2));
- }
- return jSeparator1;
- }
-
- // I/O Methods
-
- private void sendInt(int i) throws IOException
- {
- dos.writeInt(i);
- dos.flush();
- Console.setText("Send:" + i);
- //Optional
- System.out.println("Send:"+i);
- }
-
- private void receInt() throws IOException
- {
- int i = dis.readInt();
- Console.setText("Receive:"+i);
- //Optional
- System.out.println("Receive:"+i);
- }
- //Cube Type Converter
-
- private void CubeStr(String input)
- {
- String[] cc = new String[54];
- String[] cd = new String[54];
- String Result = new String();
- for(int i=0;i<54;i++)
- {
- cc[i] = input.substring(i,i+1);
- System.out.print(cc[i] + ";");
- }
- /*
- * W F
- * Y B
- * G D
- * R R
- * O L
- * B U
- */
- for(int i=0;i<54;i++)
- {
- if (cc[i].equals("W"))
- {
- cd[i] = "F";
- }
- else if (cc[i].equals("Y"))
- {
- cd[i] = "B";
- }
- else if (cc[i].equals("G"))
- {
- cd[i] = "D";
- }
- else if (cc[i].equals("R"))
- {
- cd[i] = "R";
- }
- else if (cc[i].equals("O"))
- {
- cd[i] = "L";
- }
- else if (cc[i].equals("B"))
- {
- cd[i] = "U";
- }
- else
- {
- System.out.println("Unknown character(s) found:"+cc[i]);
- }
- }
- for(int i=0;i<54;i++)
- {
- Result += cd[i];
- }
- String sol = Search.solution(Result,20,20,false);
- System.out.println(Result);
- System.out.println(sol);
- }
- // Cube Actions
-
- private JPanel getActions() {
- if(Actions == null) {
- Actions = new JPanel();
- Actions.add(getActionspane());
- }
- return Actions;
- }
-
- private JTabbedPane getActionspane() {
- if(actionspane == null) {
- actionspane = new JTabbedPane();
- actionspane.setPreferredSize(new java.awt.Dimension(239, 206));
- actionspane.addTab("B", null, getBmov(), null);
- actionspane.addTab("L", null, getLmov(), null);
- actionspane.addTab("F", null, getFmov(), null);
- actionspane.addTab("R", null, getRmov(), null);
- actionspane.addTab("U", null, getUmov(), null);
- actionspane.addTab("D", null, getDmov(), null);
- }
- return actionspane;
- }
-
- private JPanel getBmov() {
- if(bmov == null) {
- bmov = new JPanel();
- bmov.setPreferredSize(new java.awt.Dimension(239, 110));
- bmov.add(getBD());
- bmov.add(getBU());
- bmov.add(getBT());
- }
- return bmov;
- }
-
- private JPanel getLmov() {
- if(lmov == null) {
- lmov = new JPanel();
- lmov.add(getLD());
- lmov.add(getLU());
- lmov.add(getLT());
- }
- return lmov;
- }
-
- private JPanel getFmov() {
- if(fmov == null) {
- fmov = new JPanel();
- fmov.add(getFD());
- fmov.add(getFU());
- fmov.add(getFT());
- }
- return fmov;
- }
-
- private JPanel getRmov() {
- if(rmov == null) {
- rmov = new JPanel();
- rmov.add(getRD());
- rmov.add(getRU());
- rmov.add(getRT());
- }
- return rmov;
- }
-
- private JPanel getUmov() {
- if(umov == null) {
- umov = new JPanel();
- umov.add(getUD());
- umov.add(getUU());
- umov.add(getUT());
- }
- return umov;
- }
-
- private JPanel getDmov() {
- if(dmov == null) {
- dmov = new JPanel();
- dmov.add(getDD());
- dmov.add(getDU());
- dmov.add(getDT());
- }
- return dmov;
- }
-
- private JButton getBD() {
- if(bD == null) {
- bD = new JButton();
- bD.setText("B");
- bD.setPreferredSize(new java.awt.Dimension(40, 30));
- }
- bD.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(1);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return bD;
- }
-
- private JButton getBU() {
- if(bU == null) {
- bU = new JButton();
- bU.setText("B'");
- }
- bU.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(2);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return bU;
- }
-
- private JButton getBT() {
- if(bT == null) {
- bT = new JButton();
- bT.setText("B2");
- }
- bT.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(1);
- receInt();
- sendInt(1);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return bT;
- }
-
- private JButton getLD() {
- if(lD == null) {
- lD = new JButton();
- lD.setText("L");
- }
- lD.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {// Change THIS!!
- try {
- sendInt(1);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return lD;
- }
-
- private JButton getLU() {
- if(lU == null) {
- lU = new JButton();
- lU.setText("L'");
- }
- lU.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {// Change THIS!!!
- try {
- sendInt(1);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return lU;
- }
-
- private JButton getLT() {
- if(lT == null) {
- lT = new JButton();
- lT.setText("L2");
- }
- lT.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- { //Change tHIS!!!
- try {
- sendInt(1);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return lT;
- }
-
- private JButton getFD() {
- if(fD == null) {
- fD = new JButton();
- fD.setText("F");
- }
- fD.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(3);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return fD;
- }
-
- private JButton getFU() {
- if(fU == null) {
- fU = new JButton();
- fU.setText("F'");
- }
- fU.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(4);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return fU;
- }
-
- private JButton getFT() {
- if(fT == null) {
- fT = new JButton();
- fT.setText("F2");
- }
- fT.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(3);
- receInt();
- sendInt(3);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return fT;
- }
-
- private JButton getRD() {
- if(rD == null) {
- rD = new JButton();
- rD.setText("R");
- }
- rD.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- { //Change THIS!!
- try {
- sendInt(1);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return rD;
- }
-
- private JButton getRU() {
- if(rU == null) {
- rU = new JButton();
- rU.setText("R'");
- }
- rU.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- { //Change THIS!!
- try {
- sendInt(1);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return rU;
- }
-
- private JButton getRT() {
- if(rT == null) {
- rT = new JButton();
- rT.setText("R2");
- }
- rT.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- { //Change THIS!!
- try {
- sendInt(1);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return rT;
- }
-
- private JButton getUD() {
- if(uD == null) {
- uD = new JButton();
- uD.setText("U");
- }
- uD.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(5);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return uD;
- }
-
- private JButton getUU() {
- if(uU == null) {
- uU = new JButton();
- uU.setText("U'");
- }
- uU.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(6);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return uU;
- }
-
- private JButton getUT() {
- if(uT == null) {
- uT = new JButton();
- uT.setText("U2");
- }
- uT.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(5);
- receInt();
- sendInt(5);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return uT;
- }
-
- private JButton getDD() {
- if(dD == null) {
- dD = new JButton();
- dD.setText("D");
- }
- dD.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(7);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return dD;
- }
-
- private JButton getDU() {
- if(dU == null) {
- dU = new JButton();
- dU.setText("D'");
- }
- dU.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(8);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return dU;
- }
-
- private JButton getDT() {
- if(dT == null) {
- dT = new JButton();
- dT.setText("D2");
- }
- dT.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try {
- sendInt(7);
- receInt();
- sendInt(7);
- receInt();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- return dT;
- }
-
- private JLabel getCopyright() {
- if(copyright == null) {
- copyright = new JLabel();
- copyright.setText("Powered by Michael Peng");
- }
- return copyright;
- }
- }
复制代码
软件区用VB做的供解魔方机器人使用的程序更新至leJOS!
|
|