import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Test04 extends JPanel {
public static class location {
public int x;
public int y;
public location(int x,int y) {
this.x=x;this.y=y;
}
}
static int a[]=new int[6];
static int b[]=new int[6];
static int index=6;
ArrayList<Point>allPoint=new ArrayList<Point>();
ArrayList<Point>drawPoint=new ArrayList<Point>();
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
for(int Sum=0;Sum<=index;Sum++) {
if(Sum==index-1){
drawLine(a[Sum], b[Sum], a[0],b[0],g2d);
break;
}
else{drawLine(a[Sum], b[Sum], a[(Sum+1)],b[Sum+1],g2d);}
}
boolean spanNeedFill;
location l=new location(a[0]+10,b[0]+10);
Stack<location> stack=new Stack<location>();
stack.push(l);
while(!stack.empty()){
location lo=stack.pop();
int x=lo.x;
int y=lo.y;
while(!allPoint.contains(new Point(x,y))&&!drawPoint.contains(new Point(x,y))){
drawPoint.add(new Point(x,y));
x++;
}
int xr=x-1;
x=lo.x-1;
while(!allPoint.contains(new Point(x,y))&&!drawPoint.contains(new Point(x,y))){
drawPoint.add(new Point(x,y));
x--;
}
int xl=x+1;
x=xl;y=y+1;
while(x<=xr) {
spanNeedFill=false;
while(!allPoint.contains(new Point(x,y))&&!drawPoint.contains(new Point(x,y))){
spanNeedFill=true;
x++;
}
if(spanNeedFill){
location lc=new location(x-1,y);
stack.push(lc);
spanNeedFill=false;
}
while(allPoint.contains(new Point(x,y))||drawPoint.contains(new Point(x,y))&&x<=xr)
x++;
}
x=xl;y=y-2;
while(x<=xr){
spanNeedFill=false;
while(!allPoint.contains(new Point(x,y))&&!drawPoint.contains(new Point(x,y))){
spanNeedFill=true;
x++;
}
if(spanNeedFill){
location rc = new location(x-1,y);
stack.push(rc);
spanNeedFill=false;
}
while(allPoint.contains(new Point(x,y))||drawPoint.contains(new Point(x,y))&&x<=xr)
x++;
}
}
for(int i=0;i<drawPoint.size();i++){
Point p=drawPoint.get(i);
g2d.setColor(Color.blue);
g2d.fillRect(p.x,p.y,1,1);
}
}
public void drawLine(int x0,int y0,int x1,int y1,Graphics2D g) {
int x = x0; int y = y0;
int w = x1 - x0;
int h = y1 - y0;
int dx1 = w < 0 ? -1: (w > 0 ? 1 : 0);
int dy1 = h < 0 ? -1: (h > 0 ? 1 : 0);
int dx2,dy2;
int fastStep,slowStep;
if(Math.abs(w)>Math.abs(h)) {
fastStep= Math.abs(w);
slowStep= Math.abs(h);
dx2 = w < 0 ? -1: (w > 0 ? 1 : 0);
dy2 = 0;
}
else {
fastStep= Math.abs(h);
slowStep= Math.abs(w);
dx2= 0;
dy2= h < 0 ? -1 : (h > 0 ? 1 : 0);
}
int numerator = fastStep>> 1;
for (int i = 0; i <=fastStep; i++) {
g.setColor(Color.blue);
g.fillRect(x,y,1,1);
Point p=new Point(x,y);
allPoint.add(p);
numerator+= slowStep;
if (numerator >=fastStep) {
numerator-= fastStep; x+= dx1; y+= dy1;
}else {
x+= dx2; y+= dy2;
}
}
}
public static void main(String[] args) {
System.out.println("依次输入6个点横纵坐标:");
Scanner scanner = new Scanner(System.in);
for(int i=0;i<6;i++) {a[i]=scanner.nextInt(); b[i]=scanner.nextInt();}
JFrame frame = new JFrame("test");
frame.setLayout(null);
frame.setContentPane(new Test04());
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);//设置窗口相对于指定组件的位置;如果组件当前未显示或者为null,则此窗口将置于屏幕的中央。
frame.setVisible(true);
}
}
六边形填充
于 2021-11-26 14:45:49 首次发布