Reputation: 39
I have just installed Netbeans IDE 7.0 and tried to run my project (which executes fine when I run with traditional method using command prompt) based on image processing but applet viewer says start: applet not initialized
whereas compiling (build
task in Netbeans) is successful.
Here I am attaching 2 relevant Java files while whole project has 3-4 more java files for image rendering.
/*
* <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();
}
}
Is there any changes we have to make in settings or coding when we change from running from traditional command prompt, to Netbeans?
Upvotes: 0
Views: 193
Reputation: 31
Hope this little tutorial helps, http://netbeans.org/kb/docs/web/applets.html
Upvotes: 0
Reputation: 3304
I would create a new project in NetBeans and have a look at how it works with Applets by default.
If you add an Applet to your project (new File -> Swing GUI forms -> JApplet Form)
have a look at the init code that is generated for you
@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();
}
}
I would think that your code would need a block like this to able to do a 'File Run' or 'Project Run' in NetBeans
Upvotes: 1
Reputation: 1555
You have to run your applet in javaWeb if i remember correctly.
Properties -> Run -> Run with Java Web Start.
Upvotes: 0