java撰寫圖形學的填充演算法_Java撰寫圖形學的種子填充演算法

package com.tony.view;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Point;

import java.awt.Robot;

import java.awt.image.BufferedImage;

import java.util.Vector;

import javax.swing.JPanel;

public class DrawComponentPanel extends JPanel {

private int[] xPoints = new int[4];

private int[] yPoints = new int[4];

private Color fillColor;

private Color curColor;

private boolean fill = false;

public DrawComponentPanel() {

xPoints[0] = 0;

xPoints[1] = 0;

xPoints[2] = 0;

xPoints[3] = 0;

yPoints[0] = 0;

yPoints[1] = 0;

yPoints[2] = 0;

yPoints[3] = 0;

}

protected void paintComponent(final Graphics g) {

super.paintComponent(g);

Robot robot = null;

Point p;

try {

robot = new Robot();

} catch (Exception ex) {

ex.printStackTrace();

}

Vector v = new Vector();

int savex = 0, xleft = 0, xright = 0, pflag = 0, xenter = 0, x = 0, y = 0;

p = new Point(xPoints[0] + 1, yPoints[0] + 1);

v.add(0, p); /* 種子壓入堆疊 */

// System.out.println(" " + ((Point) v.get(0)).x);

BufferedImage image = (BufferedImage)this.createImage(700, 700);

Graphics ig = image.getGraphics();

ig.setColor(Color.black);

ig.drawPolygon(xPoints, yPoints, 4);

ig.setColor(fillColor);

g.drawImage(image, 0, 0, null);

while (v.size() != 0 && fill) {

/* 棧頂象素出棧 */

x = ((Point) v.get(0)).x;

y = ((Point) v.get(0)).y;

v.remove(0);

// System.out.println("x:" + x + "y:" + y + "Color.BLack"

// + Color.BLACK.getRGB() + "robot.getPixelColor(x, y).getRGB():" + image.getRGB(x, y));

savex = x; /* 儲存種子坐標x分量的值 */

while (image.getRGB(x, y) != Color.BLACK.getRGB()) {

ig.drawLine(x, y, x, y); /* 填充種子右側的象素 */

x++;

// System.out.println("--------->x" + x);

}

xright = x - 1; /* 得到種子區段的右端點 */

x = savex - 1; /* 準備向種子左側填充 */

System.out.println("--------->0");

while (image.getRGB(x, y) != Color.BLACK.getRGB()) {

ig.drawLine(x, y, x, y); /* 填充種子左側的象素 */

x--;

// System.out.println("--------->x" + x);

}

// System.out.println("--------->1");

xleft = x + 1; /* 得到種子區段的左端點 */

x = xleft;

y = y - 1; /* 考慮種子相鄰的上掃描線 */

while (x <= xright) {

pflag = 0; /* 找到新種子的標誌:0為假;1為真 */

while (image.getRGB(x, y) != Color.BLACK

.getRGB()

&& image.getRGB(x, y) != fillColor

.getRGB() && x < xright) {

if (pflag == 0) {

pflag = 1;

}

x++;

}

// System.out.println("--------->1.1");

if (pflag == 1) {

if ((x == xright)

&& image.getRGB(x, y) != Color.BLACK

.getRGB()

&& image.getRGB(x, y) != fillColor

.getRGB()) {

p = new Point(x, y);

v.add(0, p); // 新區間超過xRight,將代表該區段的象素進棧

} else {

p = new Point(x - 1, y);

v.add(0, p); /* 新區段右端點作為種子進棧 */

}

pflag = 0;

}

// System.out.println("--------->1.2");

xenter = x;

while ((image.getRGB(x, y) == Color.BLACK

.getRGB()

|| image.getRGB(x, y) == fillColor

.getRGB()) && (x < xright)) {

x++;/* 向右跳過分隔帶 */

}

// System.out.println("--------->1.3");

if (xenter == x) {

x++;/* 處理特殊情況,以退出while(x<=xRight)迴圈 */

}

}

// System.out.println("--------->2");

x = xleft;

y = y + 2;/* 檢查相鄰的下掃描線 */

while(x <= xright){

pflag = 0;

while (image.getRGB(x, y) != Color.BLACK

.getRGB()

&& image.getRGB(x, y) != fillColor

.getRGB() && x < xright) {

if (pflag == 0) {

pflag = 1;

}

x++;

}

// System.out.println("--------->2.1");

if (pflag == 1) {

if ((x == xright)

&& image.getRGB(x, y) != Color.BLACK

.getRGB()

&& image.getRGB(x, y) != fillColor

.getRGB()) {

p = new Point(x, y);

v.add(0, p); // 新區間超過xRight,將代表該區段的象素進棧

} else {

p = new Point(x - 1, y);

v.add(0, p); /* 新區段右端點作為種子進棧 */

}

pflag = 0;

}

// System.out.println("--------->2.2");

xenter = x;

while ((image.getRGB(x, y) == Color.BLACK

.getRGB()

|| image.getRGB(x, y) == fillColor

.getRGB()) && (x < xright)) {

x++;/* 向右跳過分隔帶 */

}

// System.out.println("--------->2.3");

if (xenter == x) {

x++;/* 處理特殊情況,以退出while(x<=xRight)迴圈 */

}

// System.out.println("--------->2.4");

}

g.drawImage(image, 0, 0, null);

// System.out.println("--------->3");

// System.out.println("v.size:" + v.size());

}

fill = false;

}

public int[] getXPoints() {

return xPoints;

}

public void setXPoints(int[] points) {

xPoints = points;

}

public int[] getYPoints() {

return yPoints;

}

public void setYPoints(int[] points) {

yPoints = points;

}

public Color getFillColor() {

return fillColor;

}

public void setFillColor(Color fillColor) {

this.fillColor = fillColor;

}

public boolean isFill() {

return fill;

}

public void setFill(boolean fill) {

this.fill = fill;

}

}