@Override public void paintComponent(Graphics g) { super.paintComponent(g); // paint background Graphics2D g2d = (Graphics2D) g; MachineConfiguration mc = MachineConfiguration.getSingleton(); DrawingTool tool = mc.GetTool(0); paintBackground(); paintCamera(g2d); paintLimits(g2d, mc); paintCenter(g2d); // TODO draw left motor // TODO draw right motor // TODO draw control box final int look_ahead = 500; g2d.setColor(Color.BLACK); // draw image if (fast_nodes.size() > 0) { // draw the nodes for (int i = 0; i < fast_nodes.size(); ++i) { DrawPanelNode n = fast_nodes.get(i); if (running) { if (n.line_number <= linesProcessed) { g2d.setColor(Color.RED); } else if (n.line_number <= linesProcessed + look_ahead) { g2d.setColor(Color.GREEN); } else if (prefs.getBoolean("Draw all while running", true) == false) { break; } } switch (n.type) { case TOOL: tool = MachineConfiguration.getSingleton().GetTool(n.tool_id); g2d.setStroke(tool.getStroke()); break; case COLOR: if (!running || n.line_number > linesProcessed + look_ahead) { g2d.setColor(n.c); } break; default: tool.DrawLine(g2d, n.x1, n.y1, n.x2, n.y2); break; } } } g2d.dispose(); }
public void ZoomToFitPaper() { MachineConfiguration mc = MachineConfiguration.getSingleton(); float w = (float) this.getWidth(); float h = (float) this.getHeight(); // which one do we have to zoom more to fit the picture in the component? float wzoom = w / (float) (mc.paper_right - mc.paper_left); float hzoom = h / (float) (mc.paper_top - mc.paper_bottom); cameraZoom = (wzoom < hzoom ? wzoom : hzoom) / extraScale; cameraOffsetX = 0; cameraOffsetY = 0; repaint(); }
private void OptimizeNodes() { if (instructions == null) return; MachineConfiguration mc = MachineConfiguration.getSingleton(); DrawingTool tool = mc.GetTool(0); drawScale = 0.1f; float px = 0, py = 0, pz = 90; float x, y, z, ai, aj; int i, j; boolean absMode = true; String tool_change = "M06 T"; pz = 0.5f; for (i = 0; i < instructions.size(); ++i) { String line = instructions.get(i); String[] pieces = line.split(";"); if (pieces.length == 0) continue; if (line.startsWith(tool_change)) { String numberOnly = line.substring(tool_change.length()).replaceAll("[^0-9]", ""); addNodeTool(i, (int) Integer.valueOf(numberOnly, 10)); continue; } String[] tokens = pieces[0].split("\\s"); if (tokens.length == 0) continue; // have we changed scale? // what are our coordinates? x = px; y = py; z = pz; ai = px; aj = py; for (j = 0; j < tokens.length; ++j) { if (tokens[j].equals("G20")) drawScale = 2.54f; // in->cm else if (tokens[j].equals("G21")) drawScale = 0.10f; // mm->cm else if (tokens[j].equals("G90")) { absMode = true; break; } else if (tokens[j].equals("G91")) { absMode = false; break; } else if (tokens[j].equals("G54")) break; else if (tokens[j].startsWith("X")) { float tx = Float.valueOf(tokens[j].substring(1)) * drawScale; x = absMode ? tx : x + tx; } else if (tokens[j].startsWith("Y")) { float ty = Float.valueOf(tokens[j].substring(1)) * drawScale; y = absMode ? ty : y + ty; } else if (tokens[j].startsWith("Z")) { float tz = z = Float.valueOf(tokens[j].substring(1)); // * drawScale; z = absMode ? tz : z + tz; } if (tokens[j].startsWith("I")) ai = Float.valueOf(tokens[j].substring(1)) * drawScale; if (tokens[j].startsWith("J")) aj = Float.valueOf(tokens[j].substring(1)) * drawScale; } if (j < tokens.length) continue; // * // is pen up or down? tool.DrawZ(z); if (tool.DrawIsOff()) { if (show_pen_up == false) { px = x; py = y; pz = z; continue; } addNodeColor(i, Color.BLUE); } else if (tool.DrawIsOn()) { addNodeColor(i, Color.BLACK); // TODO use actual pen color } else { addNodeColor(i, Color.ORANGE); } // what kind of motion are we going to make? if (tokens[0].equals("G00") || tokens[0].equals("G0") || tokens[0].equals("G01") || tokens[0].equals("G1")) { // if(z==pz) { addNodePos(i, ITX(px), ITY(py), ITX(x), ITY(y)); } } else if (tokens[0].equals("G02") || tokens[0].equals("G2") || tokens[0].equals("G03") || tokens[0].equals("G3")) { // draw an arc // clockwise or counter-clockwise? 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 += Math.PI * 2.0; else if (dir < 0 && theta > 0) angle1 += Math.PI * 2.0; theta = Math.abs(angle2 - angle1) * RAD2DEG * STEPS_PER_DEGREE; // Draw the arc from a lot of little line segments. for (int k = 0; k <= theta; ++k) { double angle3 = (angle2 - angle1) * ((double) k / theta) + angle1; float nx = (float) (ai + Math.cos(angle3) * radius); float ny = (float) (aj + Math.sin(angle3) * radius); addNodePos(i, ITX(px), ITY(py), ITX(nx), ITY(ny)); px = nx; py = ny; } addNodePos(i, ITX(px), ITY(py), ITX(x), ITY(y)); } px = x; py = y; pz = z; } // for ( each instruction ) }