Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

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...
0
OliWarner
Asked:
OliWarner
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now