siayubi
asked on
manipulation of Images in servlets
I have a application which can convert object into image (.jpg/.gif). Acme.Jpm.Encoder.* can download from
http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.ht
ml
import java.awt.*;
import Acme.JPM.Encoders.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Converter extends Frame
{
TextCanvas canvas;
String str = "VirtualSoft";
Image bgImg;
int width;
int height;
public Converter()
{
setVisible(true);
setSize(300,300);
GraphicsEnvironment gEnv = GraphicsEnvironment.getLoc alGraphics Environmen t();
String envfonts[] = gEnv.getAvailableFontFamil yNames();
setFont(new Font("Speedball Regular",Font.BOLD,48));
String imgFile = "images/back42.jpg";
bgImg = Toolkit.getDefaultToolkit( ).getImage (imgFile);
MediaTracker mt = new MediaTracker(this);
mt.addImage(bgImg,0);
try
{
mt.waitForAll();
}
catch(Exception e){}
canvas = new TextCanvas();
add(canvas);
repaint();
canvas.createImage(str,bgI mg,width,h eight);
}
public static void main(String args[])
{
new Converter();
}
private void setImgSize(int width, int height)
{
this.width=width;
this.height=height;
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
FontMetrics metrics = g2.getFontMetrics();
height = metrics.getHeight();
width = metrics.stringWidth(str);
setImgSize(width,height);
}
}
class TextCanvas extends Canvas
{
int width;
int height;
String str;
Image bgImg;
TextCanvas()
{
setSize(200,200);
setVisible(true);
setBackground(Color.blue);
GraphicsEnvironment gEnv = GraphicsEnvironment.getLoc alGraphics Environmen t();
String envfonts[] = gEnv.getAvailableFontFamil yNames();
setFont(new Font("Vivacious Regular",Font.BOLD,48));
}
public void createImage(String str,Image bgImg,int width,int height)
{
this.str=str;
this.bgImg=bgImg;
this.width=width;
this.height=height;
try
{
Graphics g;
System.out.println(height) ;
System.out.println(width);
OutputStream fos = new FileOutputStream("c:/Temp/ Speedball. gif");
Image img = this.createImage(width+20, height+10) ;
g = img.getGraphics();
this.paint(g);
GifEncoder gifEncoder = new GifEncoder(img,fos);
gifEncoder.encode();
fos.flush();
fos.close();
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(bgImg,0,0,this );
g.drawString(str,5 ,height-10);
}
}
it work fine but when I want to do this work through servlets it was given exception on getGraphics().
I am trying to do this because I need a way to save and manipulation of images (i,e rotate, etc) on the server. I am trying to avoid using an applet, and keep the returned servlet code pure html.
Thanks
http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.ht
ml
import java.awt.*;
import Acme.JPM.Encoders.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Converter extends Frame
{
TextCanvas canvas;
String str = "VirtualSoft";
Image bgImg;
int width;
int height;
public Converter()
{
setVisible(true);
setSize(300,300);
GraphicsEnvironment gEnv = GraphicsEnvironment.getLoc
String envfonts[] = gEnv.getAvailableFontFamil
setFont(new Font("Speedball Regular",Font.BOLD,48));
String imgFile = "images/back42.jpg";
bgImg = Toolkit.getDefaultToolkit(
MediaTracker mt = new MediaTracker(this);
mt.addImage(bgImg,0);
try
{
mt.waitForAll();
}
catch(Exception e){}
canvas = new TextCanvas();
add(canvas);
repaint();
canvas.createImage(str,bgI
}
public static void main(String args[])
{
new Converter();
}
private void setImgSize(int width, int height)
{
this.width=width;
this.height=height;
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
FontMetrics metrics = g2.getFontMetrics();
height = metrics.getHeight();
width = metrics.stringWidth(str);
setImgSize(width,height);
}
}
class TextCanvas extends Canvas
{
int width;
int height;
String str;
Image bgImg;
TextCanvas()
{
setSize(200,200);
setVisible(true);
setBackground(Color.blue);
GraphicsEnvironment gEnv = GraphicsEnvironment.getLoc
String envfonts[] = gEnv.getAvailableFontFamil
setFont(new Font("Vivacious Regular",Font.BOLD,48));
}
public void createImage(String str,Image bgImg,int width,int height)
{
this.str=str;
this.bgImg=bgImg;
this.width=width;
this.height=height;
try
{
Graphics g;
System.out.println(height)
System.out.println(width);
OutputStream fos = new FileOutputStream("c:/Temp/
Image img = this.createImage(width+20,
g = img.getGraphics();
this.paint(g);
GifEncoder gifEncoder = new GifEncoder(img,fos);
gifEncoder.encode();
fos.flush();
fos.close();
}
catch(Exception e)
{
System.out.println("Error:
}
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(bgImg,0,0,this
g.drawString(str,5 ,height-10);
}
}
it work fine but when I want to do this work through servlets it was given exception on getGraphics().
I am trying to do this because I need a way to save and manipulation of images (i,e rotate, etc) on the server. I am trying to avoid using an applet, and keep the returned servlet code pure html.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
But when I want to get Server's font through the following code,
GraphicsEnvironment gEnv = GraphicsEnvironment.getLoc alGraphics Environmen t();
String envfonts[] = gEnv.getAvailableFontFamil yNames();
It will give the following exceptions
java.lang.ClassNotFoundExc eption: Unable to find class java.awt.GraphicsEnvironme nt at java.lang.Throwable
GraphicsEnvironment gEnv = GraphicsEnvironment.getLoc
String envfonts[] = gEnv.getAvailableFontFamil
It will give the following exceptions
java.lang.ClassNotFoundExc
ASKER
I am working in the Windows NT environment.
check your environment.
GraphicsEnvironment does NOT exist in JDK 1.1
GraphicsEnvironment does NOT exist in JDK 1.1
ASKER
I am working in JDK 1.2, It work fine in previous application but not the servlet.
ASKER
and working in JSDK 2.0 environment....
ASKER
and working in JSDK 2.0 environment....
ASKER
and working in JSDK 2.0 environment....
ASKER
and working in JSDK 2.0 environment....
ASKER
and working in JSDK 2.0 environment....
ASKER
and working in JSDK 2.0 environment....
ASKER
Thanks alot for pointing me towards the right solution.
can you give me your email address, my mailing address is
siayubi@hotmail.com
Once again alot of thanks
can you give me your email address, my mailing address is
siayubi@hotmail.com
Once again alot of thanks
my e-mail: heyhey_@iname.com
ASKER