Link to home
Start Free TrialLog in
Avatar of siayubi
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.getLocalGraphicsEnvironment();
    String envfonts[] = gEnv.getAvailableFontFamilyNames();
    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,bgImg,width,height);
  }
  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.getLocalGraphicsEnvironment();
    String envfonts[] = gEnv.getAvailableFontFamilyNames();
    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

Avatar of siayubi
siayubi

ASKER

Adjusted points to 50
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of siayubi

ASKER

But when I want to get Server's font through the following code,

GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
String envfonts[] = gEnv.getAvailableFontFamilyNames();

It will give the following exceptions

java.lang.ClassNotFoundException: Unable to find class java.awt.GraphicsEnvironment at java.lang.Throwable
Avatar of siayubi

ASKER

I am working in the Windows NT environment.
check your environment.
GraphicsEnvironment does NOT exist in JDK 1.1
Avatar of siayubi

ASKER

I am working in JDK 1.2, It work fine in previous application but not the servlet.
Avatar of siayubi

ASKER

and working in JSDK 2.0 environment....
Avatar of siayubi

ASKER

and working in JSDK 2.0 environment....
Avatar of siayubi

ASKER

and working in JSDK 2.0 environment....
Avatar of siayubi

ASKER

and working in JSDK 2.0 environment....
Avatar of siayubi

ASKER

and working in JSDK 2.0 environment....
Avatar of siayubi

ASKER

and working in JSDK 2.0 environment....
Avatar of siayubi

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
my e-mail: heyhey_@iname.com