Image in java

hai,
i have written this small code to generate image on the fly. But the images saved are always showing only black screen with out anythinh on them. can you pleaes explain me why its happening?
this is my code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.io.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
public class imageserver extends JFrame{
public String Generate()
          int r,g,b;
          String query;
BufferedImage buffer = new BufferedImage (375, 250, BufferedImage.TYPE_INT_RGB );
Graphics2D img = buffer.createGraphics();
JPEGImageEncoder encoder = null;
          //get new random numbers less than 255 for red r,green g and blue b
          r = 1 + (int) ( Math.random() * 255 );
          g = 1 + (int) ( Math.random() * 255 );
          b = 1 + (int) ( Math.random() * 255 );
//get random string of four characters; to prepare the puzzle string
          query = RndString(4);
//draw image in memory
          //set new random drawing color
          img.setColor(new Color(r,g,b));
//draw the string generated on to the buffered image
img.drawString( query, 390, 260 );
img.drawRect(380,300,420,320);
try {
encoder = JPEGCodec.createJPEGEncoder (new FileOutputStream ("test.jpeg"), JPEGCodec.getDefaultJPEGEncodeParam( buffer ));
encoder.encode( buffer);
catch(Exception e){ }
return(query);
     public static String RndString (int length)
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i < length;i++)
               sb.append( (char) (65 + (int) ( Math.random() * (90-65))));
          return sb.toString();
     public static void main( String args[])
imageserver imagefile = new imageserver();
System.out.println(imagefile.Generate());
Thankyou.
:-(

It looks like you're painting your text and rectangle outside the range of your buffer. Try reducing the x,y coordinates of each and it should work fine. Good luck!
-Chris

Similar Messages

  • Useless code in java.awt.image.SampleModel.java?

    Hey there,
    i just looked up the sourcecode of java.awt.image.SampleModel.java in JDK 6
    I discovered two issues i'd like to discuss.
    1) on lines 736 to 739 this code is stated:
    if (iArray != null)
    pixels = iArray;
    else
    pixels = new int[numBands * w * h];
    I asked myself, why does this code exist? while the getPixels() method is overwritten twice by double[] getPixels() and float[] getPixels, it is impossible to reach the part of the java code that initializes the pixels-array. One could only step into that line if "null" is given for the i/d/fArray-parameter. but if one would do so, the Java parser couldn't determine, which method is to use. so this part of code is just useless IMHO.
    the java developers could get a little more performance out of this method if the if statement would be cut out - especially when reading a lot of very small rasters
    or, on the other hand, they could replace this piece of code by an explicit bounds check.
    When somebody touches this code, i would appreciate it if the errormessage "index out of bounds!" could be rewritten to be a little more verbose, like: Index out of bounds(x=123; y=456, maxX=100, maxY=400)!(numbers are just examples)
    I hope i didn't miss something very basic and could help improving this class a little bit.
    2) the local variable Offset(line 734) is coded against code conventions which say, variables shall start with a lowercase letter. Offset obviously doesn't fit that convention.
    best regards
    kdot

    One could only step into that line if "null" is given for the i/d/fArray-parameter. but if one would do so, the Java parser couldn't determine, which method is to use. so this part of code is just useless IMHO. You can have
    sampleModel.getPixels(x,y,w,h,(int[]) null, dataBuffer);No ambiguity on which method to use.
    the local variable Offset(line 734) is coded against code conventions which say, variables shall start with a lowercase letter. Offset obviously doesn't fit that convention. You're correct, offset is against coding conventions. So are many other examples scattered throughout the jdk source code. For example, Hashtable should be called HashTable. In some cases the coding conventions might not have been established when the original code was written. In other cases it might have been human error. In yet other cases the conventions were probably ignored. The person who wrote the SampleModel class did so some 10+ years ago (Java 1.2). Who knows what he/she was thinking at the time, and in all honesty - does it really matter in this case?
    Did you know there are some classes that declare unused local variables (ahem ColorConvertOp)? Some also have unused imports ( *** cough *** BufferedImage *** cough *** ). In essence, the jdk source code is not the epidemy of code correctness. But it's still pretty good.

  • Display image in java.awt.List

    hello guys,
    How i can display image in java.awt.List means listbox. give me sample code. if you have
    Waiting for your favorable reply.
    Regards,
    Bhavesh Kharwa

    java.awt.List you can not.
    javax.swing.JLast you can.

  • How to open and validate the tif images via java?

    Is it possible to open and validating the photoshop images via java. Kindly advise me.
    Thanks for looking into this.
    Maria Prabudass

    I have recently looked at athe code for Image Processor.
    To avoid bailing on errors it uses two techniques.
    1) It turns off all PS error reporting in addition to just turning off dialogs with:     app.displayDialogs = DialogModes.NO;  (it restores the original settings when exiting)
    2) It uses the Javascript construct  Try.....Catch around the basic body of the code so "any" error will not abort the script but just jump to the "Catch" code.
    Hope that is useful

  • How to scale up and scale down a image in java

    i have a problem i want to scale the any image in java
    i mean that what i have to do actually when i scale up the image and scale down the image
    plz reply as soon as possible
    mail me at following address
    [email protected]
    [email protected]
    [email protected]

    Look at Image.getScaledImage(...)

  • How can I set the size of an image in java?

    How can I set the size of an image in java? I have to choose the width and height of image...thanks to everybody...sorry for my english :-)

    Hi 43477
    Can you provide more details, do you want to setsize to display an image on a screen of when saving image etc?
    PS.
    There is a good invention called googlegoogle is good, but sometimes it's better to use more specific search, there is a search field avove on this page :)

  • Images in Java

    How can I use images in java

    javapro9999 wrote:
    I thought this place was for "New to JAVA".It is. It is not "New to having a brain and being capable of asking meaningful questions that can be answered without having to be nudged until you do".
    I participate in other forums like asp.net but they don't reply that way.Instead they put a straightforward answer .Was it that difficult?With a vague idiotic question like: "How can I use images in java" - yes, it would have been difficult. Like what, someone is supposed to read your mind to determine what you don't understand, or just repeat what is already in tutorials here? The appropriate response given that stupid question was to just point you to the tutorials.

  • How to resize image using java imageio

    Hello ,
    I want to know how to resize image using java imageio.
    I dont want to use java awt because i am writing a web application.
    help is very much needed
    thank you.

    Just use an AffineTransform !
    Its much easier

  • How to create dynamic images in java servlets?

    I want to create dynamic images in java servlet. Can servlet create dynamic images that based on the input data files? The results can be displayed in GIF, JPG..format? how can this be done? any example in internet?
    What OS do I need to install and what other requirements needed if i want to build up a servlet server?
    Thanks a lot!

    Also worth having a look at SVG http://www.w3.org/TR/2001/REC-SVG-20010904/, you can get a viewer at http://www.adobe.com/svg/ or you can use Batik http://xml.apache.org/batik/index.html to convert SVG to other formats such as JPEG.
    HH

  • Transforms image between Java 2 and Visual J++6.0.

    Hello experts,
    Where describe that transforms image in Java 2
    to image in microsoft Visual J++6.0.
    Please help me ,thank you.
    BCH Oct 14,2004

    Eh, what?
    /Kaj

  • Opening Images With Java

    How do you open images with Java? It is a JPEG, and as most will figure out from this post, I'm new to this. :P thanks in advance for any effort...

    Well... Can you possibly tell me what to put in (eg Source Packages) so yea... I'll look into the tutorial, but I also have another question. Would this work?
    Image img = Toolkit.getDefaultToolkit().getImage(C://desktop/[gamefolder]/[hiddenimages]/[image]);
    When [gamefolder] is the name of the folder, [hiddenimages] is the sub-folder with the images in it, and [image] is the image...

  • Print image using java

    Dear All,
    Can any one help me, how to print the image in java.
    Thanks in Advance.

    [http://java.sun.com/products/java-media/2D/index.jsp|http://java.sun.com/products/java-media/2D/index.jsp] will help you . Java 2d also have print api.
    you look at this
    [http://java.sun.com/javase/technologies/desktop/printing/|http://java.sun.com/javase/technologies/desktop/printing/]

  • How to set color space to JPEG image with Java advance Imaging

    How to set color space to JPEG image with Java advance Imaging.
    is there any API in JAI which support to set color space.

    I'm definately no guru, but this is how you can change it.
    CTRL + ALT + Click on the part of the component that you want to change. This brings up the Hidden Dom Inspector, background of component will be surrounded with a red outline (Make sure the red outline is surrounding the part of the tabset you want to change), Now you go to properties sheet and click the ellipses next to rules property this will pop up a dialog you look in this list (At the top) to see the default style classes that are affecting the rendering of the component outlined in red. (You will be able to select different sections of a single component) then you just rewrite the style class that you want to change in your Stylesheet (You will not find the styleclass that you want to change because it is a part of your theme .jar but as long as you name it exactly the same and place in your stylesheet it will override the theme .jar style classes) it's actually very easy -- you were right should be a piece of cake for a guru. Don't have the link handy but you can check out Winston's Blog on changing Table Formatting to get this information...It is EXTREMELY useful if you want your apps to have a custom look and not default that comes with Creator Themes.
    Hope this helps you out God knows others have helped me alot!
    Jason

  • How to download an image from java server

    Hi,
       I have created a program which allows me to upload images to Java Server thru Multipart.
    How can i receive an image from server and download it to iPhone app.Is there any way apart from NSURL i can download the image.
    Base64 Encoding & Decoding  seems to be not working.
    Thanks in advance,
      Haritha

    Hi,
       I have created a program which allows me to upload images to Java Server thru Multipart.
    How can i receive an image from server and download it to iPhone app.Is there any way apart from NSURL i can download the image.
    Base64 Encoding & Decoding  seems to be not working.
    Thanks in advance,
      Haritha

  • Where can I download the last version that allows images and java script to be turned off?

    Due to my often slow connection I must have the option to turn off "automatically download images" and Java script. Firefox 23 does not have those options. Where can I download version 22 or whatever the latest version is that allowed these options?
    Thanks

    The Quickimage add-on doesn't work well at all, it gets stuck in the image off mode and will not switch back even after rebooting Firefox. Version 23.0 of Firefox is also very slow on my computer.
    I really need to find an older version of Firefox. Any idea where to download version 22 or earlier?

  • Fetching Images Via JAVA API

    Hi,
    I am trying to fetch images via Java Api.
    Please look at the code below:
    A2iValueArray imgs1 = null;
    for(int y=0; y<rs1.GetRecordCount(); y++){
         imgs1 = rs1.GetValueAt(y,"Product_Image").GetValueArray();
    It is throwing me a Class cast exception.
    I also tried
    imgs1 = (A2iValueArray) rs1.GetValueAt(y,"Product_Image").GetValueArray();
    but to no avail...
    I am using MDM Sp04..
    Please guide me...

    This Code Worked for me:
              try {     
              A2iResultSet rs1 = catalogData.GetResultSet(search, rsd, null, true, 0);
               A2iValueArray imgs1 = new A2iValueArray();
                   for(int y=0; y<rs1.GetRecordCount(); y++){
                        if(!rs1.GetValueAt(y, "Images").IsNull()){
                        imgs1 = rs1.GetValueAt(y,"Images").GetValueArray();
                        int id = imgs1.GetValueAt(0).GetIntValue();
                        //int id = rs1.GetValueAt(0, "Product_Color").GetIntValue();
                        String imagePath = catalogCache.GetImagePath("Products", id);//"Thumbnail" ,
                        //wdComponentAPI.getMessageManager().reportWarning("Your image is at: " + CACHE_DIRECTORY + "\" + imagePath);
                        System.out.println("imagePath="+imagePath);
                        break;
                   catalogData.Logout();
              } catch (StringException e) {
                   e.printStackTrace();
    Posting your program may help troubleshoot.
    Venkat

Maybe you are looking for

  • ICal on iMac does not update when I sync my iPhone

    My Sync Calendar option has been check marked in iTunes and "All Calendars" button has been selected, yet when I sync with my iPhone 5 (iMac has Leopard version 10.6.8), none of the calendared events entered first on my iPhone appear on my iMac iCale

  • Stuck in array

    I made one simple program, inside the program, I have array of 5. I've created 5 cards objects in array, each object has 2 variables inside(the suit and the value, e.g. 5 of hearts). now, i wanna compare whether there are 2 same cards, 2 same cards a

  • Event 'salesdocument assigned' not available in crm bus2000115

    Hello experts, in r3 we archive documents with the event 'SalesDocumetn Assigned' in object VBAK, now we changed to crm and there we do not have such an event in object 'bus2000115'. How can I automatically archive a document whenb its is assigned to

  • Sap Built In Tolerance For Write Offs

    Hi Guys We have been noticing that SAP is automatically writing off balances greater than $100.00 on some customer accounts, however will not allow as small as $0.01 on other accounts to be written off without manual intervention. In our Customer tol

  • Show all available episodes of a subscribed podcast without downloading?

    I have the podcasts I am subscribed to set to automatically download any new episodes, and automatically delete them once they have been listened to/watched.  This works well enough, but when they are deleted, those particular episodes are no longer