如何将java(awt)代码转换为jsp代码
我有以下 java (awt) 代码,我想将其转换为 jsp。谁能告诉我该怎么做?
我的 java 类是 StackedBarChart1.java
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.awt.Rectangle;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.SubCategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.data.KeyToGroupMap;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.StandardGradientPaintTransformer;
public class StackedBarChart1 extends ApplicationFrame {
StackedBarChart1(String titel) {
super(titel);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));
setContentPane(chartPanel);
}
private CategoryDataset createDataset() {
DefaultCategoryDataset result = new DefaultCategoryDataset();
result.addValue(20.3, "Category 1 (D)", "Jan 08");
result.addValue(27.2, "Category 1 (D)", "Feb 08");
result.addValue(19.7, "Category 1 (D)", "Mar 08");
result.addValue(20.7, "Category 1 (D)", "Apr 08");
result.addValue(19.4, "Category 1 (E)", "Jan 08");
result.addValue(10.9, "Category 1 (E)", "Feb 08");
result.addValue(18.4, "Category 1 (E)", "Mar 08");
result.addValue(12.4, "Category 1 (E)", "Apr 08");
result.addValue(16.5, "Category 1 (W)", "Jan 08");
result.addValue(15.9, "Category 1 (W)", "Feb 08");
result.addValue(16.1, "Category 1 (W)", "Mar 08");
result.addValue(14.4, "Category 1 (W)", "Apr 08");
result.addValue(23.3, "Category 2 (D)", "Jan 08");
result.addValue(16.2, "Category 2 (D)", "Feb 08");
result.addValue(28.7, "Category 2 (D)", "Mar 08");
result.addValue(22.7, "Category 2 (D)", "Apr 08");
result.addValue(12.7, "Category 2 (E)", "Jan 08");
result.addValue(17.9, "Category 2 (E)", "Feb 08");
result.addValue(12.6, "Category 2 (E)", "Mar 08");
result.addValue(14.6, "Category 2 (E)", "Mar 08");
result.addValue(15.4, "Category 2 (W)", "Jan 08");
result.addValue(21.0, "Category 2 (W)", "Feb 08");
result.addValue(11.1, "Category 2 (W)", "Mar 08");
result.addValue(16.1, "Category 2 (W)", "Apr 08");
result.addValue(11.9, "Category 3 (D)", "Jan 08");
result.addValue(31.0, "Category 3 (D)", "Feb 08");
result.addValue(22.7, "Category 3 (D)", "Mar 08");
result.addValue(18.7, "Category 3 (D)", "Apr 08");
result.addValue(15.3, "Category 3 (E)", "Jan 08");
result.addValue(14.4, "Category 3 (E)", "Feb 08");
result.addValue(25.3, "Category 3 (E)", "Mar 08");
result.addValue(16.3, "Category 3 (E)", "Apr 08");
result.addValue(23.9, "Category 3 (W)", "Jan 08");
result.addValue(19.0, "Category 3 (W)", "Feb 08");
result.addValue(10.1, "Category 3 (W)", "Mar 08");
result.addValue(18.1, "Category 3 (W)", "Apr 08");
return result;
}
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createStackedBarChart(
"Referral Click Analysis (Stacked Bar Chart)", "Category", "Value", dataset,
PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(new Color(249, 231, 236));
GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
KeyToGroupMap map = new KeyToGroupMap("G1");
map.mapKeyToGroup("Category 1 (D)", "G1");
map.mapKeyToGroup("Category 1 (E)", "G1");
map.mapKeyToGroup("Category 1 (W)", "G1");
map.mapKeyToGroup("Category 2 (D)", "G2");
map.mapKeyToGroup("Category 2 (E)", "G2");
map.mapKeyToGroup("Category 2 (W)", "G2");
map.mapKeyToGroup("Category 3 (D)", "G3");
map.mapKeyToGroup("Category 3 (E)", "G3");
map.mapKeyToGroup("Category 3 (W)", "G3");
renderer.setSeriesToGroupMap(map);
renderer.setItemMargin(0.0);
Paint p1 = new GradientPaint(
0.0f, 0.0f, new Color(16, 89, 172), 0.0f, 0.0f, new
Color(201, 201, 244));
renderer.setSeriesPaint(0, p1);
renderer.setSeriesPaint(3, p1);
renderer.setSeriesPaint(6, p1);
Paint p2 = new GradientPaint(
0.0f, 0.0f, new Color(10, 144, 40), 0.0f, 0.0f, new
Color(160, 240, 180));
renderer.setSeriesPaint(1, p2);
renderer.setSeriesPaint(4, p2);
renderer.setSeriesPaint(7, p2);
Paint p3 = new GradientPaint(
0.0f, 0.0f, new Color(255, 35, 35), 0.0f, 0.0f, new
Color(255, 180, 180));
renderer.setSeriesPaint(2, p3);
renderer.setSeriesPaint(5, p3);
renderer.setSeriesPaint(8, p3);
renderer.setGradientPaintTransformer(
new StandardGradientPaintTransformer
(GradientPaintTransformType.HORIZONTAL));
SubCategoryAxis domainAxis = new SubCategoryAxis("Category / Month");
domainAxis.setCategoryMargin(0.05);
domainAxis.addSubCategory("Category 1");
domainAxis.addSubCategory("Category 2");
domainAxis.addSubCategory("Category 3");
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setDomainAxis(domainAxis);
plot.setRenderer(renderer);
plot.setFixedLegendItems(createLegendItems());
return chart;
}
private LegendItemCollection createLegendItems() {
LegendItemCollection result = new LegendItemCollection();
LegendItem item1 = new LegendItem("D", "D", "D", "D",
new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f,
new Color(16, 89, 172), 0.0f, 0.0f, new Color(201, 201, 244)));
LegendItem item2 = new LegendItem("E", "E", "E", "E",
new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f,
new Color(10, 144, 40), 0.0f, 0.0f, new Color(160, 240, 180)));
LegendItem item3 = new LegendItem("W", "W", "W", "W",
new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f,
new Color(255, 35, 35), 0.0f, 0.0f, new Color(255, 180, 180)));
result.add(item1);
result.add(item2);
result.add(item3);
return result;
}
public static void main(final String[] args) {
final StackedBarChart1 demo = new StackedBarChart1("Referral Click Analysis ");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
谢谢
尤加尔
i have the following java (awt) code and i want to convert it in jsp.can any one tell me how do i do this?
my java class is StackedBarChart1.java
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.awt.Rectangle;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.SubCategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.data.KeyToGroupMap;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.StandardGradientPaintTransformer;
public class StackedBarChart1 extends ApplicationFrame {
StackedBarChart1(String titel) {
super(titel);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));
setContentPane(chartPanel);
}
private CategoryDataset createDataset() {
DefaultCategoryDataset result = new DefaultCategoryDataset();
result.addValue(20.3, "Category 1 (D)", "Jan 08");
result.addValue(27.2, "Category 1 (D)", "Feb 08");
result.addValue(19.7, "Category 1 (D)", "Mar 08");
result.addValue(20.7, "Category 1 (D)", "Apr 08");
result.addValue(19.4, "Category 1 (E)", "Jan 08");
result.addValue(10.9, "Category 1 (E)", "Feb 08");
result.addValue(18.4, "Category 1 (E)", "Mar 08");
result.addValue(12.4, "Category 1 (E)", "Apr 08");
result.addValue(16.5, "Category 1 (W)", "Jan 08");
result.addValue(15.9, "Category 1 (W)", "Feb 08");
result.addValue(16.1, "Category 1 (W)", "Mar 08");
result.addValue(14.4, "Category 1 (W)", "Apr 08");
result.addValue(23.3, "Category 2 (D)", "Jan 08");
result.addValue(16.2, "Category 2 (D)", "Feb 08");
result.addValue(28.7, "Category 2 (D)", "Mar 08");
result.addValue(22.7, "Category 2 (D)", "Apr 08");
result.addValue(12.7, "Category 2 (E)", "Jan 08");
result.addValue(17.9, "Category 2 (E)", "Feb 08");
result.addValue(12.6, "Category 2 (E)", "Mar 08");
result.addValue(14.6, "Category 2 (E)", "Mar 08");
result.addValue(15.4, "Category 2 (W)", "Jan 08");
result.addValue(21.0, "Category 2 (W)", "Feb 08");
result.addValue(11.1, "Category 2 (W)", "Mar 08");
result.addValue(16.1, "Category 2 (W)", "Apr 08");
result.addValue(11.9, "Category 3 (D)", "Jan 08");
result.addValue(31.0, "Category 3 (D)", "Feb 08");
result.addValue(22.7, "Category 3 (D)", "Mar 08");
result.addValue(18.7, "Category 3 (D)", "Apr 08");
result.addValue(15.3, "Category 3 (E)", "Jan 08");
result.addValue(14.4, "Category 3 (E)", "Feb 08");
result.addValue(25.3, "Category 3 (E)", "Mar 08");
result.addValue(16.3, "Category 3 (E)", "Apr 08");
result.addValue(23.9, "Category 3 (W)", "Jan 08");
result.addValue(19.0, "Category 3 (W)", "Feb 08");
result.addValue(10.1, "Category 3 (W)", "Mar 08");
result.addValue(18.1, "Category 3 (W)", "Apr 08");
return result;
}
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createStackedBarChart(
"Referral Click Analysis (Stacked Bar Chart)", "Category", "Value", dataset,
PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(new Color(249, 231, 236));
GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
KeyToGroupMap map = new KeyToGroupMap("G1");
map.mapKeyToGroup("Category 1 (D)", "G1");
map.mapKeyToGroup("Category 1 (E)", "G1");
map.mapKeyToGroup("Category 1 (W)", "G1");
map.mapKeyToGroup("Category 2 (D)", "G2");
map.mapKeyToGroup("Category 2 (E)", "G2");
map.mapKeyToGroup("Category 2 (W)", "G2");
map.mapKeyToGroup("Category 3 (D)", "G3");
map.mapKeyToGroup("Category 3 (E)", "G3");
map.mapKeyToGroup("Category 3 (W)", "G3");
renderer.setSeriesToGroupMap(map);
renderer.setItemMargin(0.0);
Paint p1 = new GradientPaint(
0.0f, 0.0f, new Color(16, 89, 172), 0.0f, 0.0f, new
Color(201, 201, 244));
renderer.setSeriesPaint(0, p1);
renderer.setSeriesPaint(3, p1);
renderer.setSeriesPaint(6, p1);
Paint p2 = new GradientPaint(
0.0f, 0.0f, new Color(10, 144, 40), 0.0f, 0.0f, new
Color(160, 240, 180));
renderer.setSeriesPaint(1, p2);
renderer.setSeriesPaint(4, p2);
renderer.setSeriesPaint(7, p2);
Paint p3 = new GradientPaint(
0.0f, 0.0f, new Color(255, 35, 35), 0.0f, 0.0f, new
Color(255, 180, 180));
renderer.setSeriesPaint(2, p3);
renderer.setSeriesPaint(5, p3);
renderer.setSeriesPaint(8, p3);
renderer.setGradientPaintTransformer(
new StandardGradientPaintTransformer
(GradientPaintTransformType.HORIZONTAL));
SubCategoryAxis domainAxis = new SubCategoryAxis("Category / Month");
domainAxis.setCategoryMargin(0.05);
domainAxis.addSubCategory("Category 1");
domainAxis.addSubCategory("Category 2");
domainAxis.addSubCategory("Category 3");
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setDomainAxis(domainAxis);
plot.setRenderer(renderer);
plot.setFixedLegendItems(createLegendItems());
return chart;
}
private LegendItemCollection createLegendItems() {
LegendItemCollection result = new LegendItemCollection();
LegendItem item1 = new LegendItem("D", "D", "D", "D",
new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f,
new Color(16, 89, 172), 0.0f, 0.0f, new Color(201, 201, 244)));
LegendItem item2 = new LegendItem("E", "E", "E", "E",
new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f,
new Color(10, 144, 40), 0.0f, 0.0f, new Color(160, 240, 180)));
LegendItem item3 = new LegendItem("W", "W", "W", "W",
new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f,
new Color(255, 35, 35), 0.0f, 0.0f, new Color(255, 180, 180)));
result.add(item1);
result.add(item2);
result.add(item3);
return result;
}
public static void main(final String[] args) {
final StackedBarChart1 demo = new StackedBarChart1("Referral Click Analysis ");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Thanks
Yugal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 JSP 中,您可以在
<% .. %>
内编写任何 Java 代码。但你最好不要。重用您当前的课程。您可以使用<%@ page import=".." %>
导入它。但同样,您应该避免在 JSP 中编写任何 Java 代码。所以你最好在 servlet 中执行此操作(你可以在其中导入现有的类)In JSP you can write any java code within
<% .. %>
. But you'd better not. Reuse your current class. You can import it with<%@ page import=".." %>
. But again - you should avoid writing any java code in the JSP. So you'd better do this in a servlet (where you can import your existing class)严格来说,Bozho 的答案是正确的,但这不会神奇地将桌面 AWT 应用程序转换为 Web 应用程序。没有简单的方法可以将 AWT 桌面应用程序转换为 Web 应用程序。
您实际上有两个选择:
1)研究小程序作为将应用程序嵌入网页的一种方式。
2) 将您的 AWT 代码转换为一些 Web 应用程序 UI 技术,例如 Google Web Toolkit。
Strictly speaking, Bozho's answer is correct, but this won't magically convert a desktop AWT application into a web application. There's no simple way to get convert an AWT desktop app into a web app.
You really have two options:
1) Look into applets as a way to embed your app into a web page.
2) Translate your AWT code into some web-app UI technology like Google Web Toolkit.
实际上很简单..,将生成的 JFreeChart 对象作为图像(gif、jpg)保存在您的 web 应用程序上下文路径中,然后从您的 jsp 中提供该图像。
你完成了。
Quite simple actually.., save the generated JFreeChart object as an image (gif, jpg) in your webapp context path, then serve the image from your jsp.
You are done.