我想在 java 中填充轮廓
I want to fill a contour in java
我创建了一个封闭的等高线,其中有一个点列表,我想用 color.I 填充边界填充递归算法,但不幸的是数组索引超出了范围,因为我无法开发如果条件是因为闭合轮廓内的颜色和轮廓外的颜色是 same.What 方法,我应该使用该方法来获得要由特定 color.Here 填充的所需轮廓是我尝试过的代码
public class BoundaryFillAlgorithm {
public static BufferedImage toFill = MemoryPanel.Crect;
static Graphics g1 = toFill.getGraphics();
static int seedx = toFill.getWidth()/2;
static int seedy = toFill.getHeight()/2;
public static void BoundaryFill(int x,int y){
Color old = new Color(toFill.getRGB(x, y));
g1.setColor(Color.BLACK);
if(old!=Color.BLACK){
g1.fillOval(x, y, 1, 1);
BoundaryFill(x+1,y);
BoundaryFill(x,y+1);
BoundaryFill(x-1,y);
BoundaryFill(x,y-1);
}
}
这是图片
方法调用如下
BoundaryFillAlgorithm.BoundaryFill(BoundaryFillAlgorithm.seedx,BoundaryFillAlgorithm.seedy);
g.setColor(Color.red); // Set color to red
g.fillRect(600, 400, 100, 100);// a filled-in RED rectangle
为什么要重新发明轮子?
Graphics2D
已有一个方法 fill(Shape)。
有许多 类 实施 Shape interface, especially Polygon,
您可以重复使用。
终于更正了我的代码这里是更正后的代码:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class BoundaryFillAlgorithm {
public static BufferedImage toFill = MemoryPanel.Crect;
Graphics g1 = toFill.getGraphics();
public BoundaryFillAlgorithm(BufferedImage toFill){
int x = toFill.getWidth()/2-10;
int y = toFill.getHeight()/2;
int old = toFill.getRGB(x, y);
this.toFill = toFill;
fill(x,y,old);
}
private void fill(int x,int y,int old) {
if(x<=0) return;
if(y<=0) return;
if(x>=toFill.getWidth()) return;
if(y>=toFill.getHeight())return;
if(toFill.getRGB(x, y)!=old)return;
toFill.setRGB(x, y, 0xFFFF0000);
fill(x+1,y,old);
fill(x,y+1,old);
fill(x-1,y,old);
fill(x,y-1,old);
fill(x+1,y-1,old);
fill(x+1,y+1,old);
fill(x-1,y-1,old);
fill(x+1,y-1,old);
}
}
我创建了一个封闭的等高线,其中有一个点列表,我想用 color.I 填充边界填充递归算法,但不幸的是数组索引超出了范围,因为我无法开发如果条件是因为闭合轮廓内的颜色和轮廓外的颜色是 same.What 方法,我应该使用该方法来获得要由特定 color.Here 填充的所需轮廓是我尝试过的代码
public class BoundaryFillAlgorithm {
public static BufferedImage toFill = MemoryPanel.Crect;
static Graphics g1 = toFill.getGraphics();
static int seedx = toFill.getWidth()/2;
static int seedy = toFill.getHeight()/2;
public static void BoundaryFill(int x,int y){
Color old = new Color(toFill.getRGB(x, y));
g1.setColor(Color.BLACK);
if(old!=Color.BLACK){
g1.fillOval(x, y, 1, 1);
BoundaryFill(x+1,y);
BoundaryFill(x,y+1);
BoundaryFill(x-1,y);
BoundaryFill(x,y-1);
}
}
这是图片
方法调用如下
BoundaryFillAlgorithm.BoundaryFill(BoundaryFillAlgorithm.seedx,BoundaryFillAlgorithm.seedy);
g.setColor(Color.red); // Set color to red
g.fillRect(600, 400, 100, 100);// a filled-in RED rectangle
为什么要重新发明轮子?
Graphics2D
已有一个方法 fill(Shape)。
有许多 类 实施 Shape interface, especially Polygon,
您可以重复使用。
终于更正了我的代码这里是更正后的代码:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class BoundaryFillAlgorithm {
public static BufferedImage toFill = MemoryPanel.Crect;
Graphics g1 = toFill.getGraphics();
public BoundaryFillAlgorithm(BufferedImage toFill){
int x = toFill.getWidth()/2-10;
int y = toFill.getHeight()/2;
int old = toFill.getRGB(x, y);
this.toFill = toFill;
fill(x,y,old);
}
private void fill(int x,int y,int old) {
if(x<=0) return;
if(y<=0) return;
if(x>=toFill.getWidth()) return;
if(y>=toFill.getHeight())return;
if(toFill.getRGB(x, y)!=old)return;
toFill.setRGB(x, y, 0xFFFF0000);
fill(x+1,y,old);
fill(x,y+1,old);
fill(x-1,y,old);
fill(x,y-1,old);
fill(x+1,y-1,old);
fill(x+1,y+1,old);
fill(x-1,y-1,old);
fill(x+1,y-1,old);
}
}