Applet can't run at browser - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Applet can't run at browser

wian123wian123 Posts: 2Member
Hi all... i have prob about my applet..
in netbeans its work nicely.. but when i use it at html, it didnt work..

this is my coding..


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.File;
import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.*;




/**
*
* @author Wian
*/
public class pcd extends JApplet implements ActionListener{
private ImagePanel panel,panel2;
private Button browse;
static boolean fInBrowser = true;
private JMenuItem openItem, grayItem;
private JMenuItem saveItem;
private JMenuItem exitItem;
private JMenuItem blurItem;
private JMenuItem sharpenItem;
private JMenuItem brightenItem;
private JMenuItem edgeDetectItem;
private JMenuItem negativeItem;
private JMenuItem rotateItem;
private JMenuItem aboutItem;
private JMenuItem scaleItem;
Choice listfilter;

/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
panel = new ImagePanel();
panel2 = new ImagePanel();

JMenu fileMenu = new JMenu("File");

openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);

saveItem = new JMenuItem("Save");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);

JMenu editMenu = new JMenu("Filter");
grayItem = new JMenuItem("Grayscale");
grayItem.addActionListener(this);
editMenu.add(grayItem);

blurItem = new JMenuItem("Blur");
blurItem.addActionListener(this);
editMenu.add(blurItem);

sharpenItem = new JMenuItem("Sharpen");
sharpenItem.addActionListener(this);
editMenu.add(sharpenItem);

brightenItem = new JMenuItem("Brighten");
brightenItem.addActionListener(this);
editMenu.add(brightenItem);

edgeDetectItem = new JMenuItem("Edge detect");
edgeDetectItem.addActionListener(this);
editMenu.add(edgeDetectItem);

negativeItem = new JMenuItem("Negative");
negativeItem.addActionListener(this);
editMenu.add(negativeItem);

rotateItem = new JMenuItem("Rotate");
rotateItem.addActionListener(this);
editMenu.add(rotateItem);

scaleItem = new JMenuItem("Scale");
scaleItem.addActionListener(this);
editMenu.add(scaleItem);
JMenu help = new JMenu("Help");
aboutItem = new JMenuItem("About");
aboutItem.addActionListener(this);
help.add(aboutItem);

JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(help);
setJMenuBar(menuBar);
setLayout(new GridLayout(1,2));
add(panel2);
add(panel);
// TODO start asynchronous download of heavy resources
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith(".gif") || name.endsWith(".jpg") ||
name.endsWith(".jpeg") || name.endsWith(".bmp") ||
name.endsWith(".png") || name.endsWith(".tiff") ||
f.isDirectory();
}

public String getDescription() {
return "*.gif, *.jpg, *.jpeg, *.bmp, *.png, *.tiff";
}
});

int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
String name =chooser.getSelectedFile().getAbsolutePath();
panel.loadImage("name");
panel2.loadImage("name");

}
} else if (source == saveItem) {
panel.saveImage();
} else if (source == exitItem) {
System.exit(0);
} else if (source == blurItem) {
panel.blur();
} else if (source == sharpenItem) {
panel.sharpen();
} else if (source == brightenItem) {
panel.brighten();
} else if (source == edgeDetectItem) {
panel.edgeDetect();
} else if (source == negativeItem) {
panel.negative();
} else if (source == rotateItem) {
panel.rotate();
} else if (source == aboutItem){
JOptionPane.showMessageDialog(this,
"Easy Java Image Processing (eJIP)",
JOptionPane.INFORMATION_MESSAGE);
} else if (source == scaleItem){
panel.scale();
} else if (source == grayItem){
panel.gray();
}
}

// TODO overwrite start(), stop() and destroy() methods

}
class ImagePanel2 extends JPanel {
private BufferedImage image;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}

public void loadImage(String name) {
Image loadedImage = Toolkit.getDefaultToolkit().getImage(name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(loadedImage, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
image = new BufferedImage(loadedImage.getWidth(null), loadedImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(loadedImage, 0, 0, null);

repaint();
}

public void saveImage() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

int r = chooser.showSaveDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
File sf = chooser.getSelectedFile();
try {
ImageIO.write(image, "png", sf);
} catch (Exception ex) {
System.err.println(ex);
}
}

}

private void filter(BufferedImageOp op) {
BufferedImage filteredImage = new BufferedImage(image.getWidth(),
image.getHeight(), image.getType());
op.filter(image, filteredImage);
image = filteredImage;
repaint();
}

private void convolve(float[] elements) {
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
filter(op);
}

public void blur() {
float weight = 1.0f / 9.0f;
float[] elements = new float[9];
for (int i = 0; i < 9; i++) {
elements[i] = weight;
}
convolve(elements);
}

public void sharpen() {
float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f,
0.0f};
convolve(elements);
}

void edgeDetect() {
float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f,
0.0f};
convolve(elements);
}

public void brighten() {
float a = 1.5f;
float b = -20.0f;
RescaleOp op = new RescaleOp(a, b, null);
filter(op);
}

void negative() {
byte negative[] = new byte[256];
for (int i = 0; i < 256; i++) {
negative[i] = (byte) (255 - i);
}
ByteLookupTable table = new ByteLookupTable(0, negative);
LookupOp op = new LookupOp(table, null);
filter(op);
}

void rotate() {
double angle=0;
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(angle),
image.getWidth()/2, image.getHeight()/2);
AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BILINEAR);
filter(op);
}
void scale() {

AffineTransform transform = AffineTransform.getScaleInstance(2, 2);
AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BILINEAR);
filter(op);
}
void gray(){
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
filter(op);
}

}

Comments

  • silveredge52silveredge52 Posts: 198Member
    Hey,
    Didn't try in browser, but got exception opening a picture file. Believe problem with code is in actionPerformed method in the method call that passes a String literal instead of the intended variable "[color=Blue]name[/color]".
    i.e.
    [code]panel.loadImage(name); // ERROR FIX: change from string constant
    panel2.loadImage(name);// ERROR FIX: change from string constant[/code]
    regards, se52
  • wian123wian123 Posts: 2Member
    import java.awt.*;
    import java.awt.color.ColorSpace;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import java.awt.image.*;
    import java.io.File;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;




    /**
    *
    * @author Wian
    */
    public class NewJApplet extends JApplet implements ActionListener{
    private ImagePanel panel,panel2;

    static boolean fInBrowser = true;
    private JMenuItem openItem, grayItem;
    private JMenuItem saveItem;
    private JMenuItem exitItem;
    private JMenuItem blurItem;
    private JMenuItem sharpenItem;
    private JMenuItem brightenItem;
    private JMenuItem edgeDetectItem;
    private JMenuItem negativeItem;
    private JMenuItem rotateItem;
    private JMenuItem aboutItem;
    private JMenuItem scaleItem;
    Choice listfilter;

    /**
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    */
    public void init() {
    panel = new ImagePanel();
    panel2 = new ImagePanel();

    JMenu fileMenu = new JMenu("File");

    openItem = new JMenuItem("Open");
    openItem.addActionListener(this);
    fileMenu.add(openItem);

    saveItem = new JMenuItem("Save");
    saveItem.addActionListener(this);
    fileMenu.add(saveItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    fileMenu.add(exitItem);

    JMenu editMenu = new JMenu("Filter");
    grayItem = new JMenuItem("Grayscale");
    grayItem.addActionListener(this);
    editMenu.add(grayItem);

    blurItem = new JMenuItem("Blur");
    blurItem.addActionListener(this);
    editMenu.add(blurItem);

    sharpenItem = new JMenuItem("Sharpen");
    sharpenItem.addActionListener(this);
    editMenu.add(sharpenItem);

    brightenItem = new JMenuItem("Brighten");
    brightenItem.addActionListener(this);
    editMenu.add(brightenItem);

    edgeDetectItem = new JMenuItem("Edge detect");
    edgeDetectItem.addActionListener(this);
    editMenu.add(edgeDetectItem);

    negativeItem = new JMenuItem("Negative");
    negativeItem.addActionListener(this);
    editMenu.add(negativeItem);

    rotateItem = new JMenuItem("Rotate");
    rotateItem.addActionListener(this);
    editMenu.add(rotateItem);

    scaleItem = new JMenuItem("Scale");
    scaleItem.addActionListener(this);
    editMenu.add(scaleItem);
    JMenu help = new JMenu("Help");
    aboutItem = new JMenuItem("About");
    aboutItem.addActionListener(this);
    help.add(aboutItem);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(help);
    setJMenuBar(menuBar);
    setLayout(new GridLayout(1,2));
    add(panel2);
    add(panel);
    // TODO start asynchronous download of heavy resources
    }

    public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == openItem) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

    public boolean accept(File f) {
    String name = f.getName().toLowerCase();
    return name.endsWith(".gif") || name.endsWith(".jpg") ||
    name.endsWith(".jpeg") || name.endsWith(".bmp") ||
    name.endsWith(".png") || name.endsWith(".tiff") ||
    f.isDirectory();
    }

    public String getDescription() {
    return "*.gif, *.jpg, *.jpeg, *.bmp, *.png, *.tiff";
    }
    });

    int r = chooser.showOpenDialog(this);
    if (r == JFileChooser.APPROVE_OPTION) {
    String name =chooser.getSelectedFile().getAbsolutePath();
    panel.loadImage("name");
    panel2.loadImage("name");

    }
    } else if (source == saveItem) {
    panel.saveImage();
    } else if (source == exitItem) {
    System.exit(0);
    } else if (source == blurItem) {
    panel.blur();
    } else if (source == sharpenItem) {
    panel.sharpen();
    } else if (source == brightenItem) {
    panel.brighten();
    } else if (source == edgeDetectItem) {
    panel.edgeDetect();
    } else if (source == negativeItem) {
    panel.negative();
    } else if (source == rotateItem) {
    panel.rotate();
    } else if (source == aboutItem){
    JOptionPane.showMessageDialog(rootPane, "Easy Java Image Processing");
    } else if (source == scaleItem){
    panel.scale();
    } else if (source == grayItem){
    panel.gray();
    }
    }

    // TODO overwrite start(), stop() and destroy() methods

    }
    class ImagePanel extends JPanel {
    private BufferedImage image;
    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
    }

    public void loadImage(String name) {
    Image loadedImage = Toolkit.getDefaultToolkit().getImage(name);
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(loadedImage, 0);
    try {
    tracker.waitForID(0);
    } catch (InterruptedException e) {
    }
    image = new BufferedImage(loadedImage.getWidth(null), loadedImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.drawImage(loadedImage, 0, 0, null);

    repaint();
    }

    public void saveImage() {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    int r = chooser.showSaveDialog(this);
    if (r == JFileChooser.APPROVE_OPTION) {
    File sf = chooser.getSelectedFile();
    try {
    ImageIO.write(image, "png", sf);
    } catch (Exception ex) {
    System.err.println(ex);
    }
    }

    }

    private void filter(BufferedImageOp op) {
    BufferedImage filteredImage = new BufferedImage(image.getWidth(),
    image.getHeight(), image.getType());
    op.filter(image, filteredImage);
    image = filteredImage;
    repaint();
    }

    private void convolve(float[] elements) {
    Kernel kernel = new Kernel(3, 3, elements);
    ConvolveOp op = new ConvolveOp(kernel);
    filter(op);
    }

    public void blur() {
    float weight = 1.0f / 9.0f;
    float[] elements = new float[9];
    for (int i = 0; i < 9; i++) {
    elements[i] = weight;
    }
    convolve(elements);
    }

    public void sharpen() {
    float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f,
    0.0f};
    convolve(elements);
    }

    void edgeDetect() {
    float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f,
    0.0f};
    convolve(elements);
    }

    public void brighten() {
    float a = 1.5f;
    float b = -20.0f;
    RescaleOp op = new RescaleOp(a, b, null);
    filter(op);
    }

    void negative() {
    byte negative[] = new byte[256];
    for (int i = 0; i < 256; i++) {
    negative[i] = (byte) (255 - i);
    }
    ByteLookupTable table = new ByteLookupTable(0, negative);
    LookupOp op = new LookupOp(table, null);
    filter(op);
    }

    void rotate() {
    double angle=0;
    AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(angle),
    image.getWidth()/2, image.getHeight()/2);
    AffineTransformOp op = new AffineTransformOp(transform,
    AffineTransformOp.TYPE_BILINEAR);
    filter(op);
    }
    void scale() {

    AffineTransform transform = AffineTransform.getScaleInstance(2, 2);
    AffineTransformOp op = new AffineTransformOp(transform,
    AffineTransformOp.TYPE_BILINEAR);
    filter(op);
    }
    void gray(){
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);
    filter(op);
    }

    }




    May be u can try this code once more.. coz at here thats no prob..
    name is from location image..
  • silveredge52silveredge52 Posts: 198Member
    Hey,
    Without code change mentioned above running in NetBeans gives me the following error when I Open a xxx.gif file:
    [color=Red]Exception in thread "AWT-EventQueue-1" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:312)
    at Test.ImagePanel.loadImage(NewJApplet.java:175)
    at Test.NewJApplet.actionPerformed(NewJApplet.java:125)[/color] in the "output" window, and the "applet" window is empty.

    After change it works, see attachment.

    What problem are you having?
Sign In or Register to comment.