我刚刚安装了Netbeans IDE 7.0并试图运行我的项目(当我使用命令提示符的传统方法运行时执行正常)基于图像处理但applet查看器说start: applet not initialized
而编译(build
在Netbeans中的任务)是成功的。
这里我附加了2个相关的Java文件,而整个项目还有3-4个java文件用于图像渲染。
/*
* <applet code=ImageFilterDemo width=350 height=450>
* <param name=img value=strawberry.jpeg>
* <param name=filters value="Grayscale+Invert+Contrast+Blur+Sharpen">
* </applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.* ;
public class ImageFilterDemo extends Applet implements ActionListener {
Image img;
PlugInFilter pif;
Image fimg;
Image curImg;
LoadedImage lim;
Label lab;
Button reset;
Button histograph;
Dimension d;
static int iw, ih;
int pixels[];
static int w, h,just;
static int hist[] = new int[256];
static int max_hist = 0;
public void init() {
setLayout(new BorderLayout());
Panel p = new Panel();
add(p, BorderLayout.SOUTH);
reset = new Button("Reset");
histograph = new Button("Histograph");
reset.addActionListener(this);
histograph.addActionListener(this);
p.add(reset);
p.add(histograph);
StringTokenizer st = new StringTokenizer(getParameter("filters"), "+");
while(st.hasMoreTokens()) {
Button b = new Button(st.nextToken());
b.addActionListener(this);
p.add(b);
}
lab = new Label("");
add(lab, BorderLayout.NORTH);
img = getImage(getDocumentBase(),"strawberry.jpeg");
lim = new LoadedImage(img);
add(lim, BorderLayout.CENTER);
d = getSize();
w = d.width;
h = d.height;
try {
//img = getImage(getDocumentBase(), getParameter("img"));
MediaTracker t = new MediaTracker(this);
t.addImage(img, 0);
t.waitForID(0);
iw = img.getWidth(null);
ih = img.getHeight(null);
pixels = new int[iw * ih];
PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih,
pixels, 0, iw);
pg.grabPixels();
} catch (InterruptedException e) { };
for (int i=0; i<iw*ih; i++) {
int pii = pixels[i];
int r = 0xff & (pii >> 16);
int g = 0xff & (pii >> 8);
int b = 0xff & (pii);
int y = (int) (.33 * r + .56 * g + .11 * b);
hist[y]++;
}
for (int i=0; i<256; i++) {
if (hist[i] > max_hist)
max_hist = hist[i];
}
}
public void actionPerformed(ActionEvent ae) {
String a = "";
try {
a = (String)ae.getActionCommand();
if (a.equals("Reset")) {
lim.set(img);
lab.setText("Normal");
}
else if(a.equals("Histograph")){
lab.setText("Histo_graph");
just=1;
lim.repaint();
}
else {
pif = (PlugInFilter) Class.forName(a).newInstance();
fimg = pif.filter(this, img);
lim.set(fimg);
lab.setText("Filtered: " + a);
}
repaint();
} catch (ClassNotFoundException e) {
lab.setText(a + " not found");
lim.set(img);
repaint();
} catch (InstantiationException e) {
lab.setText("could't new " + a);
} catch (IllegalAccessException e) {
lab.setText("no access: " + a);
}
}
}
// 2nd file
import java.awt.*;
public class LoadedImage extends Canvas {
Image img;
public LoadedImage(Image i) {
set(i);
}
void set(Image i) {
MediaTracker mt = new MediaTracker(this);
mt.addImage(i, 0);
try {
mt.waitForAll();
} catch (InterruptedException e) { };
img = i;
repaint();
}
public void paint(Graphics g) {
if (img == null) {
g.drawString("no image", 10, 30);
}
else if(ImageFilterDemo.just==1){
int x = (ImageFilterDemo.w - 256) / 2;
int lasty = ImageFilterDemo.h - ImageFilterDemo.h * ImageFilterDemo.hist[0] / ImageFilterDemo.max_hist;
for (int i=0; i<256; i++, x++) {
int y = ImageFilterDemo.h - ImageFilterDemo.h * ImageFilterDemo.hist[i] / ImageFilterDemo.max_hist;
g.setColor(new Color(i, i, i));
g.fillRect(x, y, 1, ImageFilterDemo.h);
g.setColor(Color.red);
g.drawLine(x-1,lasty,x,y);
lasty = y;
}
ImageFilterDemo.just=0;
}
else {
g.drawImage(img, 0, 0, this);
}
}
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(this), img.getHeight(this));
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
当我们从传统的命令提示符更改为Netbeans时,我们在设置或编码方面是否需要进行任何更改?
答案 0 :(得分:1)
我将在NetBeans中创建一个新项目,并在默认情况下查看它如何与Applet一起使用。
如果您将Applet添加到项目中(新文件 - &gt; Swing GUI表单 - &gt; JApplet表单)
查看为您生成的init代码
@Override
public void init() {
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
我认为您的代码需要这样的块才能在NetBeans中执行“文件运行”或“项目运行”
答案 1 :(得分:0)
如果我没记错的话,你必须在java Web中运行你的applet。
Properties -> Run -> Run with Java Web Start.
答案 2 :(得分:0)
希望这个小教程有帮助,http://netbeans.org/kb/docs/web/applets.html