六边形填充

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值