我犹豫要问这个,但是我花了很多时间搞乱环境变量,我只是想不出来:
我试图将applet上显示的内容保存到图像文件中,经过一些研究我认为JIMI是我最好的选择 (下面不是我的原创作品,而是http://www.coderanch.com/t/337338/GUI/java/save-applet-image-gif-jpeg关于我将要做什么的一个简单例子)
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.OutputStream;
import java.util.Vector;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiWriter;
public class JimiApplet extends Applet implements ActionListener {
private ImageCanvas canvas;
private TextField filename;
private Button save, clear;
public void init() {
setLayout( new BorderLayout() );
canvas = new ImageCanvas();
add( canvas, BorderLayout.CENTER );
Panel p = new Panel();
filename = new TextField();
save = new Button( "Save" );
save.addActionListener( this );
clear = new Button( "Clear" );
clear.addActionListener( this );
p.setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .70;
p.add( filename, c );
c.weightx = .15;
p.add( save, c );
c.weightx = .15;
p.add( clear, c );
add( p, BorderLayout.SOUTH );
}
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == save ) {
save.setEnabled( false );
filename.setEnabled( false );
filename.setEditable( false );
Thread t = new Thread( new SaveButtonPressed() );
t.start();
} else if ( e.getSource() == clear ) {
canvas.clear();
}
}
private static class ImageCanvas extends Canvas implements MouseMotionListener {
private Vector points;
public ImageCanvas() {
points = new Vector();
addMouseMotionListener( this );
}
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
points.add( p );
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void clear() {
points.removeAllElements();
repaint();
}
public void paint( Graphics g ) {
int size = points.size();
g.setColor( Color.black );
if ( size > 0 ) {
Point p1 = (Point)points.get( 0 );
for ( int i = 0; i < size; i++ ) {
Point p2 = (Point)points.get( i );
g.drawLine( p1.x, p1.y, p2.x, p2.y );
p1 = p2;
}
}
}
}
private class SaveButtonPressed implements Runnable {
public void run() {
Thread t = new Thread( new SaveRunnable() );
t.start();
try {
t.join();
} catch( InterruptedException x ) {
x.printStackTrace();
}
finally {
save.setEnabled( true );
filename.setEnabled( true );
filename.setEditable( true );
}
}
}
private class SaveRunnable implements Runnable {
public void run() {
int width = canvas.getSize().width;
int height = canvas.getSize().height;
ColorModel cm = canvas.getColorModel();
int[] pixels = new int[ width * height ];
Image image = canvas.createImage( width, height );
Graphics g = image.getGraphics();
canvas.paint( g );
PixelGrabber pg = new PixelGrabber( image, 0, 0, width, height, pixels, 0, width );
boolean success = false;
try {
success = pg.grabPixels();
}
catch (InterruptedException e2) {
e2.printStackTrace();
return;
}
if ( success ) {
String mimeType = "invalid";
String extension = filename.getText();
int index = extension.indexOf( '.' );
extension = extension.substring( index +1);
System.out.println(extension);
if ( extension.equalsIgnoreCase( "GIF" ) ) {
mimeType = "/image/gif";
} else if ( extension.equalsIgnoreCase( "JPG" ) ||
extension.equalsIgnoreCase( "JPEG" ) ) {
mimeType = "image/jpeg";
} else if ( extension.equalsIgnoreCase( "PNG" ) ) {
mimeType = "image/png";
}
// You can add more options here for the different image types
// you want to support.
if ( mimeType.equals( "invalid" ) ) {
System.err.println( "Could not get a valid mime type from file extension!" );
return;
} else {
try {
OutputStream imageOutput = null;
// Do something here to get an OutputStream pointing to the socket,
// URL of servlet, etc. that you are going to write your image to.
System.out.println("got here " + mimeType);
JimiWriter writer = Jimi.createJimiWriter( mimeType );
System.out.println("probably not here");
writer.setSource( image );
writer.putImage( imageOutput );
} catch ( Exception x ) {
x.printStackTrace();
return;
}
}
} else {
System.err.println( "PixelGrabber failed!" );
}
}
}
}
我可以在环境变量中使用JIMI编译此代码。但是,在尝试保存图像时,我收到错误:
Exception in thread "Thread-5" java.lang.NoClassDefFoundError: com/sun/jimi/core/Jimi
at JimiApplet$SaveRunnable.run(JimiApplet.java:165)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.sun.jimi.core.Jimi
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
at java.land.ClassLoader.loadClass(ClassLoader.java:247)
... 2 more
经过研究,我发现这很可能是因为我的环境变量中没有jimiproclasses,所以我尝试添加它们进行多种组合,花了我一个半小时来完成我认为可以工作的所有组合 在CLASSPATH和PATH中,我尝试了以下几点:
C:\Users\dfreelan\Dropbox\colormap\Jimi;
C:\Users\dfreelan\Dropbox\colormap\Jimi\JimiProClasses.zip;
C:\Users\dfreelan\Dropbox\colormap\Jimi;C:\Users\dfreelan\Dropbox\colormap\Jimi\JimiProClasses.zip;
C:\Users\dfreelan\Dropbox\colormap\Jimi\src\classes
我尝试了更多组合,但我相信你可以在这里获得要点 对于上面的每个例子,我都可以编译(这表明某种形式的Jimi正在工作)我只是不能做Jimi.createJimiWriter而不会在运行时收到错误
非常感谢任何人的帮助。我很抱歉,如果这似乎是一个令人讨厌的问题,但我已经对它进行了长时间的努力,我一丝不苟地进行了攻击,我觉得我没有任何地方可以转弯!