Jump to content

Drawing lines from way points


toledo05

Recommended Posts

I have a database field containing a series of x,y coordinates. Within my Java application I am able to take these coordinates to generate an image (a signature, actually). I can pass this as a parameter to JavaReport when I have only one signature. What I would like to do is have this code run from within the report for each entry that needs it. Is is possible to create an imageExpression with containing Java code that returns an java.awt.Image?

 

Below is the Java code. I'm thinking the SigData and Rectangle values would be pulled from $F{fields} or $P{Parameters}. I'm not sure how something like this could be embedded within the report but it would encapsulate the functionality and work when multiple images are required.

 

Thanks in advance.

 

Code:

public BufferedImage DrawSignature(String SigData, Rectangle aRect)
{
int SigHgt = 0, SigWdt = 0, SigBase = 0, NbrPoints = 0;
int Pos = 0;
int i = 0;
int x = 0, y = 0;
float AspectX = 0, AspectY = 0;

if (aRect == null)
aRect = new Rectangle(150, 50);

for (i = 0; i < 4; i++)
{
Pos = SigData.indexOf(" "«»);
if (Pos == 0)
return null;
try
{
String tmp = SigData.substring(0, Pos);
x = Integer.parseInt(tmp);
}
catch(Exception e)
{
return null;
}
SigData = SigData.substring(Pos).trim();

switch (i)
{
case 0: SigHgt = x; break;
case 1: SigWdt = x; break;
case 2: SigBase = x; break;
case 3: NbrPoints = x-1; break;
}
}

// Lets get determine the growth, shrink ratio to perform on each
// point so that the name fills the box.
AspectY = (float) aRect.getHeight() / (float) SigHgt;
AspectX = (float) aRect.getWidth() / (float) SigWdt;

BufferedImage signature = new BufferedImage( SigWdt, SigHgt, BufferedImage.TYPE_USHORT_565_RGB );
Graphics g = signature.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, SigWdt, SigHgt);
g.setColor(Color.BLACK);

// Now go through all the points and draw the signature
int LastX = 0;
int LastY = 0;
// Get to the X, Y Positions
Pos = SigData.indexOf(","«»);
if (Pos == 0)
return null;
SigData = SigData.substring(Pos+1).trim(); // remove the comma
for (i = 0; i < NbrPoints; i++)
{
// Get the X Position of the point
Pos = SigData.indexOf(" "«»);
if (Pos == 0)
return null;
try
{
String tmp = SigData.substring(0, Pos);
y = Integer.parseInt(tmp);
}
catch(Exception e)
{
return null;
}
SigData = SigData.substring(Pos).trim();

// Get the Y Position of the point
Pos = SigData.indexOf(","«»);
if (Pos == 0)
return null;
try
{
String tmp = SigData.substring(0, Pos);
x = Integer.parseInt(tmp);
}
catch(Exception e)
{
return null;
}
SigData = SigData.substring(Pos+1).trim();

if (x != -1 && LastX > 0 && LastY > 0)
g.drawLine(LastX, LastY, x, y);

LastX = x;
LastY = y;
}
g.dispose();

return signature;
}
}
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Thanks Teodor,

 

I bought the ultimate guide but I seem to be misunderstanding something. I continue get get evaluation errors when I check the expression. Are there any examples available that show how to do something more complex then concatenate a couple of strings in a textexpression?

 

Thanks again.

Link to comment
Share on other sites

Hi,

 

The errors you get would tell you exactly what is wrong with the, just like the errors you get when you compile a Java file. The expressions in JRXML are Java expressions (unless you changed the language of the report template).

The book cannot tell what is wrong with your expressions, just show you how to use JasperReports.

 

Can we see the error you get?

 

I hope this helps.

Teodor

Link to comment
Share on other sites

Hi Teodord,

 

Here are the errors. Also, the code in the eimageexpression.

 

Thanks.

 

 

it.businesslogic.ireport.gui.logpane.ProblemItem@1409285 Syntax error on token "Image", new expected Line 94, Column 46 /jasperReport/detail[1]/band[1]/image[1]/imageExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@538db1 Syntax error, insert ";" to complete BlockStatements Line 94, Column 46 /jasperReport/detail[1]/band[1]/image[1]/imageExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@17b9a1a Syntax error, insert ")" to complete Expression Line 94, Column 46 /jasperReport/detail[1]/band[1]/image[1]/imageExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@1c247ad Syntax error, insert "}" to complete ClassBody Line 94, Column 46 /jasperReport/detail[1]/band[1]/image[1]/imageExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@1efbf25 Syntax error on token ")", { expected Line 94, Column 46 /jasperReport/detail[1]/band[1]/image[1]/imageExpression[1]

 

 

 

 

Code:

Image DrawSignature()
{
String SigData = "F{SignatureInfo}";
int SigHgt = 0, SigWdt = 0, SigBase = 0, NbrPoints = 0;
int Pos = 0;
int i = 0;
int x = 0, y = 0;
float AspectX = 0, AspectY = 0;

//if (aRect == null)
aRect = new Rectangle(150, 50);

for (i = 0; i < 4; i++)
{
Pos = SigData.indexOf(" "«»);
if (Pos == 0)
return null;
try
{
String tmp = SigData.substring(0, Pos);
x = Integer.parseInt(tmp);
}
catch(Exception e)
{
return null;
}
SigData = SigData.substring(Pos).trim();

switch (i)
{
case 0: SigHgt = x; break;
case 1: SigWdt = x; break;
case 2: SigBase = x; break;
case 3: NbrPoints = x-1; break;
}
}

// Lets get determine the growth, shrink ratio to perform on each
// point so that the name fills the box.
AspectY = (float) aRect.getHeight() / (float) SigHgt;
AspectX = (float) aRect.getWidth() / (float) SigWdt;

BufferedImage signature = new BufferedImage( SigWdt, SigHgt, BufferedImage.TYPE_USHORT_565_RGB );
Graphics g = signature.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, SigWdt, SigHgt);
g.setColor(Color.BLACK);

// Now go through all the points and draw the signature
int LastX = 0;
int LastY = 0;
// Get to the X, Y Positions
Pos = SigData.indexOf(","«»);
if (Pos == 0)
return null;
SigData = SigData.substring(Pos+1).trim(); // remove the comma
for (i = 0; i < NbrPoints; i++)
{
// Get the X Position of the point
Pos = SigData.indexOf(" "«»);
if (Pos == 0)
return null;
try
{
String tmp = SigData.substring(0, Pos);
y = Integer.parseInt(tmp);
}
catch(Exception e)
{
return null;
}
SigData = SigData.substring(Pos).trim();

// Get the Y Position of the point
Pos = SigData.indexOf(","«»);
if (Pos == 0)
return null;
try
{
String tmp = SigData.substring(0, Pos);
x = Integer.parseInt(tmp);
}
catch(Exception e)
{
return null;
}
SigData = SigData.substring(Pos+1).trim();

if (x != -1 && LastX > 0 && LastY > 0)
g.drawLine(LastX, LastY, x, y);

LastX = x;
LastY = y;
}
g.dispose();

return signature;
};
Link to comment
Share on other sites

Hi,

 

That is not a Java expression. That is a Java method declaration. Don't tell me you put that in the <imageExpression> tag in JRXML...

 

Put it in a some sort of utility class in your application, make it static and let it have some parameters if you need it to receive some information in order to return your image. Only after that, make the call in the <imageExpression> to this utility class of yours to produce the image by passing some report parameters or report fields to it.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...