本文整理了Java中java.awt.Polygon
类的一些代码示例,展示了Polygon
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Polygon
类的具体详情如下:
包路径:java.awt.Polygon
类名称:Polygon
[英]The Polygon
class encapsulates a description of a closed, two-dimensional region within a coordinate space. This region is bounded by an arbitrary number of line segments, each of which is one side of the polygon. Internally, a polygon comprises of a list of (x, y) coordinate pairs, where each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon. The first and final pairs of (x, y) points are joined by a line segment that closes the polygon. This Polygon
is defined with an even-odd winding rule. (The even-odd rule specifies that a point lies inside the path if a ray drawn in any direction from that point to infinity is crossed by path segments an odd number of times.) This class's hit-testing methods, which include the contains
, intersects
and inside
methods, use the insideness definition described in the Shape class comments.
[中]Polygon
类封装了坐标空间中封闭二维区域的描述。该区域由任意数量的线段包围,每个线段都是多边形的一侧。在内部,多边形由一系列(x,y)坐标对组成,每对坐标对定义多边形的一个顶点,两个连续的坐标对是多边形边的一条直线的端点。第一对和最后一对(x,y)点由闭合多边形的线段连接。这个Polygon
是用奇偶缠绕规则定义的*(奇偶规则规定,如果从该点到无穷远的任何方向绘制的光线与路径段相交奇数次,则该点位于路径内。)*该类的命中测试方法,包括contains
、intersects
和inside
方法,使用Shape类注释中描述的内部性定义。
代码示例来源:origin: stackoverflow.com
gui.add(l1);
Polygon pointer = new Polygon();
pointer.addPoint(
strokePad + radii + pointerPad,
bottomLineY);
pointer.addPoint(
strokePad + radii + pointerPad + pointerSize,
bottomLineY);
pointer.addPoint(
strokePad + radii + pointerPad + (pointerSize / 2),
height - strokePad);
} else {
pointer.addPoint(
width - (strokePad + radii + pointerPad),
bottomLineY);
pointer.addPoint(
width - (strokePad + radii + pointerPad + pointerSize),
bottomLineY);
pointer.addPoint(
width - (strokePad + radii + pointerPad + (pointerSize / 2)),
height - strokePad);
代码示例来源:origin: stackoverflow.com
import java.awt.Polygon;
public class JavaTest {
public static void main(final String[] args) {
final Polygon polygon = new Polygon();
polygon.addPoint(-10, -10);
polygon.addPoint(-10, 10);
polygon.addPoint(10, 10);
polygon.addPoint(10, -10);
System.out.println(polygon.contains(0, 0));
}
}
代码示例来源:origin: plantuml/plantuml
private void addSymbolNavasocInv(GeneralPath generalPath, Point2D.Double point, LineSegmentInt seg) {
final Polygon arrow = new Polygon();
arrow.addPoint(0, 0);
arrow.addPoint(13, -8);
arrow.addPoint(6, 0);
arrow.addPoint(13, 8);
appendAndRotate(generalPath, point, seg, arrow);
}
代码示例来源:origin: plantuml/plantuml
@Override
public void draw(ColorMapper colorMapper, Graphics2D g2d) {
final Polygon p = new Polygon();
p.addPoint(size, 0);
p.addPoint(size * 2, size);
p.addPoint(size, size * 2);
p.addPoint(0, size);
g2d.setColor(colorMapper.getMappedColor(getYellow()));
g2d.fill(p);
g2d.setColor(colorMapper.getMappedColor(getRed()));
g2d.draw(p);
}
}
代码示例来源:origin: stackoverflow.com
public void paintComponent(Graphics g) {
super.paintComponent(g);
Polygon outerPolygon = new Polygon();
for (int i = 0; i < numOfPoints; i++) {
outerPolygon.addPoint(
xOffset + (int) (linePosition + sizeModifer
* Math.cos(i * 2 * Math.PI / numOfPoints)),
* Math.sin(i * 2 * Math.PI / numOfPoints)));
Polygon innerPolygon = new Polygon();
for (int i = 0; i < numOfPoints; i++) {
int randomRange = 5 + (int) (Math.random() * ((sizeModifer - 5) + 1));
innerPolygon.addPoint(
xOffset + (int) (linePosition + randomRange
* Math.cos(i * 2 * Math.PI / numOfPoints)),
public Dimension getPreferredSize() {
return new Dimension(160, 160);
代码示例来源:origin: stackoverflow.com
mainMap = new JFrame();
mainMap.setResizable(false);
mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
poly = new Polygon(xPoly, yPoly, xPoly.length);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawPolygon(poly);
return new Dimension(800, 600);
super.mouseClicked(me);
if (poly.contains(me.getPoint())) {
System.out.println("Clicked polygon");
p.addMouseListener(ma);//add listener to panel
mainMap.add(p);
mainMap.pack();
mainMap.setVisible(true);
代码示例来源:origin: stackoverflow.com
setPreferredSize(new Dimension(1200, 720));
poly = new Polygon(new int[]{ax, bx, cx, dx}, new int[]{ay, by, cy, dy}, 4);
g2d.draw(poly);
g2d.fill(poly);
for (int i = 0; i < 150; i++) {
flag++;
poly.addPoint(ax, ay);
poly.addPoint(bx, (by = flag % 3 == 0 ? --by : by));
poly.addPoint(cx, cy++);
poly.addPoint(dx, dy);
Thread.sleep(SPEED);
repaint();
Frame frame = new JFrame();
PolyToRectangle se = new PolyToRectangle();
frame.add(se);
frame.pack();
frame.setVisible(true);
se.polyToRectangle();
代码示例来源:origin: stackoverflow.com
private int[] p3x = {400, 400, 460, 460, 440, 440, 420, 420, 400};
private int[] p3y = {400, 460, 460, 400, 400, 440, 440, 400, 400};
private Polygon p1 = new Polygon(p1x, p1y, p1x.length);
private Polygon p2 = new Polygon(p2x, p2y, p2x.length);
private Polygon p3 = new Polygon(p3x, p3y, p3x.length);
private AffineTransform at = new AffineTransform();
private double dt = DELTA_THETA;
this.setPreferredSize(new Dimension(700, 700));
this.setBackground(Color.white);
p1.translate(-50, +100);
p2.translate(-100, -100);
ds = -ds;
repaint();
at.scale(scale, scale);
at.translate(-p3x[5] + 10, -p3y[5]);
g2d.setPaint(Color.blue);
JFrame frame = new JFrame("Affine Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AffineTest sl = new AffineTest();
frame.add(sl);
frame.pack();
frame.setVisible(true);
sl.start();
代码示例来源:origin: stackoverflow.com
private static final int[] y = { 60, 105, 105, 110, 95, 95 };
private static final Polygon POLYGON = new Polygon(x, y, Math.min(x.length, y.length));
private static final Ellipse2D CIRCLE = new Ellipse2D.Double(100, 40, 45, 45);
this.shape = shape;
Rectangle bounds = shape.getBounds();
this.preferredSize = new Dimension(bounds.x + bounds.width, bounds.y + bounds.height);
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.BLUE);
g2.draw(shape);
g2.fill(shape);
@Override
public void run() {
JFrame mainFrame = new JFrame("Program");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SelectShape polygon = new SelectShape(POLYGON);
SelectShape circle = new SelectShape(CIRCLE);
tabbedPane.addTab("Polygon", polygon);
tabbedPane.addTab("Circle", circle);
mainFrame.add(tabbedPane);
mainFrame.pack();
mainFrame.setVisible(true);
代码示例来源:origin: stackoverflow.com
private int[] xs = { p1.x, p2.x, p3.x };
private int[] ys = { p1.y, p2.y, p3.y };
private Polygon triangle = new Polygon(xs, ys, xs.length);
if (isPreferredSizeSet()) {
return super.getPreferredSize();
return new Dimension(PREF_W, PREF_H);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(255, 255, 255)); // !! never painted
g.fillPolygon(triangle); // !! never painted
g.drawOval(10, 10, 10, 10); // !!!! painted only when i set preferredSize
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
gridPanel.add(new MyDrawingPanel());
JFrame frame = new JFrame("PaintPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(gridPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
代码示例来源:origin: stackoverflow.com
public class Test {
public static void main(String[] args) {
int width = 500;
int height = 500;
int[] x = { (width / 2) - 50, (width / 2), (width / 2) + 50 };
int[] y = { (height / 2) + 50, (height / 2) - 50, (height / 2) + 50 };
Polygon p = new Polygon(x, y, 3);
JFrame f = new JFrame("Thats what a triangle looks like");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
g.fillPolygon(p);
}
};
panel.setPreferredSize(new Dimension(width, height));
f.getContentPane().add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
代码示例来源:origin: stackoverflow.com
Graphics2D g2d = (Graphics2D) g.create();
Polygon triangle = new Polygon();
triangle.addPoint(0, 0);
triangle.addPoint(10, 5);
triangle.addPoint(0, 10);
triangle.translate(150, 10);
g2d.translate(50, 50);
g2d.setColor( Color.YELLOW );
g2d.fill( balloon );
g2d.setColor( Color.GRAY );
g2d.draw( balloon );
g2d.dispose();
JFrame frame = new JFrame("BalloonRectangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new BalloonRectangle() );
frame.setLocationByPlatform( true );
frame.setSize(250, 200);
frame.setVisible( true );
代码示例来源:origin: stackoverflow.com
public class DrawPolyPanel extends JPanel{
public DrawPolyPanel(){
setPreferredSize(new Dimension(200, 200));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Polygon p = new Polygon();
for (int i = 0; i < 6; i++)
p.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 6)),
(int) (100 + 50 * Math.sin(i * 2 * Math.PI / 6)));
g.drawPolygon(p);
}
public static void main(String[] args){
JFrame frame = new JFrame("DrawPoly");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawPolyPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
代码示例来源:origin: stackoverflow.com
shape = new Polygon();
setSize(120, 120);
shape.addPoint(0, 0);
shape.addPoint(0, 60);
shape.addPoint(90, 0);
return (new Dimension(120, 120));
return shape.contains(x, y);
System.err.println("paintComponent()");
super.paintComponent(g);
g.fillPolygon(shape);
JFrame frame = new JFrame();
JPanel panel = new JPanel();
ShiftingButton button = new ShiftingButton();
panel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
代码示例来源:origin: stackoverflow.com
JFrame frame = new JFrame("Test");
frame.add(new JComponent() {
Point p1, p2; boolean first;
setPreferredSize(new Dimension(400, 400));
addMouseListener(new MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(p1.x-1, p1.y-1, 3, 3); g.drawString("p1", p1.x, p1.y);
g.fillRect(p2.x-1, p2.y-1, 3, 3); g.drawString("p2", p2.x, p2.y);
g2d.translate(p1.x, p1.y);
g2d.rotate(angle);
g2d.drawArc(0, -diameter/2, diameter, diameter, 0, 180);
g2d.fill(new Polygon(new int[] {0,10,-10}, new int[] {0,-10,-10}, 3));
代码示例来源:origin: org.cytoscape/vizmap-gui-impl
@Override
public void paintComponent(Graphics g) {
clear(g);
Graphics2D g2d = (Graphics2D) g;
//this.setPreferredSize(new Dimension(strW + 6, 1));
int panelHeight = this.getHeight() - 30;
Polygon poly = new Polygon();
int top = 10;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(1.0f));
int center = (this.getWidth() / 2) + 4;
poly.addPoint(center, top);
poly.addPoint(center - 6, top + 15);
poly.addPoint(center, top + 15);
g.fillPolygon(poly);
g2d.drawLine(center, top, center, panelHeight);
g2d.setColor(Color.DARK_GRAY);
g2d.setFont(new Font("SansSerif", Font.BOLD, 10));
final String label = type.getDisplayName();
final int width = SwingUtilities.computeStringWidth(g2d.getFontMetrics(), label);
AffineTransform af = new AffineTransform();
af.rotate(Math.PI + (Math.PI / 2));
g2d.setTransform(af);
g2d.setColor(Color.black);
g2d.drawString(type.getDisplayName(), (-this.getHeight() / 2) - (width / 2),
(this.getWidth() / 2) + 5);
}
代码示例来源:origin: opentripplanner/OpenTripPlanner
BasicStroke.JOIN_BEVEL, 1.0f, new float[] { 4 * lineWidth, lineWidth },
2 * lineWidth);
Stroke arrowStroke = new ShapeStroke(new Polygon(new int[] { 0, 0, 30 }, new int[] { 0, 20,
10 }, 3), lineWidth / 2, 5.0f * lineWidth, 2.5f * lineWidth);
BasicStroke thinStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
FontMetrics largeFontMetrics = context.graphics.getFontMetrics(largeFont);
context.graphics.setFont(largeFont);
context.graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
context.graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
代码示例来源:origin: stackoverflow.com
setLayout(new BorderLayout());
add(new DrawSine(), BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setTitle("Exercise13_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawString("Y", 220, 40);
Polygon p = new Polygon();
Polygon p2 = new Polygon();
p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
代码示例来源:origin: stackoverflow.com
int[] yTriangle = {60,60,40};
obstacles[1] = new Area(new Polygon(xTriangle, yTriangle, 3));
obstacles[2] = new Area(new Polygon(xDiamond, yDiamond, 4));
obstacles[3] = new Area(new Polygon(xOther, yOther, 4));
public void actionPerformed(ActionEvent e) {
animate();
imageLabel.repaint();
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.ORANGE);
for (Area obstacle : obstacles) {
if (doAreasCollide(obstacle, player)) {
g.setColor(Color.GREEN);
g.fill(obstacle);
g.setColor(Color.YELLOW);
g.fill(player);
代码示例来源:origin: stackoverflow.com
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Polygon bubble = new Polygon();
bubble.addPoint(x + 10, y + 5);
bubble.addPoint(x + width - 10, y + 5);
bubble.addPoint(x + width - 10, y + height / 3);
bubble.addPoint(x + width, y + height / 2);
bubble.addPoint(x + width - 10, y + height * 2 / 3);
bubble.addPoint(x + width - 10, y - 5 + height);
bubble.addPoint(x + 10, y - 5 + height);
Graphics2D g2d = (Graphics2D) g;
//Area rect = new Area(new Rectangle(x, y, width, height));
Shape clip = g2d.getClip();
Area rect = new Area(clip);
rect.subtract(new Area(bubble));
g2d.setClip(rect);
g2d.setColor(c.getParent().getBackground());
g2d.fillRect(0, 0, width, height);
//g2d.setClip(null);
g2d.setClip(clip);
g2d.setColor(Color.BLACK);
g2d.draw(bubble);
}
内容来源于网络,如有侵权,请联系作者删除!