Well, after an all-day battle, I canβt find the right answer. Basically, I have a very simple setup that fills my BufferedImage canvas on a white background. Then I draw a polygon from the points coming from the args array. This seems to work just fine. The problem occurs when I want to count the number of pixels that Polygon fills (which fills).
To do this, I looped around the canvas and compared each pixel using the getRGB method, and if it was not white (background color), I increased the counter. However, the result that I always get is the canvas dimension (640 * 480), which means that the entire canvas is white.
So, I assume that Polygon, which refers to the screen, is not added to the canvas and floats on top? This is the only answer I can come up with, but it can be completely wrong.
I would like to be able to count the number of pixels that are not white. Any suggestions?
the code:
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);
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();
}
The "findPoints (args)" method is too long to publish, but basically everything it does takes argument values ββand compiles a list of points.
Thanks in advance Boots