简体   繁体   中英

How to fill an irregular polygon which has been drawn by mouse clicks?

I am working on a java graphics project. So basically, i need to draw a shape on my JPanel using mouse clicks. I have to prompt the user for the border color which is working good but I don't know how to fill the irregular polygon.. I have prompted the user for choosing the shape color using JColorChooser but how to fill it?

I am trying to implement it in my paintComponent method.

but i dont know how to fill the irregular polygon.

  1. Create a Polygon object to represent the Shape .
  2. Use the Graphics2D draw(Shape) method to paint the Shape outline and the fill(Shape) method to fill the Shape

Simple example:

Polygon triangle = new Polygon();
triangle.addPoint(0, 0);
triangle.addPoint(15, 30);
triangle.addPoint(30, 0);
g2d.setColor( Color.RED );
g2d.fill( triangle );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM