// From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135 void drawArrow( Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke, BasicStroke drawStroke) { double aDir = Math.atan2(xCenter - x, yCenter - y); // Line can be dashed. g2d.setStroke(drawStroke); g2d.drawLine(x, y, xCenter, yCenter); // make the arrow head solid even if dash pattern has been specified g2d.setStroke(lineStroke); Polygon tmpPoly = new Polygon(); int i1 = 12 + (int) (stroke * 2); // make the arrow head the same size regardless of the length length int i2 = 6 + (int) stroke; tmpPoly.addPoint(x, y); tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5)); tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir)); tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5)); tmpPoly.addPoint(x, y); // arrow tip g2d.drawPolygon(tmpPoly); // Remove this line to leave arrow head unpainted: g2d.fillPolygon(tmpPoly); }
private void draw_horizon(int rad, Point center, int[] angles) { // Draw an arc int arc_angle = ((angles[0] > angles[1]) ? (360 - angles[0]) + angles[1] : (angles[1] - angles[0])); Polygon remainder = new Polygon(); offgraphics_.setColor(GREEN); offgraphics_.fillArc(center.x - rad, center.y - rad, 2 * rad, 2 * rad, angles[0], arc_angle); if (pitch_ != 0) { if ((pitch_ > 0 && Math.abs(roll_) < 90) || (pitch_ < 0 && Math.abs(roll_) >= 90)) offgraphics_.setColor(BLUE); int cover_angle = (angles[0] + arc_angle / 2 + ((arc_angle < 180) ? 180 : 0)) % 360; // System.out.println (points[0] + " " + points[1]); // System.out.println (accepted_point); remainder.addPoint( center.x + polar_to_rect_x(rad, cover_angle), center.y - polar_to_rect_y(rad, cover_angle)); remainder.addPoint( center.x + polar_to_rect_x(rad, angles[0]), center.y - polar_to_rect_y(rad, angles[0])); remainder.addPoint( center.x + polar_to_rect_x(rad, angles[1]), center.y - polar_to_rect_y(rad, angles[1])); offgraphics_.fillPolygon(remainder); // offgraphics_.setColor (getBackground ()); // offgraphics_.drawPolygon (remainder); } }
private Polygon outline(int x, int y, double direction) { Polygon shape = new Polygon(); shape.addPoint(x, y); addPointRelative(shape, x, y, getOuterRadius(), direction - getAngle()); addPointRelative(shape, x, y, getInnerRadius(), direction); addPointRelative(shape, x, y, getOuterRadius(), direction + getAngle()); shape.addPoint(x, y); // Closing the polygon (TEG 97-04-23) return shape; }
/** * Draws a single arrow head * * @param aG the canvas to draw on; * @param aXpos the X position of the arrow head; * @param aYpos the (center) Y position of the arrow head; * @param aFactor +1 to have a left-facing arrow head, -1 to have a right-facing arrow head; * @param aArrowWidth the total width of the arrow head; * @param aArrowHeight the total height of the arrow head. */ public static final void drawArrowHead( final Graphics2D aG, final int aXpos, final int aYpos, final int aFactor, final int aArrowWidth, final int aArrowHeight) { final double halfHeight = aArrowHeight / 2.0; final int x1 = aXpos + (aFactor * aArrowWidth); final int y1 = (int) Math.ceil(aYpos - halfHeight); final int y2 = (int) Math.floor(aYpos + halfHeight); final Polygon arrowHead = new Polygon(); arrowHead.addPoint(aXpos, aYpos); arrowHead.addPoint(x1, y1); arrowHead.addPoint(x1, y2); aG.fill(arrowHead); }
/** * Adds a polygon. Note that the vertices of the polygon are passed using a float[][] and NOT a * float[]. Eg. * * <p>{{x0, y0, z0}, {x1, y1, z1}, ...} * * <p>We *flatten* the data once it is encapsulated inside this class; outside this class we want * clarity; inside this class we want speed! */ public int addPolygon(float[][] vs, Color c, boolean doubleSided) { Polygon polygon = new Polygon(vs.length, c, doubleSided); for (int i = 0; i < vs.length; i++) { polygon.addPoint(this.addPoint(vs[i][0], vs[i][1], vs[i][2])); } polygons[polynext] = polygon; // if this was the last polygon then we may now set the bounding box if (polynext == polygons.length - 1) { this.box.setBB(); } return polynext++; }
/** * The GraphicDrawer.paint() Function allows the user to draw pre-defined graphics into a given * Graphics Object. The user may give the rectangle properties which defines at best the desired * area to draw with a Int id which requests the desired graphic, whenever it's vectorial or * raster graphics * * @author LuisArturo */ public void paint(int id, Dimension r, Graphics g) { Graphics2D g2; switch (id) { case FILE: { g2 = (Graphics2D) g; try { FileInputStream img = new FileInputStream("archivo.png"); BufferedImage in = ImageIO.read(img); g2.drawImage( in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null); } catch (Exception e) { e.printStackTrace(); } } break; case REDO: { g2 = (Graphics2D) g; g2.setColor(color); g2.fillArc( r.width * 2 / 10, r.height * 2 / 10, r.width * 6 / 10, r.height * 6 / 10, 270, 225); Polygon p = new Polygon(); p.addPoint(r.width * 2 / 10, r.height * 2 / 10); p.addPoint(r.width * 2 / 10, r.height * 5 / 10); p.addPoint(r.width * 5 / 10, r.height * 5 / 10); g2.fillPolygon(p); g2.setColor(g2.getBackground()); g2.fillArc( r.width * 3 / 10, r.height * 3 / 10, r.width * 4 / 10, r.height * 4 / 10, 270, 225); } break; case TEXT: { g2 = (Graphics2D) g; g2.setColor(color); g2.setFont(new Font("Serif", Font.ITALIC | Font.BOLD, r.height * 6 / 10)); g2.drawChars("A".toCharArray(), 0, 1, r.width * 3 / 10, r.height * 7 / 10); } break; case SELECT: { g2 = (Graphics2D) g; try { FileInputStream img = new FileInputStream("mano.png"); BufferedImage in = ImageIO.read(img); g2.drawImage( in, r.width * 1 / 10, r.height * 1 / 10, r.width * 2 / 3, r.height * 2 / 3, null); } catch (Exception e) { e.printStackTrace(); } } break; case PENCIL: { g2 = (Graphics2D) g; try { FileInputStream img = new FileInputStream("lapiz.png"); BufferedImage in = ImageIO.read(img); g2.drawImage( in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null); } catch (Exception e) { e.printStackTrace(); } } break; case ARROW: { g2 = (Graphics2D) g; try { FileInputStream img = new FileInputStream("arrow.png"); BufferedImage in = ImageIO.read(img); g2.drawImage( in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null); } catch (Exception e) { e.printStackTrace(); } } break; case ZOOM: { g2 = (Graphics2D) g; try { FileInputStream img = new FileInputStream("zoom.png"); BufferedImage in = ImageIO.read(img); g2.drawImage( in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null); } catch (Exception e) { e.printStackTrace(); } } break; } }
private void addPointRelative(Polygon shape, int x, int y, double radius, double angle) { shape.addPoint(x + (int) (radius * Math.cos(angle)), y + (int) (radius * Math.sin(angle))); }