public Container CreateContentPane() { // Create the content-pane-to-be. JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setOpaque(true); // the log panel log = new JTextPane(); log.setEditable(false); log.setBackground(Color.BLACK); logPane = new JScrollPane(log); kit = new HTMLEditorKit(); doc = new HTMLDocument(); log.setEditorKit(kit); log.setDocument(doc); DefaultCaret c = (DefaultCaret) log.getCaret(); c.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); ClearLog(); // the preview panel previewPane = new DrawPanel(); previewPane.setPaperSize(paper_top, paper_bottom, paper_left, paper_right); // status bar statusBar = new StatusBar(); Font f = statusBar.getFont(); statusBar.setFont(f.deriveFont(Font.BOLD, 15)); Dimension d = statusBar.getMinimumSize(); d.setSize(d.getWidth(), d.getHeight() + 30); statusBar.setMinimumSize(d); // layout Splitter split = new Splitter(JSplitPane.VERTICAL_SPLIT); split.add(previewPane); split.add(logPane); split.setDividerSize(8); contentPane.add(statusBar, BorderLayout.SOUTH); contentPane.add(split, BorderLayout.CENTER); // open the file if (recentFiles[0].length() > 0) { OpenFileOnDemand(recentFiles[0]); } // connect to the last port ListSerialPorts(); if (Arrays.asList(portsDetected).contains(recentPort)) { OpenPort(recentPort); } return contentPane; }
// User has asked that a file be opened. public void OpenFileOnDemand(String filename) { Log("<font color='green'>Opening file " + recentFiles[0] + "...</font>\n"); String ext = filename.substring(filename.lastIndexOf('.')); if (!ext.equalsIgnoreCase(".ngc")) { LoadImage(filename); } else { LoadGCode(filename); } statusBar.Clear(); }
// Take the next line from the file and send it to the robot, if permitted. public void SendFileCommand() { if (running == false || paused == true || fileOpened == false || portConfirmed == false || linesProcessed >= linesTotal) return; String line; do { // are there any more commands? line = gcode.get((int) linesProcessed++).trim(); previewPane.setLinesProcessed(linesProcessed); statusBar.SetProgress(linesProcessed, linesTotal); // loop until we find a line that gets sent to the robot, at which point we'll // pause for the robot to respond. Also stop at end of file. } while (ProcessLine(line) && linesProcessed < linesTotal); if (linesProcessed == linesTotal) { // end of file Halt(); } }
public static Color getColor(String name, Color def) { if (name == null) { // System.out.println("illegal color: " + name); // Thread.dumpStack(); return (def); } name = name.toLowerCase(); Color nc = (Color) colors.get(name); if (nc == null) { StringTokenizer st = new StringTokenizer(name, ", "); // System.out.println(st.countTokens(); if ((st.countTokens() < 3) || (st.countTokens() > 4)) { // System.out.println("illegal color"); nc = def; } else { try { int r = Integer.parseInt(st.nextToken()); int g = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); if (st.countTokens() == 0) { nc = new GuessColor(r, g, b); } else { int a = Integer.parseInt(st.nextToken()); nc = new GuessColor(r, g, b, a); } colors.put(name, nc); } catch (Exception e) { ExceptionWindow.getExceptionWindow(e); StatusBar.setErrorStatus("Invalid color, using default"); nc = def; } } } return (nc); }
// The user has done something. respond to it. public void actionPerformed(ActionEvent e) { Object subject = e.getSource(); if (subject == buttonZoomIn) { previewPane.ZoomIn(); return; } if (subject == buttonZoomOut) { previewPane.ZoomOut(); return; } if (subject == buttonOpenFile) { OpenFileDialog(); return; } if (subject == buttonStart) { if (fileOpened) { paused = false; running = true; UpdateMenuBar(); linesProcessed = 0; previewPane.setRunning(running); previewPane.setLinesProcessed(linesProcessed); statusBar.Start(); SendFileCommand(); } return; } if (subject == buttonPause) { if (running) { if (paused == true) { buttonPause.setText("Pause"); paused = false; // @TODO: if the robot is not ready to unpause, this might fail and the program would // appear to hang. SendFileCommand(); } else { buttonPause.setText("Unpause"); paused = true; } } return; } if (subject == buttonDrive) { Drive(); return; } if (subject == buttonHalt) { Halt(); return; } if (subject == buttonRescan) { ListSerialPorts(); UpdateMenuBar(); return; } if (subject == buttonConfig) { UpdateConfig(); return; } if (subject == buttonJogMotors) { JogMotors(); return; } if (subject == buttonAbout) { JOptionPane.showMessageDialog( null, "Created by Dan Royer ([email protected]).\n\n" + "Find out more at http://www.marginallyclever.com/\n" + "Get the latest version and read the documentation online @ http://github.com/i-make-robots/DrawBot/"); return; } if (subject == buttonCheckForUpdate) { CheckForUpdate(); return; } if (subject == buttonExit) { System.exit(0); // @TODO: be more graceful? return; } int i; for (i = 0; i < 10; ++i) { if (subject == buttonRecent[i]) { OpenFileOnDemand(recentFiles[i]); return; } } for (i = 0; i < portsDetected.length; ++i) { if (subject == buttonPorts[i]) { OpenPort(portsDetected[i]); return; } } }
void EstimateDrawTime() { int i, j; float drawScale = 1.0f; double px = 0, py = 0, pz = 0; float feed_rate = 1.0f; estimated_time = 0; float estimated_length = 0; int estimate_count = 0; for (i = 0; i < gcode.size(); ++i) { String line = gcode.get(i); String[] pieces = line.split(";"); if (pieces.length == 0) continue; String[] tokens = pieces[0].split("\\s"); if (tokens.length == 0) continue; for (j = 0; j < tokens.length; ++j) { if (tokens[j].equals("G20")) drawScale = 0.393700787f; if (tokens[j].equals("G21")) drawScale = 0.1f; if (tokens[j].startsWith("F")) { feed_rate = Float.valueOf(tokens[j].substring(1)) * drawScale; Log("<span style='color:green'>feed rate=" + feed_rate + "</span>\n"); feed_rate *= 1; } } double x = px; double y = py; double z = pz; double ai = px; double aj = py; for (j = 1; j < tokens.length; ++j) { if (tokens[j].startsWith("X")) x = Float.valueOf(tokens[j].substring(1)) * drawScale; if (tokens[j].startsWith("Y")) y = Float.valueOf(tokens[j].substring(1)) * drawScale; if (tokens[j].startsWith("Z")) z = Float.valueOf(tokens[j].substring(1)) * drawScale; if (tokens[j].startsWith("I")) ai = px + Float.valueOf(tokens[j].substring(1)) * drawScale; if (tokens[j].startsWith("J")) aj = py + Float.valueOf(tokens[j].substring(1)) * drawScale; } if (tokens[0].equals("G00") || tokens[0].equals("G0") || tokens[0].equals("G01") || tokens[0].equals("G1")) { // draw a line double ddx = x - px; double ddy = y - py; double dd = Math.sqrt(ddx * ddx + ddy * ddy); estimated_time += dd / feed_rate; estimated_length += dd; ++estimate_count; px = x; py = y; pz = z; } else if (tokens[0].equals("G02") || tokens[0].equals("G2") || tokens[0].equals("G03") || tokens[0].equals("G3")) { // draw an arc int dir = (tokens[0].equals("G02") || tokens[0].equals("G2")) ? -1 : 1; double dx = px - ai; double dy = py - aj; double radius = Math.sqrt(dx * dx + dy * dy); // find angle of arc (sweep) double angle1 = atan3(dy, dx); double angle2 = atan3(y - aj, x - ai); double theta = angle2 - angle1; if (dir > 0 && theta < 0) angle2 += 2.0 * Math.PI; else if (dir < 0 && theta > 0) angle1 += 2.0 * Math.PI; theta = Math.abs(angle2 - angle1); // Draw the arc from a lot of little line segments. for (int k = 0; k <= theta * DrawPanel.STEPS_PER_DEGREE; ++k) { double angle3 = (angle2 - angle1) * ((double) k / (theta * DrawPanel.STEPS_PER_DEGREE)) + angle1; float nx = (float) (ai + Math.cos(angle3) * radius); float ny = (float) (aj + Math.sin(angle3) * radius); double ddx = nx - px; double ddy = ny - py; double dd = Math.sqrt(ddx * ddx + ddy * ddy); estimated_time += dd / feed_rate; estimated_length += dd; ++estimate_count; px = nx; py = ny; } double ddx = x - px; double ddy = y - py; double dd = Math.sqrt(ddx * ddx + ddy * ddy); estimated_time += dd / feed_rate; estimated_length += dd; ++estimate_count; px = x; py = y; pz = z; } } // for ( each instruction ) estimated_time += estimate_count * 0.007617845117845f; Log( "<font color='green'>" + estimate_count + " line segments.\n" + estimated_length + "cm of line.\n" + "Estimated " + statusBar.formatTime((long) (estimated_time * 10000)) + "s to draw.</font>\n"); }