如何使用Point2D.Double作为点绘制多边形?

时间:2013-11-22 04:08:11

标签: java floating-point draw point polygons

无法使用具有小数的坐标设置点,因此我调整了比例。这是我的形象。霸天虎标志。

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Polygon;

import java.awt.Color;

import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;


public class DecepticonComponent extends JComponent

{

    public void paintComponent(Graphics g)

    {

    Graphics2D g2 = (Graphics2D) g;
    Rectangle2D.Double background = new Rectangle2D.Double(0,0,610,671);
    g2.draw(background);
    g2.setColor(Color.black);
    g2.fill(background);

    Color myNewPurple = new Color(103,28,196);

    Polygon centerFace = new Polygon();
    centerFace.addPoint(207,25);
    centerFace.addPoint(277,137);
    centerFace.addPoint(344,137);
    centerFace.addPoint(414,25);
    centerFace.addPoint(372,284);
    centerFace.addPoint(310,335);
    centerFace.addPoint(249,284);
    g2.draw(centerFace);
    g2.setColor(myNewPurple);
    g2.fill(centerFace);

    Polygon centerTriangle = new Polygon();
    centerTriangle.addPoint(292,193);
    centerTriangle.addPoint(328,193);
    centerTriangle.addPoint(310,249);
    g2.draw(centerTriangle);
    g2.setColor(Color.black);
    g2.fill(centerTriangle);

    Polygon robotHead = new Polygon();
    robotHead.addPoint(25,25);//1
    robotHead.addPoint(95,123);//2
    robotHead.addPoint(222,165);//3
    robotHead.addPoint(231,228);//4
    robotHead.addPoint(109,196);//5
    robotHead.addPoint(109,207);//6
    robotHead.addPoint(233,242);//7
    robotHead.addPoint(237,270);//8
    robotHead.addPoint(116,242);//9
    robotHead.addPoint(116,253);//10
    robotHead.addPoint(235,282);//11
    robotHead.addPoint(310,349);//12
    robotHead.addPoint(385,282);//11
    robotHead.addPoint(507,253);//10
    robotHead.addPoint(507,242);//9
    robotHead.addPoint(384,270);//8
    robotHead.addPoint(388,242);//7
    robotHead.addPoint(511,207);//6
    robotHead.addPoint(511,193);//5
    robotHead.addPoint(390,228);//4
    robotHead.addPoint(399,165);//3
    robotHead.addPoint(511,123);//2
    robotHead.addPoint(581,25);//1
    robotHead.addPoint(560,305);//13
    robotHead.addPoint(310,641);
    robotHead.addPoint(60,305);//13
    g2.draw(robotHead);
    g2.setColor(myNewPurple);
    g2.fill(robotHead);

    Polygon eye1 = new Polygon();
    eye1.addPoint(123,319);
    eye1.addPoint(263,368);
    eye1.addPoint(235,431);
    g2.draw(eye1);
    g2.setColor(Color.black);
    g2.fill(eye1);

    Polygon eye2 = new Polygon();
    eye2.addPoint(497,319);
    eye2.addPoint(357,368);
    eye2.addPoint(385,431);
    g2.draw(eye2);
    g2.setColor(Color.black);
    g2.fill(eye2);

    Polygon chinL = new Polygon();
    chinL.addPoint(62,333);
    chinL.addPoint(276,620);
    chinL.addPoint(84,543);
    g2.draw(chinL);
    g2.setColor(myNewPurple);
    g2.fill(chinL);

    Polygon chinR = new Polygon();
    chinR.addPoint(558,333);
    chinR.addPoint(344,620);
    chinR.addPoint(536,543);
    g2.draw(chinR);
    g2.setColor(myNewPurple);
    g2.fill(chinR);
    }
}

1 个答案:

答案 0 :(得分:3)

不使用Ploygon,但您可以使用Shape的API并使用类似Path2D的内容

请查看Drawing Arbitrary Shapes了解详情