?
Solved

manipulation of Images in servlets

Posted on 2000-02-07
14
Medium Priority
?
304 Views
Last Modified: 2013-11-24
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

0
Comment
Question by:siayubi
  • 11
  • 3
14 Comments
 

Author Comment

by:siayubi
ID: 2496693
Adjusted points to 50
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
ID: 2499066
you can access the real UI resources after the Frame / Component peer (that is the real underlying OS dependand implementation) is created. usually it is created when you make the Frame / component visible. you can create them manually with
frame.addNotify();

btw. you may have some additional problems in Unix / XWindows enviroment.

0
 

Author Comment

by:siayubi
ID: 2499528
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:siayubi
ID: 2499576
I am working in the Windows NT environment.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2499656
check your environment.
GraphicsEnvironment does NOT exist in JDK 1.1
0
 

Author Comment

by:siayubi
ID: 2499823
I am working in JDK 1.2, It work fine in previous application but not the servlet.
0
 

Author Comment

by:siayubi
ID: 2499835
and working in JSDK 2.0 environment....
0
 

Author Comment

by:siayubi
ID: 2499871
and working in JSDK 2.0 environment....
0
 

Author Comment

by:siayubi
ID: 2499883
and working in JSDK 2.0 environment....
0
 

Author Comment

by:siayubi
ID: 2499890
and working in JSDK 2.0 environment....
0
 

Author Comment

by:siayubi
ID: 2499961
and working in JSDK 2.0 environment....
0
 

Author Comment

by:siayubi
ID: 2499973
and working in JSDK 2.0 environment....
0
 

Author Comment

by:siayubi
ID: 2503134
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
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2506781
my e-mail: heyhey_@iname.com
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month4 days, 20 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question