Processing Forum
- Poly p;
- void setup() {
- int[] x = { 20, 40, 40, 60, 60, 20 };
- int[] y = { 20, 20, 40, 40, 60, 60 };
- p = new Poly(x, y, 6);
- }
- void draw(){
- background(255);
- if (p.contains(mouseX, mouseY)) {
- fill(255, 0, 0);
- } else {
- fill(255);
- }
- p.drawMe();
- }
- /*
- The class inherit all the fields, constructors and functions
- of the java.awt.Polygon class, including contains(), xpoint,ypoint,npoint
- */
- class Poly extends java.awt.Polygon {
- public Poly(int[] x,int[] y, int n) {
- //call the java.awt.Polygon constructor
- super(x, y, n);
- }
- void drawMe() {
- beginShape();
- for (int i = 0; i < npoints; i++) {
- vertex(xpoints[i], ypoints[i]);
- }
- endShape(CLOSE);
- }
- }