?
Solved

Dynamic imaging

Posted on 2005-03-13
14
Medium Priority
?
496 Views
Last Modified: 2010-04-01
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
Comment
Question by:OliWarner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 2

Expert Comment

by:prcsn1
ID: 13531900
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
 
LVL 16

Author Comment

by:OliWarner
ID: 13531926
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13532957
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Author Comment

by:OliWarner
ID: 13534781
Those examples use a variable called "u" which I cant trace back to anything...
any ideas?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13534915
Where abouts?  I can't see a variable called "u" :-(

Hang on...I'll have another look...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13534932
Nope...can't find it...  can you post your code?
0
 
LVL 16

Author Comment

by:OliWarner
ID: 13535057
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
 
LVL 35

Accepted Solution

by:
TimYates earned 1000 total points
ID: 13535185
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13535199
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
 
LVL 28

Assisted Solution

by:rrz
rrz earned 1000 total points
ID: 13535313
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13535347
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
 
LVL 4

Expert Comment

by:JK2429
ID: 13537465
0
 
LVL 16

Author Comment

by:OliWarner
ID: 13537526
Thanks to tim and rrz@871311...
I've built a solution using bits of both code examples.

Thanks again. Oli.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13537699
Cool :-)  Good luck!!

Tim
0

Featured Post

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.

Question has a verified solution.

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

The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
"Any files you do not have backed up in at least two [other] places are files you do not care about."
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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