Dynamic imaging

I'm looking to get started in making a couple of dynamically drawn images in Java/JSP

All suggestions are welcome but i need them fast...
LVL 16
OliWarnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prcsn1Commented:
There's some really cool graph stuff at http://cewolf.sourceforge.net/new/ it does indeed generate graphs (using JFreeChart) and renders those as GIF, which ends up at the client. It works well - or did last time I ran it (2004), so it's probabyl even better now!
0
OliWarnerAuthor Commented:
I'm not after doing anything really complex, just drawing a few lines and a couple of lines of text depending on results of a database.

I've done much moer complicated jobs in ASP.net and my background is in Java so I was wondering where I start without components in the Java world...
0
TimYatesCommented:
http://today.java.net/pub/a/today/2004/04/22/images.html

Goes through the entire procedure of generating your own images...

Dont forget though, that if the webserver is running on a machine without a graphics card, you need to set the system property:

      java.awt.headless=true

Or it won't work :-)

Tim
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

OliWarnerAuthor Commented:
Those examples use a variable called "u" which I cant trace back to anything...
any ideas?
0
TimYatesCommented:
Where abouts?  I can't see a variable called "u" :-(

Hang on...I'll have another look...
0
TimYatesCommented:
Nope...can't find it...  can you post your code?
0
OliWarnerAuthor Commented:
certainly
this is taken from the source given at the end of tutorial on the second page:

<%@ page import="java.io.*" %>
<%@ page import="java.awt.*"
%><%@ page import="java.awt.image.*"
%><%@ page import="java.io.*"
%><%@ page import="javax.swing.*"
%><%@ page import="javax.imageio.*"
%><%@ page import="com.sun.image.codec.jpeg.*"
%><%
    try {
        String[] slices = request.getParameterValues("slice");
        String[] colors = request.getParameterValues("color");
        int[] sizes = new int[slices.length];
        Color[] cols = new Color[slices.length];
       
        for(int i=0; i<slices.length; i++) {
            u.p("sizes = " + slices[i]);
            u.p("colors = " + colors[i]);
            sizes[i] = Integer.parseInt(slices[i]);
            cols[i] = new Color(Integer.parseInt(colors[i],16));
        }
       
        int width = Integer.parseInt(request.getParameter("width"));
        int height = Integer.parseInt(request.getParameter("height"));
        Color background = new Color(Integer.parseInt(request.getParameter("background"),16));
       
       
        u.p("starting to do an image");
        // prepare some output
        BufferedImage buffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = buffer.createGraphics();
        g.setColor(background);
        g.fillRect(0,0,width,height);
        int arc = 0;
        for(int i=0; i<sizes.length; i++) {
            g.setColor(cols[i]);
            g.fillArc(0,0,width,height,arc,sizes[i]);
            arc += sizes[i];
        }
       
        // set the content type and get the output stream
        response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        // output the image as pnt
        ImageIO.write(buffer, "png", os);
        // output the image as a jpeg
        os.close();
    } catch (Exception ex) {
        u.p(ex);
    }
%>
0
TimYatesCommented:
Ooooh...  I see what you mean now :-(

Sorry about that, I didn't notice them before...

Don't know what that is...

Just get rid of them like this:

--------------

<%@ page import="java.io.*" %>
<%@ page import="java.awt.*"
%><%@ page import="java.awt.image.*"
%><%@ page import="java.io.*"
%><%@ page import="javax.swing.*"
%><%@ page import="javax.imageio.*"
%><%@ page import="com.sun.image.codec.jpeg.*"
%><%
    try {
        String[] slices = request.getParameterValues("slice");
        String[] colors = request.getParameterValues("color");
        int[] sizes = new int[slices.length];
        Color[] cols = new Color[slices.length];
       
        for(int i=0; i<slices.length; i++) {
            System.out.println("sizes = " + slices[i]);
            System.out.println("colors = " + colors[i]);
            sizes[i] = Integer.parseInt(slices[i]);
            cols[i] = new Color(Integer.parseInt(colors[i],16));
        }
       
        int width = Integer.parseInt(request.getParameter("width"));
        int height = Integer.parseInt(request.getParameter("height"));
        Color background = new Color(Integer.parseInt(request.getParameter("background"),16));
       
       
        System.out.println("starting to do an image");
        // prepare some output
        BufferedImage buffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = buffer.createGraphics();
        g.setColor(background);
        g.fillRect(0,0,width,height);
        int arc = 0;
        for(int i=0; i<sizes.length; i++) {
            g.setColor(cols[i]);
            g.fillArc(0,0,width,height,arc,sizes[i]);
            arc += sizes[i];
        }
       
        // set the content type and get the output stream
        response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        // output the image as pnt
        ImageIO.write(buffer, "png", os);
        // output the image as a jpeg
        os.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
%>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimYatesCommented:
Heh, that code (whilst it should work) looks a bit slapdash...  I guess he had a deadline to get it out for publication...

        // output the image as a jpeg

should obviously read:

        // output the image as a png
0
rrzCommented:
Here is an easier example. Put the files in your context root.     rrz

--------------------getImage.jsp----------------------------------------------------------------------
<html>
<body>
Do you see a blue square ?
<img src="<%=request.getContextPath()%>/imageIO.jsp" />
</body>
</html>
----------------------imageIO.jsp-------------------------------------------------------------
<%@ page import="java.awt.*, java.awt.image.*, java.util.*, java.io.*,javax.imageio.*,javax.imageio.stream.*" %><%
  int width=200, height=200;
  BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics g = bi.getGraphics();
  g.setColor(Color.BLUE);
  g.fillRect(0, 0, width, height);
  g.dispose();
  response.setContentType("image/jpg");
  Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
  ImageWriter writer = (ImageWriter)writers.next();
  ImageOutputStream ios = ImageIO.createImageOutputStream(response.getOutputStream());
  writer.setOutput(ios);
  writer.write(bi);
  ios.close();
  if(out!=null)return; %>
0
TimYatesCommented:
Of course, if you want to do anything meaningful like a graph, you will need to put all the code back in from the "trickier" example ;-)

hehehe
0
JK2429Commented:
0
OliWarnerAuthor Commented:
Thanks to tim and rrz@871311...
I've built a solution using bits of both code examples.

Thanks again. Oli.
0
TimYatesCommented:
Cool :-)  Good luck!!

Tim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.