わかりました、午後中ずっと戦った後、正しい答えが得られないようです。基本的に、キャンバス、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)' は長すぎて投稿できませんが、基本的には、引数値を取得してポイントのリストをコンパイルするだけです。
前もってありがとう、ブーツ