Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 283|回复: 0

[jsp学习]用JSP创建可重用的图形背景

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 07:02:54 | 显示全部楼层 |阅读模式
    主文件: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
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-6 11:04 , Processed in 0.420346 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表