Java Code Examples for java.awt.Graphics2D.drawPolygon()

The following are Jave code examples for showing how to use drawPolygon() of the java.awt.Graphics2D class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jmt   File: Convex2DGraph.java   View Source Code Vote up 6 votes
/**
 * This function draw the area among the axis and the point of the convex
 * hull
 * 
 * @param g
 *            The graphic object
 * @param allConvex
 *            The vector with the points of the convex hull
 * @param allDominants
 *            The vector with the dominant points
 * @param area
 *            The filter area
 * @param maskedoff
 *            The vector with the masked-off points
 */
public void drawArea(Graphics2D g, Vector<Point2D> allConvex, Color color) {
	Polygon poly = new Polygon();
	DPoint p;

	poly.addPoint(plane.getTrueX(0), plane.getTrueY(0));
	poly.addPoint(plane.getTrueX(xMaxValue), plane.getTrueY(0));
	// Add the point a the polygon for paint the convex area
	for (int i = 0; i < allConvex.size(); i++) {
		p = (DPoint) allConvex.get(i);
		poly.addPoint(plane.getTrueX(p.getX()), plane.getTrueY(p.getY()));
	}
	poly.addPoint(plane.getTrueX(0), plane.getTrueY(yMaxValue));

	g.setStroke(LINES);
	g.setColor(color);
	g.drawPolygon(poly);
}
 
Example 2
Project: jmt   File: PainterConvex2D.java   View Source Code Vote up 6 votes
/**
 * Draw a semi-trasparet area
 * @param g The graphic object
 * @param dragPoint The first point
 * @param beginPoint The second point
 * @param c The color of the area
 */
public void drawDragArea(Graphics2D g, Point dragPoint, Point beginPoint, Color c) {
	g.setColor(c);

	Polygon poly = new Polygon();

	poly.addPoint((int) beginPoint.getX(), (int) beginPoint.getY());
	poly.addPoint((int) beginPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) beginPoint.getY());

	//Set the widths of the shape's outline
	Stroke oldStro = g.getStroke();
	Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	g.setStroke(stroke);
	g.drawPolygon(poly);
	g.setStroke(oldStro);

	//Set the trasparency of the iside of the rectangle
	Composite oldComp = g.getComposite();
	Composite alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
	g.setComposite(alphaComp);
	g.fillPolygon(poly);
	g.setComposite(oldComp);

}
 
Example 3
Project: VASSAL-src   File: PolygonEditor.java   View Source Code Vote up 6 votes
public void paint(Graphics g) {
  paintBackground(g);
  if (polygon != null && polygon.npoints > 0) {
    final Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.white);
    g2d.setComposite(
      AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
    g2d.fill(polygon);

    if (selected >= 0 && selected < polygon.xpoints.length) {
      g2d.setColor(Color.red);
      int x = polygon.xpoints[selected];
      int y = polygon.ypoints[selected];
      g2d.fillOval(x - 10, y - 10, 20, 20);
    }

    g2d.setComposite(AlphaComposite.SrcAtop);
    g2d.setColor(Color.black);
    g2d.setStroke(new BasicStroke(2.0F));
    g2d.drawPolygon(polygon);
  }
}
 
Example 4
Project: QN-ACTR-Release   File: PainterConvex2D.java   View Source Code Vote up 6 votes
/**
 * Draw a semi-trasparet area
 * @param g The graphic object
 * @param dragPoint The first point
 * @param beginPoint The second point
 * @param c The color of the area
 */
public void drawDragArea(Graphics2D g, Point dragPoint, Point beginPoint, Color c) {
	g.setColor(c);

	Polygon poly = new Polygon();

	poly.addPoint((int) beginPoint.getX(), (int) beginPoint.getY());
	poly.addPoint((int) beginPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) beginPoint.getY());

	//Set the widths of the shape's outline
	Stroke oldStro = g.getStroke();
	Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	g.setStroke(stroke);
	g.drawPolygon(poly);
	g.setStroke(oldStro);

	//Set the trasparency of the iside of the rectangle
	Composite oldComp = g.getComposite();
	Composite alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
	g.setComposite(alphaComp);
	g.fillPolygon(poly);
	g.setComposite(oldComp);

}
 
Example 5
Project: Pixie   File: DrawOptions.java   View Source Code Vote up 6 votes
/**
 * Draw a polygon which passes through all the points drawn by the user.
 *
 * @param g2d        the graphics object
 * @param polygon    the polygon to be drawn on the interface
 * @param color      the color of the polygon
 * @param dashedLine true = the line used for drawing the polygon will be dashed; false = the line will be solid/plain
 */
public static void drawPolygon(Graphics2D g2d, Polygon polygon, Color color, boolean dashedLine) {
    g2d.setStroke(dashedLine ? getDashedStroke() : new BasicStroke(0));

    if (polygon.npoints > 0) {
        g2d.setColor(color);
        g2d.drawPolygon(polygon);

        // make the line thicker
        g2d.setStroke(new BasicStroke(2));

        for (int index = 0; index < polygon.npoints; index++) {
            g2d.drawLine(polygon.xpoints[index], polygon.ypoints[index],
                    polygon.xpoints[index], polygon.ypoints[index]);
        }
    }

    g2d.setStroke(new BasicStroke(0));
}
 
Example 6
Project: Logisim   File: Power.java   View Source Code Vote up 6 votes
private void drawInstance(InstancePainter painter, boolean isGhost) {
	Graphics2D g = (Graphics2D) painter.getGraphics().create();
	Location loc = painter.getLocation();
	g.translate(loc.getX(), loc.getY());

	Direction from = painter.getAttributeValue(StdAttr.FACING);
	int degrees = Direction.EAST.toDegrees() - from.toDegrees();
	double radians = Math.toRadians((degrees + 360) % 360);
	g.rotate(radians);

	GraphicsUtil.switchToWidth(g, Wire.WIDTH);
	if (!isGhost && painter.getShowState()) {
		g.setColor(painter.getPort(0).getColor());
	}
	g.drawLine(0, 0, 5, 0);

	GraphicsUtil.switchToWidth(g, 1);
	if (!isGhost && painter.shouldDrawColor()) {
		BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
		g.setColor(Value.repeat(Value.TRUE, width.getWidth()).getColor());
	}
	g.drawPolygon(new int[] { 6, 14, 6 }, new int[] { -8, 0, 8 }, 3);

	g.dispose();
}
 
Example 7
Project: jdk8u-jdk   File: Test8004821.java   View Source Code Vote up 5 votes
private static void test(final Graphics2D g, final int[] arr) {
    g.drawPolygon(arr, arr, arr.length);
    g.drawPolygon(new Polygon(arr, arr, arr.length));
    g.fillPolygon(arr, arr, arr.length);
    g.fillPolygon(new Polygon(arr, arr, arr.length));
    g.drawPolyline(arr, arr, arr.length);
}
 
Example 8
Project: openjdk-jdk10   File: Test8004821.java   View Source Code Vote up 5 votes
private static void test(final Graphics2D g, final int[] arr) {
    g.drawPolygon(arr, arr, arr.length);
    g.drawPolygon(new Polygon(arr, arr, arr.length));
    g.fillPolygon(arr, arr, arr.length);
    g.fillPolygon(new Polygon(arr, arr, arr.length));
    g.drawPolyline(arr, arr, arr.length);
}
 
Example 9
Project: Pixie   File: BoundingBoxWindow.java   View Source Code Vote up 5 votes
/**
 * Draw the outer box of the object; a rectangle which is resized to the
 * proper image size.
 *
 * @param g2d the graphics object
 * @return the rectangle drawn on the image
 */
private void drawPolygon(Graphics2D g2d) {

    java.awt.Polygon polygon = new Polygon(((ObjectPolygon) currentObject).getPolygon().xpoints,
            ((ObjectPolygon) currentObject).getPolygon().ypoints,
            ((ObjectPolygon) currentObject).getPolygon().npoints);

    // shift the polygon with the bordered added
    for (int index = 0; index < polygon.npoints; index++) {
        polygon.xpoints[index] -= displayBox.x;
        polygon.ypoints[index] -= displayBox.y;
    }

    // compute the position of the object box, to be displayed
    polygon = resize.originalToResized(polygon);

    g2d.setColor(objectColor);

    g2d.drawPolygon(polygon);

    // make the line thicker
    g2d.setStroke(new BasicStroke(3));

    // draw the points of the polygon
    for (int index = 0; index < polygon.npoints; index++) {
        g2d.drawLine(polygon.xpoints[index], polygon.ypoints[index],
                polygon.xpoints[index], polygon.ypoints[index]);
    }

    // reset the thicknes of the line
    g2d.setStroke(new BasicStroke(0));
}
 
Example 10
Project: brModelo   File: baseDrawerItem.java   View Source Code Vote up 5 votes
private boolean DrawComplexPath(Graphics2D g, int l, int t) {
        String[] dist = getPath().split(",");
        int dl = dist.length;
        if (dl < 3) {
            g.drawString("?", l, t);
            return false;
        }
        if (dl % 2 > 0) {
            String[] tmp = new String[dl + 1];
            tmp[dl] = "0";
            dist = tmp;
            dl = dist.length;
        }
        int tam = dl / 2;
        int xPoints[] = new int[tam];
        int yPoints[] = new int[tam];
        try {
            int y = 0;
            for (int i = 0; i < tam; i++) {
                xPoints[i] = Expr(dist[y++].trim());
                yPoints[i] = Expr(dist[y++].trim());
//                xPoints[i] = l + Integer.valueOf(dist[y++].trim());
//                yPoints[i] = t + Integer.valueOf(dist[y++].trim());
            }
        } catch (Exception x) {
            g.drawString("?", l, t);
            return false;
        }
        if (isFill()) {
            g.fillPolygon(xPoints, yPoints, tam);
        } else {
            g.drawPolygon(xPoints, yPoints, tam);
        }
        return true;
    }
 
Example 11
Project: pdi   File: Quadrado.java   View Source Code Vote up 5 votes
public BufferedImage criaQuadrado(BufferedImage img, PosicoesDTO posicoes) {
	Graphics graphics = img.getGraphics();
	Graphics2D graphics2d = (Graphics2D) graphics.create();
	graphics2d.setStroke(new BasicStroke(4));
	graphics2d.setColor(Color.BLUE);

	int x1 = posicoes.getX1();
	int x2 = posicoes.getX2();
	int y1 = posicoes.getY1();
	int y2 = posicoes.getY2();

	graphics2d.drawPolygon(new int[] { x1, x1, x2, x2 }, new int[] { y1, y2, y2, y1 }, 4);
	graphics2d.dispose();
	return img;
}
 
Example 12
Project: KeysPerSecond   File: GraphPanel.java   View Source Code Vote up 4 votes
@Override
public void paintComponent(Graphics g1){
	if(Main.config.showGraph){
		try{
			Graphics2D g = (Graphics2D)g1;
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
			if(ColorManager.transparency){
				g.setColor(ColorManager.transparent);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Main.config.getBackgroundOpacity()));
				g.setColor(Main.config.getBackgroundColor());
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Main.config.getBackgroundOpacity()));
			}else{
				g.setColor(Main.config.getBackgroundColor());
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0F));
			}
			Polygon poly = new Polygon();
			poly.addPoint(this.getWidth() - SizeManager.graphOffset - 2, this.getHeight() - SizeManager.graphOffset);
			for(int i = 1; i <= values.size(); i++){
				int px = (int) (SizeManager.graphOffset + 2 + ((double)(this.getWidth() - SizeManager.graphOffset * 2 - 4) / (double)(Main.config.backlog - 1)) * (Main.config.backlog - i));
				int py = (int) (this.getHeight() - SizeManager.graphOffset - ((float)(this.getHeight() - SizeManager.graphOffset * 2) * ((float)values.get(i - 1) / (float)maxval)));
				poly.addPoint(px, py);
				if(i == values.size()){
					poly.addPoint(px, this.getHeight() - SizeManager.graphOffset);
				}
			}
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Main.config.getForegroundOpacity()));
			if(Main.config.graphAvg){
				int y = (int) (this.getHeight() - SizeManager.graphOffset - ((float)(this.getHeight() - SizeManager.graphOffset * 2) * (Main.avg / (float)maxval)));
				g.setColor(Main.config.getForegroundColor().darker());
				g.setStroke(avgstroke);
				g.drawLine(SizeManager.graphOffset + 2, y, this.getWidth() - SizeManager.graphOffset - 2, y);
			}
			g.setStroke(line);
			g.setColor(ColorManager.alphaAqua);
			g.fillPolygon(poly);
			g.setColor(Main.config.getForegroundColor());
			g.drawPolygon(poly);
			g.drawImage(ColorManager.graph_upper_left,   2, 2, 2 + SizeManager.graphImageSize, 2 + SizeManager.graphImageSize, 0, 0, 4, 4, this);
			g.drawImage(ColorManager.graph_lower_left,   2, this.getHeight() - 3 - SizeManager.graphImageSize, 2 + SizeManager.graphImageSize, this.getHeight() - 3, 0, 0, 4, 4, this);
			g.drawImage(ColorManager.graph_upper_right,  this.getWidth() - 3 - SizeManager.graphImageSize, 2, this.getWidth() - 3, 2 + SizeManager.graphImageSize, 0, 0, 4, 4, this);
			g.drawImage(ColorManager.graph_lower_right,  this.getWidth() - 3 - SizeManager.graphImageSize, this.getHeight() - 3 - SizeManager.graphImageSize, this.getWidth() - 3, this.getHeight() - 3, 0, 0, 4, 4, this);
			g.drawImage(ColorManager.graph_side_left,    2, 2 + SizeManager.graphImageSize, 2 + SizeManager.graphImageSize, this.getHeight() - 3 - SizeManager.graphImageSize, 0, 0, 4, 56, this);
			g.drawImage(ColorManager.graph_upper_middle, 2 + SizeManager.graphImageSize, 2, this.getWidth() - 3 - SizeManager.graphImageSize, 2 + SizeManager.graphImageSize, 0, 0, 46, 4, this);
			g.drawImage(ColorManager.graph_lower_middle, 2 + SizeManager.graphImageSize, this.getHeight() - 3 - SizeManager.graphImageSize, this.getWidth() - 3 - SizeManager.graphImageSize, this.getHeight() - 3, 0, 0, 46, 4, this);
			g.drawImage(ColorManager.graph_side_right,   this.getWidth() - 3 - SizeManager.graphImageSize, 2 + SizeManager.graphImageSize, this.getWidth() - 3, this.getHeight() - 3 - SizeManager.graphImageSize, 0, 0, 4, 56, this);
		}catch(NullPointerException e){
			//catch but do not solve, this is caused by the race
			//condition. However adding synchronisation would impact
			//performance more then it is worth
		}
	}
}
 
Example 13
Project: JGraphix   File: JGraphix_Example_1.java   View Source Code Vote up 4 votes
public static void main(String[] args) {
	DrawFrame df = DrawFrame.create("MyDrawing", 200, 300);
	Graphics2D g = df.getGraphics2D();
	
	float linewidth = 5.0f;	// line width = 5 pixels
	g.setStroke(new BasicStroke(linewidth));
	
	g.setColor(Color.BLUE);
	g.drawLine(40, 10, 10, 40);
	g.draw(new Line2D.Double(70.2, 10.3, 100.4, 40.5));

	g.fillOval(10, 60, 30, 30);
	g.drawOval(60, 60, 30, 30);
	g.fillRoundRect(110, 60, 30, 30, 10, 10);
	g.drawRoundRect(160, 60, 30, 30, 10, 10);

	g.setColor(Color.GREEN.darker());
	g.fillArc(10, 110, 30, 30, 45, 240);
	g.fillArc(60, 110, 30, 30, 45 + 240, 360 - 240);
	g.fillArc(110, 110, 30, 30, 90, 270);
	g.fillArc(160, 110, 30, 30, 270, 270);

	g.setColor(Color.MAGENTA);
	g.drawArc(10, 160, 30, 30, 45, 240);
	g.drawArc(60, 160, 30, 30, 45 + 240, 360 - 240);
	g.drawArc(110, 160, 30, 30, 90, 270);
	g.drawArc(160, 160, 30, 30, 270, 270);

	g.setColor(Color.ORANGE);
	g.fillPolygon(new int[] { 10, 40, 10, 40 }, new int[] { 210, 210, 240, 240 }, 4);
	g.drawPolygon(new int[] { 60, 90, 60, 90 }, new int[] { 210, 210, 240, 240 }, 4);
	g.drawPolyline(new int[] { 110, 140, 110, 140 }, new int[] { 210, 210, 240, 240 }, 4);
	g.drawPolyline(new int[] { 160, 160, 190, 190 }, new int[] {240, 210, 240, 210 }, 4);

	// Printing texts
	g.setColor(Color.BLACK);
	g.setFont(new Font("Monospaced", Font.PLAIN, 14));
	g.drawString("Drawn with JGraphix", 10, 275);

	df.refresh();

	System.out.println("done.");
}