Hello,
I'm using NetBeans 7.2.1 with JDK 1.6 in a Windows XP system.
I have a problem with an Applet during runtime. The problem used to appear during compilation, but I found that adding the classpaths javaws.jar and jnlp.jar sorta solve the problem. Also, enabling Webstart and using it as a set configuration helped.
Again, the program compiles fine, but the problem arises during runtime. The program is supposed to ask the user for an image file in the local machine, but it doesn't do so.
The whole description of the problem:
compile-single:
run-applet:
javax.jnlp.UnavailableServiceException: uninitialized
at javax.jnlp.ServiceManager.lookup(ServiceManager.ja va:44)
at org.me.hello.ImageScale.init(ImageScale.java:63)
at sun.applet.AppletPanel.run(AppletPanel.java:417)
at java.lang.Thread.run(Thread.java:619)
BUILD SUCCESSFUL (total time: 12 seconds)
My code:
package org.me.hello; import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.jnlp.FileContents; import javax.jnlp.FileOpenService; import javax.jnlp.ServiceManager; import javax.jnlp.UnavailableServiceException; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class ImageScale extends JApplet { private ImageIcon image; private JPanel scaleJPanel; private JLabel percentJLabel; private JTextField scaleInputJTextField; private JButton scaleChangeJButton; private double scaleValue=1.0; @Override public void init() { scaleJPanel=new JPanel(); percentJLabel=new JLabel("scale percent:"); scaleInputJTextField=new JTextField("100"); scaleChangeJButton=new JButton("Set Scale"); scaleJPanel.add(percentJLabel); scaleJPanel.add(scaleInputJTextField ); scaleJPanel.add(scaleChangeJButton); add(scaleJPanel, BorderLayout.NORTH ); scaleChangeJButton.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { scaleValue=Double.parseDouble( scaleInputJTextField.getText() ) /100.0; repaint(); } } ); try { FileOpenService fileOpenService = (FileOpenService) ServiceManager.lookup("javax.jnlp.FileOpenService"); FileContents contents= fileOpenService.openFileDialog(null, null); byte[] imageData=new byte[ (int ) contents.getLength() ]; contents.getInputStream().read( imageData); image=new ImageIcon( imageData); add( new DrawJPanel(), BorderLayout.CENTER); } catch(Exception e) { e.printStackTrace(); } } private class DrawJPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); double spareWidth= getWidth()-scaleValue* image.getIconWidth(); double spareHeight= getHeight()-scaleValue*image.getIconHeight(); g.drawImage( image.getImage(), (int) (spareWidth)/2, (int)(spareHeight)/2, (int)(image.getIconWidth() *scaleValue), (int)(image.getIconHeight()*scaleValue ), this); } } }
Thank you.