packagePolygonScanningAndFilling;importjava.util.Iterator;importjava.util.Timer;importjava.util.TimerTask;import javax.swing.*;import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.io.IOException;public class PolygonScanning extendsJPanel {static intX0;static intY0;static intX1;static intY1;static int a[]=new int [10]; //保存点击的10个x坐标
static int b[]=new int [10]; //保存点击的10个y坐标
static int index=0;static int time=0;static int time2=0;static booleanadd;
@Overrideprotected voidpaintComponent(Graphics g) {super.paintComponent(g);this.addMouseListener(newMouseAdapter() {public voidmouseExited(MouseEvent e) {
time++;
repaint();
}public voidmouseClicked(MouseEvent e) {if(e.getButton() ==e.BUTTON1) {
add=true;if(index!=0)
{for(int i=0;i
{if(a[i]==e.getX()&&b[i]==e.getY())
add=false;
}
}if(add)
{ a[index]=e.getX();
b[index]=e.getY();
System.out.println("坐标为("+a[index]+","+b[index]+")");
index++;//frame.setVisible(true);
repaint();
System.out.print(time2);if(time2==0)
time2++;
}
}//if(e.getButton() == e.BUTTON3)//{//
//}
}
});
Graphics2D g2d=(Graphics2D)g;int Ymax=0;for(int i=0;i
{if(Ymax
Ymax=b[i];
}//System.out.println("Ymax"+Ymax);
/** 画出多边形*/
if(time2>0)
{
System.out.print("开始");int Sum=0;for(;Sum<=index;Sum++) {if(Sum==index-1)
{
g2d.drawLine(a[Sum], b[Sum], a[0],b[0]);break;
}else{
g2d.drawLine(a[Sum], b[Sum], a[Sum+1],b[Sum+1]);
}
}
}if(time!=0) {
Node [] head=new Node [Ymax]; //建立对应扫描边数的链表长度
for(int i=0;i
{if(b[i]
{if(head[b[i]]==null)
head[b[i]]=new Node(0,0,0);
head[b[i]].ymin=b[i];if(head[b[i]].next==null) //该点是第一个插入的节点
{
head[b[i]].next=new Node(0,0,0);
head[b[i]].next.x=a[i];
head[b[i]].next.dx=(float)(a[i]-a[i+1])/(b[i]-b[i+1]);
head[b[i]].next.yMax=b[i+1]; //ymax为后一点的y
}else { //该点不是第一个插入的节点
if(head[b[i]].next.next==null)
head[b[i]].next.next=new Node(0,0,0);if((float)(a[i]-a[i+1])/(b[i]-b[i+1])
{
head[b[i]].next.next.x=head[b[i]].next.x;
head[b[i]].next.next.dx=head[b[i]].next.dx;
head[b[i]].next.next.yMax=head[b[i]].next.yMax;
head[b[i]].next.x=a[i];
head[b[i]].next.dx=(float)(a[i]-a[i+1])/(b[i]-b[i+1]);
head[b[i]].next.yMax=b[i+1];
}else{
head[b[i]].next.next.x=a[i];
head[b[i]].next.next.dx=(float)(a[i]-a[i+1])/(b[i]-b[i+1]);
head[b[i]].next.next.yMax=b[i+1];
}
}
}else{if(head[b[i+1]]==null)
head[b[i+1]]=new Node(0,0,0);
head[b[i+1]].ymin=b[i+1];if(head[b[i+1]].next==null) //该点是第一个插入的节点
{
head[b[i+1]].next=new Node(0,0,0);
head[b[i+1]].next.x=a[i+1];
head[b[i+1]].next.dx=(float)(a[i]-a[i+1])/(b[i]-b[i+1]);
head[b[i+1]].next.yMax=b[i]; //ymax为后一点的y
}else { //该点不是第一个插入的节点
if(head[b[i+1]].next.next==null)
head[b[i+1]].next.next=new Node(0,0,0);if((float)(a[i]-a[i+1])/(b[i]-b[i+1])
{
head[b[i+1]].next.next.x=head[b[i+1]].next.x;
head[b[i+1]].next.next.dx=(float)head[b[i+1]].next.dx;
head[b[i+1]].next.next.yMax=head[b[i+1]].next.yMax;
head[b[i+1]].next.x=a[i+1];
head[b[i+1]].next.dx=(float)(a[i]-a[i+1])/(b[i]-b[i+1]);
head[b[i+1]].next.yMax=b[i];
}else{
head[b[i+1]].next.next.x=a[i+1];
head[b[i+1]].next.next.dx=(float)(a[i]-a[i+1])/(b[i]-b[i+1]);
head[b[i+1]].next.next.yMax=b[i];
}
}
}
}if(index>0)
{if(b[0]
{if(head[b[0]]==null)
head[b[0]]=new Node(0,0,0);
head[b[0]].ymin=b[0];if(head[b[0]].next==null) //该点是第一个插入的节点
{
head[b[0]].next=new Node(0,0,0);
head[b[0]].next.x=a[0];
head[b[0]].next.dx=(float)(a[0]-a[index-1])/(b[0]-b[index-1]);
head[b[0]].next.yMax=b[index-1]; //ymax为后一点的y
}else { //该点不是第一个插入的节点
if(head[b[0]].next.next==null)
head[b[0]].next.next=new Node(0,0,0);if((float)(a[0]-a[index-1])/(b[0]-b[index-1])
{
head[b[0]].next.next.x=head[b[0]].next.x;
head[b[0]].next.next.dx=head[b[0]].next.dx;
head[b[0]].next.next.yMax=head[b[0]].next.yMax;
head[b[0]].next.x=a[0];
head[b[0]].next.dx=(float)(a[0]-a[index-1])/(b[0]-b[index-1]);
head[b[0]].next.yMax=b[index-1];
}else{
head[b[0]].next.next.x=a[0];
head[b[0]].next.next.dx=(float)(a[0]-a[index-1])/(b[0]-b[index-1]);
head[b[0]].next.next.yMax=b[index-1];
}
}
}else{if(head[b[index-1]]==null)
head[b[index-1]]=new Node(0,0,0);
head[b[index-1]].ymin=b[index-1];if(head[b[index-1]].next==null) //该点是第一个插入的节点
{
head[b[index-1]].next=new Node(0,0,0);
head[b[index-1]].next.x=a[index-1];
head[b[index-1]].next.dx=(float)(a[0]-a[index-1])/(b[0]-b[index-1]);
head[b[index-1]].next.yMax=b[0]; //ymax为后一点的y
}else { //该点不是第一个插入的节点
if(head[b[index-1]].next.next==null)
head[b[index-1]].next.next=new Node(0,0,0);if((float)(a[0]-a[index-1])/(b[0]-b[index-1])
{
head[b[index-1]].next.next.x=head[b[index-1]].next.x;
head[b[index-1]].next.next.dx=head[b[index-1]].next.dx;
head[b[index-1]].next.next.yMax=head[b[index-1]].next.yMax;
head[b[index-1]].next.x=a[index-1];
head[b[index-1]].next.dx=(float)(a[0]-a[index-1])/(b[0]-b[index-1]);
head[b[index-1]].next.yMax=b[0];
}else{
head[b[index-1]].next.next.x=a[index-1];
head[b[index-1]].next.next.dx=(float)(a[0]-a[index-1])/(b[0]-b[index-1]);
head[b[index-1]].next.next.yMax=b[0];
}
}
}
}for(int i=0;i
{ System.out.println("新编表y"+head[i].ymin+"新编表x"+head[i].next.x+"新编表dx"+head[i].next.dx+"新编表yMax"+head[i].next.yMax);if(head[i].next.next!=null)
{
System.out.println("多的"+"新编表y"+head[i].ymin+"新编表x"+head[i].next.next.x+"新编表dx"+head[i].next.next.dx+"新编表yMax"+head[i].next.next.yMax);
}break;
}int YMIN=b[0];for(int i=0;i
{if(YMIN>b[i]&&b[i]!=0)
YMIN=b[i];
}
classAndArray [] ca=newclassAndArray [Ymax];for(int i=YMIN;i
ca[i]=newclassAndArray();//一个点一个点的全装入ca中再排序打印出点
for(int i=0;i
{if(head[i]!=null)if(head[i].next!=null)
{//System.out.println("新编表y"+head[i].ymin+"新编表x"+head[i].next.x+"新编表dx"+head[i].next.dx+"新编表yMax"+head[i].next.yMax);
for(int j=head[i].ymin;j
{
ca[i+j-head[i].ymin].list.add(head[i].next.x+(int)(0.5+((j-head[i].ymin)*head[i].next.dx)));//System.out.print("ca[i+j-head[i].ymin]为"+(i+j-head[i].ymin)+"值为"+ca[i+j-head[i].ymin].list.toString());//System.out.println("Ymin为"+i+" x为"+(head[i].next.x+(j-head[i].ymin)*head[i].next.dx));
}if(head[i].next.next!=null)
{for(int j=head[i].ymin;j
{
ca[i+j-head[i].ymin].list.add(head[i].next.next.x+(int)(0.5+(j-head[i].ymin)*head[i].next.next.dx));//System.out.print("next中ca[i+j-head[i].ymin]为"+(i+j-head[i].ymin)+"值为"+ca[i+j-head[i].ymin].list.toString());//System.out.println("Ymin为"+i+" x为"+head[i].next.next.x+(j-head[i].ymin)*head[i].next.next.dx);
}//System.out.println("多的"+"新编表y"+head[i].ymin+"新编表x"+head[i].next.next.x+"新编表dx"+head[i].next.next.dx+"新编表yMax"+head[i].next.next.yMax);
}
}
}//
for(int i=YMIN;i
{
ca[i].listSort();for (int j = 0; j < ca[i].list.size(); j++) {if(j%2==0||(j==0))
{
g2d.drawLine(ca[i].list.get(j), i, ca[i].list.get(j+1), i);
}
}
System.out.println(ca[i].list.toString());
}
}
}private static voidcreateAndShowGUI() {
JFrame frame= newJFrame();
frame.setLocationRelativeTo(null);
frame.setLayout(null);
JPanel jp=newJPanel();
frame.setVisible(true);
frame.setContentPane(newPolygonScanning());
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}public static void main(String[] args) throwsIOException {
createAndShowGUI();
}
}