In what class are you putting the 'getPixelCount' method?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import java.net.*; /** * Extend this class to add your picture manipulation methods. */ public class Picture extends Actor { private String source; private int count; private Pixel[] allThePixels; /** * Default Constructor */ public Picture(){ load("WinterWonderLand.jpg"); this.allThePixels = getPixels(); } /** * Constructor that accepts a file image name so that we can * create additional images * * @param imageFileName represents the name of the new image * to create. */ public Picture(String imageFileName){ setImage(imageFileName); } /** * This method is called by the Greenfoot framework. You may want to override it * to automatically load a picture and carry out manipulations when the user clicks * on the act button in Greenfoot. */ public void act() { } /** * Gets a pixel for a specific <code>x</code> and <code>y</code> location * * @param x the x location of the pixel in the picture * @param y the y location of the pixel in the picture * * @return a Pixel object for the given location */ public Pixel getPixel(int x, int y) { return new Pixel(this, x, y); } /** * Gets the pixel at <code>index</code> position in the list * * @param index the position of the pixel in a list of pixels * * @return a Pixel object from the given position in the list */ public Pixel getPixel(int index) { int num = getWorld().getHeight() * getWorld().getWidth(); if (index < 0 || ( index >= num ) ) { String msg = index + " not between 0 and " + num + "\nThis means that the position " + index + " is not in the list - \ngenerally it means that " + "you have looped too far. \nCheck your condition"; throw new IllegalArgumentException(); } return allThePixels[index]; } /** * Gets all pixels in this picture * @return an array of Pixel objects, traversed by * row then by column. */ public Pixel[] getPixels() { int width = getImage().getWidth(); int height = getImage().getHeight(); Pixel[] pixels = new Pixel[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixels[y * width + x] = new Pixel(this, x, y); } } return pixels; } /** * Loads a picture from a given source. * @param source the image source. If the source starts * with http://, it is a URL, otherwise, a filename. */ public void load(String source) { try { if (source.startsWith("http://")) { BufferedImage img = ImageIO.read(new URL(source).openStream()); int width = img.getWidth(); int height = img.getHeight(); GreenfootImage gimg = new GreenfootImage(width, height); for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) gimg.setColorAt(x, y, new java.awt.Color(img.getRGB(x, y))); setImage(gimg); } else { setImage(new GreenfootImage(source)); World world = getWorld(); if (world != null) { world.repaint(); } } this.source = source; } catch (Exception ex) { this.source = null; ex.printStackTrace(); } } /** * Reloads this picture, undoing any manipulations. */ public void reload() { load(source); } /** * Displays a file chooser for picking a picture. */ public void pick() { JFileChooser chooser = new JFileChooser("."); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { load(chooser.getSelectedFile().getAbsolutePath()); } } /** * This method is called by the Greenfoot framework. */ public void repaint() { count++; if (count == 10 * getImage().getWidth()) { getWorld().repaint(); count = 0; } } }
import java.awt.*; import java.awt.image.*; /** */ public class Pixel { private Picture picture; private int x; private int y; /** * Constructs a pixel at a given location in a picture. * @param picture the picture containing the pixel * @param x the x-coordinate of the pixel * @param y the y-coordinate of the pixel */ public Pixel(Picture picture, int x, int y) { if (x < 0 || x >= picture.getImage().getWidth()){ throw new IllegalArgumentException("x value " + x + " is not in range 0.." + (picture.getImage().getWidth() - 1)); } if (y < 0 || y >= picture.getImage().getHeight()){ throw new IllegalArgumentException("y value " + y + " is not in range 0.." + (picture.getImage().getHeight() - 1)); } this.picture = picture; this.x = x; this.y = y; } /** * Gets the x location of this pixel. * @return the x location of this pixel */ public int getX() { return x; } /** * Gets the y location of this pixel. * @return the y location of this pixel */ public int getY() { return y; } /** * Gets the red value of this pixel. * @return the red value (0..255) */ public int getRed() { return picture.getImage().getColorAt(x, y).getRed(); } /** * Gets the green value of this pixel. * @return the green value (0..255) */ public int getGreen() { return picture.getImage().getColorAt(x, y).getGreen(); } /** * Gets the blue value of this pixel. * @return the blue value (0..255) */ public int getBlue() { return picture.getImage().getColorAt(x, y).getBlue(); } /** * Gets the Color value of this pixel. * @return the Color value */ public Color getColor(){ return new Color(picture.getImage().getColorAt(x, y)); } /** * Gets the Color value of this pixel. * @return the Color value */ public Color getColor(int xParam, int yParam){ return new Color(picture.getImage().getColorAt(xParam, yParam)); } /** * Sets the red value of this pixel. * @param value the red value (0..255) */ public void setRed(double value){ if (value < 0 || value > 255) { throw new IllegalArgumentException(value + " not between 0 and 255"); } java.awt.Color color = picture.getImage().getColorAt(x, y); picture.getImage().setColorAt(x, y, new Color((int) value, color.getGreen(), color.getBlue())); picture.repaint(); } /** * Sets the red value of this pixel. * @param value the red value (0..255) */ public void setGreen(double value){ if (value < 0 || value > 255) { throw new IllegalArgumentException(value + " not between 0 and 255"); } java.awt.Color color = picture.getImage().getColorAt(x, y); picture.getImage().setColorAt(x, y, new Color(color.getRed(), (int) value, color.getBlue())); picture.repaint(); } /** * Sets the red value of this pixel. * @param value the red value (0..255) */ public void setBlue(double value){ if (value < 0 || value > 255) { throw new IllegalArgumentException(value + " not between 0 and 255"); } java.awt.Color color = picture.getImage().getColorAt(x, y); picture.getImage().setColorAt(x, y, new Color(color.getRed(), color.getGreen(), (int) value)); picture.repaint(); } /** * Sets all the Color values for this pixel. * @param newRed the new Color value for red * @param newGreen the new Color value for green * @param newBlue the new Color value for blue */ public void setColor(double newRed, double newGreen, double newBlue) { picture.getImage().setColorAt(x, y, new Color(newRed, newGreen, newBlue)); picture.repaint(); } /** * Sets the Color value of this pixel. * @param value the Color value */ public void setColor(java.awt.Color value) { picture.getImage().setColorAt(x, y, value); picture.repaint(); } /** * Gets the distance between this pixel's color and another color. * @param other the other color * @return the distance between this pixel's color and the other color */ public double colorDistance(java.awt.Color other) { int dr = getRed() - other.getRed(); int dg = getGreen() - other.getGreen(); int db = getBlue() - other.getBlue(); return Math.sqrt(dr * dr + dg * dg + db * db); } /** * Gets the average of the color values of this pixel. * @return the average of the red, green, and blue values */ public double getAverage() { return (getRed() + getGreen() + getBlue()) / 3.0; } public String toString() { return "Pixel[x=" + x + ",y=" + y + ",red=" + getRed() + ",green=" + getGreen() + ",blue=" + getBlue() + "]" ; } }
import greenfoot.*; public class WinterWonderland extends Picture { private World theWorld; private Printer thePrinter; /** * This is the default constructor for the WinterWonderland class */ public WinterWonderland() { thePrinter = new Printer("Now showing:"); } public void act(){ theWorld.addObject(thePrinter, 300, 790); getPixelCount(); redDot(); increaseRed(); topLeftSquare(); Greenfoot.stop(); } /** * This method will return an integer that represents the number of pixels in the world. */ public void getPixelCount() { theWorld.getHeight(); theWorld.getWidth(); } /** * Run the project. This method creates a red dot five pixels wide by five pixels high * on the left hand side of image half way down. Look for it in the bricks. * I would suggest getting rid of all this repetition. It will make the other * methods easier to think about once you have all this in a loop. */ public void redDot(){ { int i=240000; while ( i<=240005) { Pixel aRandomPix = getPixel(i); aRandomPix.setColor(Color.RED); i++; } } // // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240001); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240002); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240003); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240004); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240005); // // aRandomPix.setColor(Color.RED); // // end first row of dot // // { int i=240600; while ( i<=240605) { Pixel aRandomPix = getPixel(i); aRandomPix.setColor(Color.RED); i++; } } // // aRandomPix = getPixel(240600); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240601); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240602); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240603); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240604); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(240605); // // aRandomPix.setColor(Color.RED); // // end second row of dot { int i=241200; while ( i<=241205) { Pixel aRandomPix = getPixel(i); aRandomPix.setColor(Color.RED); i++; } } // // // // aRandomPix = geixel(241200); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241201); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241202); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241203); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241204); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241205); // // aRandomPix.setColor(Color.RED); // // end third row of dot { int i=241800; while ( i<=241805) { Pixel aRandomPix = getPixel(i); aRandomPix.setColor(Color.RED); i++; } } // // // // aRandomPix = getPixel(241800); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241801); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241802); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241803); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241804); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(241805); // // aRandomPix.setColor(Color.RED); // // end forth row of dot { int i=242400; while ( i<=242405) { Pixel aRandomPix = getPixel(i); aRandomPix.setColor(Color.RED); i++; } } // // // // aRandomPix = getPixel(242400); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(242401); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(242402); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(242403); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(242404); // // aRandomPix.setColor(Color.RED); // // // // aRandomPix = getPixel(242405); // // aRandomPix.setColor(Color.RED); // // end fifth row of dot } /** * This method will increase the current red color value for all pixels by 30%. */ public void increaseRed() { int getPixel=5000; while ( getPixel<=7050) { Pixel myPix=getPixel(getPixel); myPix.setColor(Color.YELLOW); getPixel=getPixel+1; } } /** * This method will create a solid colored 200X200 square in the top left hand corner of the world. */ public void topLeftSquare() { { int x=0; int y=0; while(x<=200) { x++; while(y<=200) { Pixel sqPix=getPixel(x,y); sqPix.setColor(Color.YELLOW); y++; } } } } /** * This method will invert the colors of the given image area or space */ public void invertColors() { } /** * Method automatically called by the Greenfoot IDE when the WinterWonderland * object is added to the world - hence the name. * You will never have to call this method. It is used here * to initialize theWorld instance variable because I cannot get this object reference when the constructor * executes - the world does not exist for the WinterWonderland class at that point. */ public void addedToWorld(World world){ theWorld = world; } }
public int getPixelCount() { return theWorld.getWidth() * theWorld.getHeight(); }
/** * This method will return an integer that represents the number of pixels in the world. */ public int getPixelCount() { theWorld.getHeight(); theWorld.getWidth(); return theWorld.getHeight()*theWorld.getWidth(); }
for (int y=0; y<200; y++) for (int x=0; x<200; x++) { Pixel pixel = new Pixel(this, x, y); // alter pixel using get and set methods in the Pixel class }