import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
public class DrawPolygon extends Frame {
boolean endClick = false;
boolean coatColor = false;
int[] xPoints = new int[20];
int[] yPoints = new int[20];
int nPoints = 0;
public static void main(String[] args) {
DrawPolygon dp = new DrawPolygon();
dp.launchFrame();
}
private void launchFrame() {
this.setLocation(0, 0);
this.setSize(400, 300);
this.setResizable(false);
this.setTitle("Polygon&CoatRed");
this.setBackground(Color.white);
this.setLayout(new FlowLayout());
Button end = new Button("Click point end");
Button coat = new Button("Coat with red");
Button redo = new Button("Redo");
this.add(end);
this.add(coat);
this.add(redo);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
this.addMouseListener(new MouseMonitor());
end.addActionListener(new PointEnd());
coat.addActionListener(new CoatRed());
redo.addActionListener(new Redo());
this.setVisible(true);
}
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, 400, 300);
Color c = g.getColor();
g.setColor(Color.black);
for(int i=0; i<nPoints; i++) {
g.fillOval(xPoints[i]-4/2, yPoints[i]-4/2, 4, 4);
}
for(int i=1; i<nPoints; i++) {
g.drawLine(xPoints[i-1], yPoints[i-1], xPoints[i], yPoints[i]);
}
if(endClick) {
g.drawLine(xPoints[0], yPoints[0], xPoints[nPoints-1], yPoints[nPoints-1]);
}
if(coatColor) {
xPoints[nPoints] = xPoints[0];
yPoints[nPoints] = yPoints[0];
g.setColor(Color.white);
boolean isRed = false;
boolean[] isPeakPoints = new boolean[20];
if( (yPoints[0]-yPoints[nPoints-1])*(yPoints[0]-yPoints[1])>0 ) {
isPeakPoints[0] = true;
}
else {
isPeakPoints[0] = false;
}
for(int i=1; i<nPoints; i++) {
if( (yPoints[i]-yPoints[i-1])*(yPoints[i]-yPoints[i+1])>0 ) {
isPeakPoints[i] = true;
}
else {
isPeakPoints[i] = false;
}
}
int xmin = xPoints[0];
int xmax = xPoints[0];
int ymin = yPoints[0];
int ymax = yPoints[0];
for(int i=1; i<nPoints; i++) {
if(xPoints[i]<xmin) {
xmin = xPoints[i];
}
if(xPoints[i]>xmax) {
xmax = xPoints[i];
}
if(yPoints[i]<ymin) {
ymin = yPoints[i];
}
if(yPoints[i]>ymax) {
ymax = yPoints[i];
}
}
for(int y=ymin; y<=ymax; y++) {
for(int x=xmin; x<=xmax; x++) {
for(int n=0; n<nPoints; n++) {
double d1 = Point2D.distance(x, y, xPoints[n], yPoints[n]);
double d2 = Point2D.distance(x, y, xPoints[n+1], yPoints[n+1]);
double d3 = Point2D.distance(xPoints[n], yPoints[n], xPoints[n+1], yPoints[n+1]);
if( (x==xPoints[n]&&y==yPoints[n]) && isPeakPoints[n] ) {
y = y+4;
x = xmin;
break;
}
else if(Math.abs(d1 + d2 - d3) < 0.05) {
x=x+4;
isRed = !isRed;
if(isRed) {
g.setColor(Color.red);
}
else {
g.setColor(Color.white);
}
break;
}
}
g.fillOval(x, y, 1, 1);
}
}
}
g.setColor(c);
}
private class MouseMonitor extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
xPoints[nPoints] = e.getX();
yPoints[nPoints] = e.getY();
nPoints ++;
repaint();
}
}
public class PointEnd implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
endClick = true;
repaint();
}
}
public class CoatRed implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
endClick = true;
coatColor = true;
repaint();
}
}
public class Redo implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
endClick = false;
coatColor = false;
nPoints = 0;
repaint();
}
}
}