TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
主文件: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};
//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<datavalues.length; i++)
{
average += datavalues;
}
average = average / datavalues.length;
//Calculate maximum
int maximum = 0;
for(int i=0; i<datavalues.length; i++)
{
if(datavalues > maximum)
{
maximum = datavalues;
}
}
///////////////////////////////////////////////////////////////////////////////////////
//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<datavalues.length; i++)
{
int currentValue = datavalues;
//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 + " (" + 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");
}
源码下载:http://file.javaxxz.com/2014/10/1/070254156.zip |
|