0x12abc
0x12abc

Reputation: 21

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.

Upvotes: 0

Views: 427

Answers (1)

camickr
camickr

Reputation: 324118

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 );

Upvotes: 1

Related Questions