[Bluej-discuss] Chapter 11 - Imageviewer Smooth Filter
Mark Wallsgrove
mark at simplycompute.co.uk
Thu Jul 19 20:51:32 BST 2007
Afternoon everyone,
I am just reading through chapter 11 on page 343 and I have become stuck on
a task. The idea is to create a smooth filter for an image. My filter
doesn't smooth lol.. I have programmed what they asked for, get the average
of 9 pixels and set the middle pixel to that average. This creates a very
distasteful image.
Here is my code for the filter:
[CODE]
import java.awt.Color;
import java.util.ArrayList;
/**
* Write a description of class SmoothFilter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SmoothFilter extends Filter
{
/**
* Constructor for objects of class SmoothFilter
*/
public SmoothFilter(String name)
{
super(name);
}
/**
* Apply this filter to an image
* @param image The image to apply the filter to
*/
public OFImage apply(OFImage image){
//Create new image
OFImage newImage = new OFImage(image);
//Get height and width
int w = image.getWidth();
int h = image.getHeight();
//Loops
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
newImage.setPixel(x, y, getNewPixel(image, x, y));
}
}
return newImage;
}
/**
* return the modified pixel
* @param image Image to take a sample from
* @return the new pixel
*/
private Color getNewPixel(OFImage image, int x, int y){
//Create new list
int averagePix = 0;
int counter = 0;
for(int cy = 0; cy < 3; cy++){
for(int cx = 0; cx < 3; cx++){
if(((x-1)+cx > -1) && ((x-1)+cx < image.getWidth())){
if(((y-1)+cy > -1) && ((y-1)+cy < image.getHeight())){
averagePix +=
image.getPixel(x-1+cx,y-1+cy).getRGB();
counter++;
//System.out.println("Coords: "+ (y-1+cy) +" :: y=
"+ y +" cy= "+ cy +" \n "+ (x-1+cx));
//System.exit(0);
}
}
}
}
return (new Color(averagePix / counter));
}
}
[/CODE]
Could anyone else give me a clue for what I am meant to be programming..
ie.. whats the sudo code. I think I might have got the wrong idea. The txt
for that question isn't worded correctly I don't think.
Thanks for any help given..
All the best
Mark
More information about the bluej-discuss
mailing list