/** * Draws an open irregular polygon using between 3 and 12 sets of provided coordinates. <br> * Examples: <br> * Expo.drawPolygon(g,100,300,200,100,300,300); // for a triangle * Expo.drawPolygon(g,525,300,600,250,650,250,725,300,725,350,650,400); // for a hexagon */ public static void drawPolygon(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) { Polygon myPoly = new Polygon(); myPoly.addPoint(x1, y1); myPoly.addPoint(x2, y2); myPoly.addPoint(x3, y3); g.drawPolygon(myPoly); }
/** * Draws an open star with a specified number of points.<br> * The center of this star is specified by centerX,centerY and its size is specified by radius * <br> * (As in the radius of the circle the star would fit inside). <br> * Precondition: points >= 2 <br> * Example: <br> * Expo.drawStar(g,300,200,100,8); <br> * Draws an open star with 8 points and a radius of 100 pixels whose center is located at the * coordinate (300,200). */ public static void drawStar(Graphics g, int centerX, int centerY, int radius, int points) { int halfRadius = getHalfRadius(radius, points); int p = points; points *= 2; int xCoord[] = new int[points]; int yCoord[] = new int[points]; int currentRadius; for (int k = 0; k < points; k++) { if (k % 2 == 0) currentRadius = radius; else currentRadius = halfRadius; xCoord[k] = (int) Math.round(Math.cos(twoPI * k / points - halfPI) * currentRadius) + centerX; yCoord[k] = (int) Math.round(Math.sin(twoPI * k / points - halfPI) * currentRadius) + centerY; } int x = (p - 5) / 2 + 1; if (p >= 5 && p <= 51) switch (p % 4) { case 1: yCoord[x] = yCoord[x + 1] = yCoord[points - x - 1] = yCoord[points - x]; break; case 2: yCoord[x] = yCoord[x + 1] = yCoord[points - x - 1] = yCoord[points - x]; yCoord[x + 3] = yCoord[x + 4] = yCoord[points - x - 4] = yCoord[points - x - 3]; break; case 3: yCoord[x + 2] = yCoord[x + 3] = yCoord[points - x - 3] = yCoord[points - x - 2]; } g.drawPolygon(xCoord, yCoord, points); }
public static void drawPolygon( Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x5, int y5, int x6, int y6, int x7, int y7, int x8, int y8, int x9, int y9, int x10, int y10) { Polygon myPoly = new Polygon(); myPoly.addPoint(x1, y1); myPoly.addPoint(x2, y2); myPoly.addPoint(x3, y3); myPoly.addPoint(x4, y4); myPoly.addPoint(x5, y5); myPoly.addPoint(x6, y6); myPoly.addPoint(x7, y7); myPoly.addPoint(x8, y8); myPoly.addPoint(x9, y9); myPoly.addPoint(x10, y10); g.drawPolygon(myPoly); }
public void paintComponent(Graphics g) { super.paintComponent(g); int xCenter = getSize().width / 2; int yCenter = getSize().height / 2; int radius = (int) (Math.min(getSize().width, getSize().height) * 0.4); // Create a Polygon object Polygon polygon = new Polygon(); // Add points to the polygon polygon.addPoint(xCenter + radius, yCenter); polygon.addPoint( (int) (xCenter + radius * Math.cos(2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(2 * Math.PI / 6))); polygon.addPoint( (int) (xCenter + radius * Math.cos(2 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(2 * 2 * Math.PI / 6))); polygon.addPoint( (int) (xCenter + radius * Math.cos(3 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(3 * 2 * Math.PI / 6))); polygon.addPoint( (int) (xCenter + radius * Math.cos(4 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(4 * 2 * Math.PI / 6))); polygon.addPoint( (int) (xCenter + radius * Math.cos(5 * 2 * Math.PI / 6)), (int) (yCenter - radius * Math.sin(5 * 2 * Math.PI / 6))); // Draw the polygon g.drawPolygon(polygon); }
private void paintObstacle(Graphics g, Obstacle obstacle) { Polygon polygon = obstacle.getPolygon(); boolean opaque = obstacle.getOpaque(); if (opaque) { g.setColor(obstacle.getOpaqueBackgroundColor()); g.fillPolygon(polygon); g.setColor(obstacle.getOpaqueForegroundColor()); g.drawPolygon(polygon); } else { g.setColor(obstacle.getBackgroundColor()); g.fillPolygon(polygon); g.setColor(obstacle.getForegroundColor()); g.drawPolygon(polygon); } }
/** * Draws an open regular polygon with a specified number of sides.<br> * The center of this polygon is specified by centerX,centerY and its size is specified by radius * <br> * (As in the radius of the circle the regular polygon would fit inside). <br> * Precondition: sides >= 3 <br> * Example: Expo.drawRegularPolygon(g,300,200,100,8); Draws an open regular octagon with a radius * of 100 pixels whose center is located at the coordinate (300,200). */ public static void drawRegularPolygon( Graphics g, int centerX, int centerY, int radius, int sides) { int xCoord[] = new int[sides]; int yCoord[] = new int[sides]; double rotate; if (sides % 2 == 1) rotate = halfPI; else rotate = halfPI + Math.PI / sides; for (int k = 0; k < sides; k++) { xCoord[k] = (int) Math.round(Math.cos(twoPI * k / sides - rotate) * radius) + centerX; yCoord[k] = (int) Math.round(Math.sin(twoPI * k / sides - rotate) * radius) + centerY; } if (sides == 3) yCoord[1] = yCoord[2]; g.drawPolygon(xCoord, yCoord, sides); }
@Override public void repaint(Graphics g) { if (tiles.size() >= 2) { g.setColor(new Color(255, 255, 255, 255)); Tile[] t = new Tile[tiles.size()]; tiles.toArray(t); Area a = new Area(t); for (int i = 0; i < a.getPolygon().npoints; i++) { Point p = new Point( tiles.get(i).matrix(ctx).bounds().xpoints[0], tiles.get(i).matrix(ctx).bounds().ypoints[0]); Point pp; if (i == a.getPolygon().npoints - 1) pp = new Point( tiles.get(0).matrix(ctx).bounds().xpoints[0], tiles.get(0).matrix(ctx).bounds().ypoints[0]); else pp = new Point( tiles.get(i + 1).matrix(ctx).bounds().xpoints[0], tiles.get(i + 1).matrix(ctx).bounds().ypoints[0]); g.drawLine(p.x, p.y, pp.x, pp.y); } g.drawPolygon(a.getPolygon()); if (a.contains(ctx.players.local())) { g.setColor(new Color(0, 255, 0, 100)); } else { g.setColor(new Color(255, 0, 0, 100)); } g.fillPolygon(ctx.players.local().tile().matrix(ctx).bounds()); } else { g.setColor(new Color(255, 0, 0, 100)); g.fillPolygon(ctx.players.local().tile().matrix(ctx).bounds()); } for (int i = 0; i < tiles.size(); i++) { if (i == gui.tileList.getSelectedIndex()) g.setColor(Color.yellow); else g.setColor(new Color(0, 0, 255, 150)); g.fillPolygon(tiles.get(i).matrix(ctx).bounds()); } }
private void paintDevice(Graphics g, Device d) { // reads the robot's current position model.getRobot().readPosition(d.getRobotPosition()); Polygon currentShape = d.getShape(); // draws the shape Polygon globalShape = new Polygon(); Point2D point = new Point2D.Double(); for (int i = 0; i < currentShape.npoints; i++) { point.setLocation(currentShape.xpoints[i], currentShape.ypoints[i]); // calculates the coordinates of the point according to the local position d.getLocalPosition().rotateAroundAxis(point); // calculates the coordinates of the point according to the robot position d.getRobotPosition().rotateAroundAxis(point); // adds the point to the global shape globalShape.addPoint((int) Math.round(point.getX()), (int) Math.round(point.getY())); } g.setColor(d.getBackgroundColor()); g.fillPolygon(globalShape); g.setColor(d.getForegroundColor()); g.drawPolygon(globalShape); }
public static void drawPolygon( Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x5, int y5, int x6, int y6) { Polygon myPoly = new Polygon(); myPoly.addPoint(x1, y1); myPoly.addPoint(x2, y2); myPoly.addPoint(x3, y3); myPoly.addPoint(x4, y4); myPoly.addPoint(x5, y5); myPoly.addPoint(x6, y6); g.drawPolygon(myPoly); }
public void flecha(Graphics papel, int x1, int y1, int x2, int y2) { double ang = 0.0, angSep = 0.0; double tx = 0, ty = 0; int dist = 0; Point punto1 = null, punto2 = null; punto2 = new Point(x1, y1); punto1 = new Point(x2, y2); dist = 15; ty = -(punto1.y - punto2.y) * 1.0; tx = (punto1.x - punto2.x) * 1.0; ang = Math.atan(ty / tx); if (tx < 0) ang += Math.PI; Point p1 = new Point(), p2 = new Point(), punto = punto2; angSep = 25.0; p1.x = (int) (punto.x + dist * Math.cos(ang - Math.toRadians(angSep))); p1.y = (int) (punto.y - dist * Math.sin(ang - Math.toRadians(angSep))); p2.x = (int) (punto.x + dist * Math.cos(ang + Math.toRadians(angSep))); p2.y = (int) (punto.y - dist * Math.sin(ang + Math.toRadians(angSep))); Graphics2D g2D = (Graphics2D) papel; papel.setColor(Color.black); g2D.setStroke(new BasicStroke(1.2f)); papel.drawLine(punto1.x, punto1.y, punto.x, punto.y); int x[] = {p1.x, punto.x, p2.x}; int y[] = {p1.y, punto.y, p2.y}; Polygon myTri = new Polygon(x, y, 3); papel.setColor(Color.BLACK); papel.drawPolygon(myTri); papel.fillPolygon(myTri); }