午後全体と戦った後、正しい答えが得られないようです。 基本的には、私のキャンバスであるBufferedImageを白い背景で塗りつぶす非常に簡単な設定があります。次に私のargs配列から来る点からポリゴンを描画します。ディスプレイ上で、これは完全に動作します。この問題はPolygon(塗りつぶされている)のピクセル数を数えたいときに発生します。Javaでのピクセル数
これを行うには、キャンバスをループして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(引数)は、」投稿するにはあまりにも長いですが、基本的にすべてのそれはそれは引数の値をとり、点のリストをコンパイルありません。事前に
おかげで、 ブーツ
であなたの計算を実行します。多角形が塗りつぶされているので、 'getBounds()'メソッドを使ってポリゴンが占めるキャンバスの大きさと、ピクセル単位のサイズを得るための減算を見ることができます。 –