我有我的主;
package lab8_9;
import se.lth.cs.ptdc.images.ImageFilter;
import se.lth.cs.ptdc.images.ImageGUI;
public class ImageProcessor {
public static void main(String[] args) {
ImageFilter[] filters = new ImageFilter[2];
filters[0] = new IdentityFilter("Identity Filter");
filters[1] = new BlueFilter("Blue Filter");
new ImageGUI(filters);
}
}
然后我有我的班级;
package lab8_9;
import java.awt.Color;
import se.lth.cs.ptdc.images.ImageFilter;
public class IdentityFilter extends ImageFilter {
public IdentityFilter(String name) {
super(name);
}
public Color[][] apply(Color[][] inPixels, double paramValue) {
int height = inPixels.length;
int width = inPixels[0].length;
Color[][] outPixels = new Color[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color pixel = inPixels[i][j];
outPixels[i][j] = new Color(pixel.getRed(),
pixel.getGreen(),
pixel.getBlue());
}
}
return outPixels;
}
}
两者都运行正常,但后来我尝试编写自己的类;
package lab8_9;
import java.awt.Color;
import java.awt.image.ImageFilter;
public class BlueFilter extends ImageFilter {
public BlueFilter(String name) {
super(name);
}
public Color[][] apply(Color[][] inPixels, double paramValue) {
int height = inPixels.length;
int width = inPixels[0].length;
Color[][] outPixels = new Color[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color pixel = inPixels[i][j];
outPixels[i][j] = new Color(0,
0,
pixel.getBlue());
}
}
return outPixels;
}
}
我收到两个错误,一个是我的主要错误;
filters[1] = new BlueFilter("Blue Filter");
“类型不匹配:无法从BlueFilter转换为ImageFilter”
和一个我正在尝试添加的类,在构造函数中;
public BlueFilter(String name) {
super(name);
}
“构造函数ImageFilter(String)未定义”
我迷失在这里,有什么问题?我几乎复制了IdentityFilter,它运行良好,但它不起作用。任何帮助是极大的赞赏。我是初学者,所以请不要解释我做错了什么。
如果有人需要,这是超类,我还没写过;
package se.lth.cs.ptdc.images;
import java.awt.Color;
public abstract class ImageFilter {
private String name;
protected ImageFilter(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract Color[][] apply(Color[][] inPixels, double paramValue);
protected short[][] computeIntensity(Color[][] pixels) {
int height = pixels.length;
int width = pixels[0].length;
short[][] intensity = new short[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color c = pixels[i][j];
intensity[i][j] = (short) ((c.getRed() + c.getGreen() + c
.getBlue()) / 3);
}
}
return intensity;
}
protected short convolve(short[][] p, int i, int j, short[][] kernel,
int weight) {
short sum = 0;
for (int ii = -1; ii <= 1; ii++) {
for (int jj = -1; jj <= 1; jj++) {
sum += p[i + ii][j + jj] * kernel[ii + 1][jj + 1];
}
}
return (short) Math.round((double) sum / weight);
}
}
答案 0 :(得分:5)
您要导入java.awt.image.ImageFilter;
而不是se.lth.cs.ptdc.images.ImageFilter;
。