3

わかりました、午後中ずっと戦った後、正しい答えが得られないようです。基本的に、キャンバス、BufferedImage を白い背景で塗りつぶす非常に単純なセットアップがあります。次に、args 配列からのポイントから Polygon を描画します。表示に関しては、これは完全に機能します。Polygon (塗りつぶされている) が使用するピクセル数をカウントしたいときに問題が発生します。

これを行うために、キャンバスをループし、getRGB メソッドを使用して各ピクセルを比較し、白 (背景色) でない場合は、カウンターをインクリメントしました。ただし、常に得られる結果はキャンバスのサイズ (640*480) で、これはキャンバス全体が白であることを意味します。

だから私は、画面に描画されるポリゴンがキャンバスに追加されておらず、上に浮かんでいると仮定していますか? それが私が思いつく唯一の答えですが、完全に間違っている可能性があります。

私が望むのは、白ではないピクセルの数を数えられるようにすることです。助言がありますか?

コード:

public class Application extends JPanel {

public static int[] xCoord = null;
public static int[] yCoord = null;
private static int xRef = 250;
private static int yRef = 250;
private static int xOld = 0;
private static int yOld = 0;
private static BufferedImage canvas;
private static Polygon poly;

public Application(int width, int height) {
    canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    fillCanvas(Color.WHITE);
    poly = new Polygon(xCoord, yCoord, xCoord.length);     

    //Loop through each pixel
    int count = 0;
    for (int i = 0; i < canvas.getWidth(); i++)
        for (int j = 0; j < canvas.getHeight(); j++) {
            Color c = new Color(canvas.getRGB(i, j));
            if (c.equals(Color.WHITE))
                count++;
        }
    System.out.println(count);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(canvas, null, null);
    g2.fillPolygon(poly);

}

public void fillCanvas(Color c) {
    int color = c.getRGB();
    for (int x = 0; x < canvas.getWidth(); x++) {
        for (int y = 0; y < canvas.getHeight(); y++) {
            canvas.setRGB(x, y, color);
        }
    }
    repaint();
}


public static void main(String[] args) {       
    if (args != null)
        findPoints(args);

    JFrame window = new JFrame("Draw");
    Application panel = new Application(640, 480);

    window.add(panel);
    Dimension SIZE = new Dimension(640, 480);
    window.setPreferredSize(SIZE);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.pack();    
}//end main

メソッド 'findPoints(args)' は長すぎて投稿できませんが、基本的には、引数値を取得してポイントのリストをコンパイルするだけです。

前もってありがとう、ブーツ

4

2 に答える 2

1

感嘆符を追加して、条件内のブール値を反転するだけです。

if (!c.equals(Color.WHITE))

最初に Color オブジェクトを作成する代わりに、rgb 値を確認する方が高速です。

if ((rgb & 0xFFFFFF) != 0xFFFFFF)

BufferedImage を作成し、ポリゴンを描画してカウントします。基本的に、これは:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g = img.createGraphics();
g.fill(polygon);
g.dispose();
countThePixels(img);
于 2012-04-11T15:28:06.263 に答える
0

キャンバスを作成/塗りつぶすときは、BufferedImageオブジェクトを操作しています。ポリゴンがオブジェクトにキャプチャされPolygonます。画面への実際のレンダリングは、Graphicsオブジェクトを操作して行われます。つまり、ある意味では、ポリゴンは「浮いている」のです。つまり、キャンバスには表示されませんが、Graphics オブジェクトがレンダリングされるときにその上にペイントされます。

キャンバス自体にポリゴンを実際にレンダリングするか、Graphics オブジェクトからピクセルを取得して計算を実行する必要があります。

于 2012-04-11T15:32:54.430 に答える