首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → 用JSP创建可重用的图形背景

用JSP创建可重用的图形背景

摘要: 用JSP创建可重用的图形背景

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"><table width="722" border="0"> <tr> <td width="453">
主文件:index.jsp
<%@ page language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.awt.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>

<%@ include file = "graphBG.jsp"%>

<%

//Data arrays
String datanames[] = {"Apples", "Oranges", "Peaches", "Lemons", "Grapefruit"};
int datavalues[] = {11, 62, 33, 102, 50};
</td> <td width="258" align="right">
</td> </tr> </table> ??
//current y position
int y_pos = 0;

//y offset to cater for header space
int headerOffset = 50;

//inner padding to make sure bars never touch the outer border
int innerOffset = 20;

//height of bar, text and total
int barHeight = 10;
int textHeight = 20;
int displayHeight = barHeight + textHeight;

//Color used for the bars
Color barColor = new Color(153,19,19);

//Set the graph's outer width
int WIDTH = 300;

//Set the graph's outer height
int HEIGHT = (datavalues.length * displayHeight) + headerOffset + innerOffset;

//Width of the graphable area
int innerWIDTH = WIDTH - (innerOffset * 2);

//Calculate average
int average = 0;
for(int i=0; i {
average += datavalues[i];
}
average = average / datavalues.length;

//Calculate maximum
int maximum = 0;
for(int i=0; i {
if(datavalues[i] > maximum)
{
maximum = datavalues[i];
}
}




///////////////////////////////////////////////////////////////////////////////////////
//Draw Graph Background and Header:
///////////////////////////////////////////////////////////////////////////////////////

response.setContentType("image/jpeg");
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
graphBG gr = new graphBG();
gr.draw(biContext, WIDTH, HEIGHT, "Farm Produce", "Overall Average: " + average);



/////////////////////////////////////////////////////////////////////////////////////
//Draw data onto the graph:
/////////////////////////////////////////////////////////////////////////////////////


//Loop through & draw the bars

for(int i=0; i {
int currentValue = datavalues[i];

//Set y position for bar
y_pos = i * displayHeight + headerOffset;

//Set bar width
int barWidth = (innerWIDTH * currentValue) / maximum;

//Display the current value
String display = datanames[i] + " (" + currentValue + ")";
biContext.setColor(Color.black);
biContext.drawString(display, 20, y_pos);

//Set dimensions of the bar
biContext.setColor(barColor);
Dimension bar = new Dimension(barWidth , barHeight);
Dimension barBorder = new Dimension(barWidth - 3 , barHeight - 3);
Rectangle barRect = new Rectangle(bar);
Rectangle barRectBorder = new Rectangle(barBorder);

//Draw bar and border:
barRect.setLocation(21, 5 + y_pos);
barRectBorder.setLocation(22, 6 + y_pos);
biContext.setColor(barColor);
biContext.fill(barRect);
biContext.setColor(Color.white);
biContext.draw(barRectBorder);
}



/////////////////////////////////////////////////////////////////////////////////////
//Display the graph
/////////////////////////////////////////////////////////////////////////////////////

//Encode:
OutputStream output = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(bi);
output.close();

%>

下面是graphBG.jsp
<%!
public class graphBG
{

Font font = new Font("ARIAL", Font.BOLD , 11);
Color headerColor = new Color(20,50,100);

public Graphics2D draw(Graphics2D biContext, int WIDTH, int HEIGHT, String headerText, String averageText)
{
//Grey color for the drop shadow
Color dropShadow = new Color(200,200,200);

//Graph and header dimensions
Dimension dim = new Dimension(WIDTH,HEIGHT);
Dimension dimGraph = new Dimension(WIDTH-21,HEIGHT-51);
Dimension dimHeader = new Dimension(WIDTH-21,20);

//Define Rectangles:
Rectangle area = new Rectangle(dim);
Rectangle graphArea = new Rectangle(dimGraph);
Rectangle headerArea = new Rectangle(dimHeader);

/////////////////////////////////////////////////////////////////////////////////////
//Set up the graph:
/////////////////////////////////////////////////////////////////////////////////////

//Set background color to white:
biContext.setColor(Color.white);
biContext.fill(area);

//Drop shadow for the graph area:
graphArea.setLocation(18, 42); //This is the drop shadow's location
biContext.setColor(dropShadow);
biContext.fill(graphArea);

//Fill the graph area (white):
graphArea.setLocation(11, 36);
biContext.setColor(Color.white);
biContext.fill(graphArea);

//Draw the graph border (black):
biContext.setColor(Color.black);
biContext.draw(graphArea);


//Header Drop Shadow:
headerArea.setLocation(18, 12); //This is the drop shadow's location
biContext.setColor(dropShadow);
biContext.fill(headerArea);

//Fill the header (blue):
headerArea.setLocation(11, 5);
biContext.setColor(headerColor);
biContext.fill(headerArea);

//Draw the header border (white):
biContext.setColor(Color.white);
biContext.draw(headerArea);

//Insert Header Text:
biContext.setFont(font);
biContext.setColor(Color.white);
biContext.drawString(headerText, 21, 18);

//Display the average
biContext.setColor(Color.white);
biContext.drawString(averageText, 150, 18);


return biContext;
}
}
%>
function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> </tr> <tr>


↑返回目录
前一篇: 在jsp中使用SAX
后一篇: 给URL字符串加超链接

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved