2017-03-19 10 views
-3

私は色で塗りつぶしたい点のリストを閉じた輪郭を作成しました。境界塗りつぶし再帰アルゴリズムを使用しましたが、配列インデックスは境界線から外れます閉じた輪郭の内側の色と輪郭の外側の色が同じであるため、私はif条件を作成できません。特定の色で塗りつぶしたい輪郭を得るためにはどのメソッドを使用するべきですか。これは私が試したコードです私はJavaで等高線を塗りたい

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

ここ

my image: rectangle

イメージです車輪の再発明なぜここ

は、メソッド呼び出し

BoundaryFillAlgorithm.BoundaryFill(BoundaryFillAlgorithm.seedx,BoundaryFillAlgorithm.seedy); 

答えて

0

最後HERESに修正コード自分のコードを修正:

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

} 

}

0
g.setColor(Color.red); // Set color to red 
g.fillRect(600, 400, 100, 100);// a filled-in RED rectangle 
0

ですか?
Graphics2Dには既に方法fill(Shape)があります。 Shapeインターフェイスを実装する多くのクラス、特にPolygon, を再利用する可能性があります。

+0

かについてとABD一緒に融合矩形が矩形の一辺が三角形の辺であることを意味する三角形のような不規則な形状を –

+0

@NizamAhmed私は[Path](https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.html)と[GeneralPath](https: //docs.oracle.com/javase/7/docs/api/java/awt/geom/GeneralPath.html)、いくつかのサブパスで構成されるシェイプを構築できます。 –

関連する問題