用java编写一个颜色为红色,粗线型的笑脸和哭脸,需要继承JFrame类,用java中的绘制图形做
6个回答
展开全部
JFreeChart------------它是一种组件技术,专用于在java中实现图形报表
----------饼图,柱状图,折线图
JFreeChart它是一种独立图表技术,它与struts2本身并无关系,只不过,它可以与struts2配合使用
饼图的步骤:
1、创建一个web工程
2、导入struts2框架(core,struts2-jfreeChart)
3、向工程导入(jfreechart.jar,jcommon.jar)
4、创建BaseAction继承于ActionSupport,并且,在类中,声明一个属性 JFreeChart chart;并且生成set,get方法
5、编写ChartAction类,继承于BaseAction
6、在ChartAction类中,编写 showPie()用于显示饼图
7、在struts.xml配置当前action
8、在index.jsp配置,通过<img src="chart!showPie.action">进入到指定方法
=================================================================================================================
9、编写ChartDao类,模拟从数据库查询数据
//得到饼图需要的数据集
public DefaultPieDataset getPieDataset(){
DefaultPieDataset dp = new DefaultPieDataset();
dp.setValue("联想",321);
dp.setValue("华硕",189);
dp.setValue("戴尔",98);
dp.setValue("IBM",213);
dp.setValue("Apple",287);
dp.setValue("惠普",120);
dp.setValue("SONY",87);
return dp;
}
==============================================================================================================
10、在ChartAction中的showPie方法,生成饼图
public String showPie(){
//得以要显示的数据集,根据数据,生成饼图
DefaultPieDataset dp = dao.getPieDataset();
//chart = ChartFactory.createPieChart("标题",要显示在饼图中的数据集,是否显示颜色说明,"是否显示工具提示","是否显示网络地址 ");
chart = ChartFactory.createPieChart("一季度各电脑品牌销售汇总",dp,true,false,false);
return "success";
}
=============================================================================================================
11、在struts.xml文件,进行相关配置
<package name="struts2" extends="struts-default">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/>
</result-types>
<action name="chart" class="org.java.web.ChartAction">
<result name="success" type="chart">
<param name="width">800</param>
<param name="height">600</param>
</result>
</action>
</package>
=================================================================================================================
12、解决乱码
标题区的乱码
Font f1 = new Font("隶书",Font.BOLD,40);
TextTitle tt = new TextTitle("一季度各电脑品牌销售汇总",f1);
chart.setTitle(tt);//绑定标题
颜色提示区乱码
Font f2 = new Font("隶书",Font.BOLD,20);
LegendTitle lt = chart.getLegend();
lt.setItemFont(f2);//设置该区域的字体
解决饼图中的乱码
PiePlot pp = (PiePlot) chart.getPlot();//得到饼图区域
pp.setLabelFont(f2);//设置饼图区域中的字体
=============================================================================================================
13、设置饼图中要显示的数据格式
//该对象,用于指定饼图要显示的数据格式 //0:key //1:value 2:百分比
StandardPieSectionLabelGenerator sc = new StandardPieSectionLabelGenerator("{0},{1}台,{2}");
//把显示的格式,绑定饼图中
pp.setLabelGenerator(sc);
需要完整版请联系我
----------饼图,柱状图,折线图
JFreeChart它是一种独立图表技术,它与struts2本身并无关系,只不过,它可以与struts2配合使用
饼图的步骤:
1、创建一个web工程
2、导入struts2框架(core,struts2-jfreeChart)
3、向工程导入(jfreechart.jar,jcommon.jar)
4、创建BaseAction继承于ActionSupport,并且,在类中,声明一个属性 JFreeChart chart;并且生成set,get方法
5、编写ChartAction类,继承于BaseAction
6、在ChartAction类中,编写 showPie()用于显示饼图
7、在struts.xml配置当前action
8、在index.jsp配置,通过<img src="chart!showPie.action">进入到指定方法
=================================================================================================================
9、编写ChartDao类,模拟从数据库查询数据
//得到饼图需要的数据集
public DefaultPieDataset getPieDataset(){
DefaultPieDataset dp = new DefaultPieDataset();
dp.setValue("联想",321);
dp.setValue("华硕",189);
dp.setValue("戴尔",98);
dp.setValue("IBM",213);
dp.setValue("Apple",287);
dp.setValue("惠普",120);
dp.setValue("SONY",87);
return dp;
}
==============================================================================================================
10、在ChartAction中的showPie方法,生成饼图
public String showPie(){
//得以要显示的数据集,根据数据,生成饼图
DefaultPieDataset dp = dao.getPieDataset();
//chart = ChartFactory.createPieChart("标题",要显示在饼图中的数据集,是否显示颜色说明,"是否显示工具提示","是否显示网络地址 ");
chart = ChartFactory.createPieChart("一季度各电脑品牌销售汇总",dp,true,false,false);
return "success";
}
=============================================================================================================
11、在struts.xml文件,进行相关配置
<package name="struts2" extends="struts-default">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/>
</result-types>
<action name="chart" class="org.java.web.ChartAction">
<result name="success" type="chart">
<param name="width">800</param>
<param name="height">600</param>
</result>
</action>
</package>
=================================================================================================================
12、解决乱码
标题区的乱码
Font f1 = new Font("隶书",Font.BOLD,40);
TextTitle tt = new TextTitle("一季度各电脑品牌销售汇总",f1);
chart.setTitle(tt);//绑定标题
颜色提示区乱码
Font f2 = new Font("隶书",Font.BOLD,20);
LegendTitle lt = chart.getLegend();
lt.setItemFont(f2);//设置该区域的字体
解决饼图中的乱码
PiePlot pp = (PiePlot) chart.getPlot();//得到饼图区域
pp.setLabelFont(f2);//设置饼图区域中的字体
=============================================================================================================
13、设置饼图中要显示的数据格式
//该对象,用于指定饼图要显示的数据格式 //0:key //1:value 2:百分比
StandardPieSectionLabelGenerator sc = new StandardPieSectionLabelGenerator("{0},{1}台,{2}");
//把显示的格式,绑定饼图中
pp.setLabelGenerator(sc);
需要完整版请联系我
展开全部
/*
* Copyright (c) cherry Co. Ltd.
* All right reserved.
*/
package com.cherry.swing;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.QuadCurve2D;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JFrame;
/**
* @version %I% %G%, 2016年1月7日 下午2:14:17
* @author cherry
*/
public class SmileDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 133080909855022641L;
private static final SmileDemo mSmileDemo;
private FaceClass mSmileFaceClass;
private FaceClass mCryFaceClass;
private FaceClass[] mFaceClassArr = new FaceClass[2];
static {
mSmileDemo = new SmileDemo("含泪的微笑");
}
public SmileDemo() {
}
/**
* @param cTitle
* : 窗口标题
*/
public SmileDemo(String cTitle) {
super(cTitle);
}
/*
* (non-Javadoc)
*
* @see java.awt.Container#paintComponents(java.awt.Graphics)
*/
@Override
public void paint(Graphics g) {
super.paintComponents(g);
Graphics2D tGraphics2D = (Graphics2D) g;
this.paintInstance(tGraphics2D);
}
public void paintInstance(Graphics2D cGraphics2D) {
cGraphics2D.setColor(Color.red);
cGraphics2D.setStroke(new BasicStroke(3f));// 设置线条粗细为3像素
cGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);// 消除锯齿
for (FaceClass fc : mFaceClassArr) {
Method[] tMethodArr = fc.getClass().getMethods();
for (Method m : tMethodArr) {
String tMethodName = m.getName();// 获取方法名
if (tMethodName.endsWith("ChartPoint")) {
ChartPoint tChartPoint;
try {
tChartPoint = (ChartPoint) m.invoke(fc, null);
cGraphics2D.setColor(Color.red);
if(tMethodName.contains("Mouth")) {
//嘴
cGraphics2D.draw(new QuadCurve2D.Float(tChartPoint.xHor, tChartPoint.yVer, tChartPoint.xMid, tChartPoint.yMid, tChartPoint.xLong, tChartPoint.yWidth));
} else {
if(tMethodName.contains("Eye") || tMethodName.contains("Eye")) {
//眼
cGraphics2D.drawOval(tChartPoint.xHor, tChartPoint.yVer, tChartPoint.xLong, tChartPoint.yWidth);
cGraphics2D.setColor(Color.black);//给眼睛填充黑色
cGraphics2D.fillOval(tChartPoint.xHor+2, tChartPoint.yVer+1, tChartPoint.xLong-2, tChartPoint.yWidth-2);
} else {
//轮廓
cGraphics2D.drawOval(tChartPoint.xHor, tChartPoint.yVer, tChartPoint.xLong, tChartPoint.yWidth);
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void initWindow() {
mSmileDemo.setDefaultCloseOperation(EXIT_ON_CLOSE);// 退出时关闭程序
mSmileDemo.setSize(800, 600);
mSmileDemo.getContentPane().setBackground(Color.white);
// mSmileDemo.pack();
// 初始化SmileOrCry相关坐标参数
// 笑脸
ChartPoint tFaceChartPoint_Smile = mSmileDemo.new ChartPoint(50, 140,
320, 320);
ChartPoint tLeft_EyeChartPoint_Smile = mSmileDemo.new ChartPoint(120,
200, 20, 40);
ChartPoint tRight_EyeChartPoint_Smile = mSmileDemo.new ChartPoint(280,
200, 20, 40);
ChartPoint tMouthChartPoint_Smile = mSmileDemo.new ChartPoint(120, 380,
210, 450, 300, 380);
mSmileFaceClass = mSmileDemo.new FaceClass(tFaceChartPoint_Smile,
tLeft_EyeChartPoint_Smile, tRight_EyeChartPoint_Smile,
tMouthChartPoint_Smile);
// 哭脸
ChartPoint tFaceChartPoint_Cry = mSmileDemo.new ChartPoint(450, 140,
320, 320);
ChartPoint tLeft_EyeChartPoint_Cry = mSmileDemo.new ChartPoint(520,
200, 20, 40);
ChartPoint tRight_EyeChartPoint_Cry = mSmileDemo.new ChartPoint(680,
200, 20, 40);
ChartPoint tMouthChartPoint_Cry = mSmileDemo.new ChartPoint(520, 380,
610, 320, 700, 380);
mCryFaceClass = mSmileDemo.new FaceClass(tFaceChartPoint_Cry,
tLeft_EyeChartPoint_Cry, tRight_EyeChartPoint_Cry,
tMouthChartPoint_Cry);
// 将两个表情存入数组
mFaceClassArr[0] = mSmileFaceClass;
mFaceClassArr[1] = mCryFaceClass;
mSmileDemo.setVisible(true);
}
private class FaceClass {
private ChartPoint mFaceChart_Point;// 脸部坐标及相关参数
private ChartPoint mLeft_EyeChartPoint;// 左眼坐标及相关参数
private ChartPoint mRight_EyeChartPoint;// 右眼坐标及相关参数
private ChartPoint mMouthChart_Point;// 嘴巴坐标及相关参数
public ChartPoint getmFaceChartPoint() {
return mFaceChart_Point;
}
public ChartPoint getmLeft_EyeChartPoint() {
return mLeft_EyeChartPoint;
}
public ChartPoint getmRight_EyeChartPoint() {
return mRight_EyeChartPoint;
}
public ChartPoint getmMouthChartPoint() {
return mMouthChart_Point;
}
public FaceClass(ChartPoint cFaceChart_Point,
ChartPoint mLeft_EyeChartPoint,
ChartPoint mRight_EyeChartPoint, ChartPoint mMouthChart_Point) {
this.mFaceChart_Point = cFaceChart_Point;
this.mLeft_EyeChartPoint = mLeft_EyeChartPoint;
this.mRight_EyeChartPoint = mRight_EyeChartPoint;
this.mMouthChart_Point = mMouthChart_Point;
}
}
private class ChartPoint {
private int xHor;// 横坐标||或左端点横坐标
private int yVer;// 纵坐标||或左端点纵坐标
private int xLong;// 长||或右端点横坐标
private int yWidth;// 宽||或右端点纵坐标
private int xMid;// 嘴巴方向
private int yMid;// 嘴巴方向
public int getxMid() {
return xMid;
}
public int getyMid() {
return yMid;
}
public int getxHor() {
return xHor;
}
public int getyVer() {
return yVer;
}
public int getxLong() {
return xLong;
}
public int getyWidth() {
return yWidth;
}
public ChartPoint(int xHor, int yVer, int xLong, int yWidth) {
this.xHor = xHor;
this.yVer = yVer;
this.xLong = xLong;
this.yWidth = yWidth;
}
public ChartPoint(int xHor, int yVer, int xMid, int yMid, int xLong, int yWidth) {
this.xHor = xHor;
this.yVer = yVer;
this.xMid = xMid;
this.yMid = yMid;
this.xLong = xLong;
this.yWidth = yWidth;
}
}
public static void main(String[] args) {
mSmileDemo.initWindow();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
去csdn,多得去了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
20块,支付宝
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个还是要一点代码量的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询