用java编写一个颜色为红色,粗线型的笑脸和哭脸,需要继承JFrame类,用java中的绘制图形做

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java编写一个颜色为红色,粗线型的笑脸和哭脸,需要继承JFrame类,用java中的绘制图形做相关的知识,希望对你有一定的参考价值。

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);

需要完整版请联系我
参考技术A /*
 * 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();
    

参考技术B 去csdn,多得去了 参考技术C 20块,支付宝 参考技术D 这个还是要一点代码量的

用多个球从圆形边界反弹?

【中文标题】用多个球从圆形边界反弹?【英文标题】:Bouncing off a circular Boundary with multiple balls? 【发布时间】:2013-11-22 11:01:56 【问题描述】:

我正在制作这样的游戏:

黄色笑脸要逃离红色笑脸,当黄色笑脸撞到边界时游戏结束,当红色笑脸撞到边界时,它们应该以它们来的相同角度反弹,如下所示:

每10秒一个新的红色笑脸出现在大圆圈中,当红色笑脸碰到黄色时,游戏结束,红色笑脸的速度和起始角度应该是随机的。我用箭头键控制黄色笑脸。我遇到的最大问题是:创建多个红色笑脸,并将红色笑脸从边界反射到它们来的角度。我不知道如何给一个红色笑脸一个起始角度,并随着它出现的角度反弹它。我会很高兴任何提示! html 文件中的 Mycanvas 大小为 700x700。

我的js源码:

var canvas = document.getElementById("mycanvas");

var ctx = canvas.getContext("2d");

// Object containing some global Smiley properties.
var SmileyApp = 
   radius: 15,
   xspeed: 0,
   yspeed: 0,
   xpos:200,  // x-position of smiley
   ypos: 200  // y-position of smiley
;

var SmileyRed = 
   radius: 15,
   xspeed: 0,
   yspeed: 0,
   xpos:350,  // x-position of smiley
   ypos: 65  // y-position of smiley
;
function drawBigCircle() 
 var centerX = canvas.width / 2;
      var centerY = canvas.height / 2;
      var radiusBig = 300;

      ctx.beginPath();
      ctx.arc(centerX, centerY, radiusBig, 0, 2 * Math.PI, false);
//      context.fillStyle = 'green';
//      context.fill();
      ctx.lineWidth = 5;
 //     context.strokeStyle = '#003300'; // green
      ctx.stroke();


function lineDistance( positionx, positiony )

  var xs = 0;
  var ys = 0;

  xs = positionx - 350;
  xs = xs * xs;

  ys = positiony - 350;
  ys = ys * ys;

  return Math.sqrt( xs + ys );

function drawSmiley(x,y,r) 
   // outer border
   ctx.lineWidth = 3;
   ctx.beginPath();
   ctx.arc(x,y,r, 0, 2*Math.PI);
  //red ctx.fillStyle="rgba(255,0,0, 0.5)";
   ctx.fillStyle="rgba(255,255,0, 0.5)";
   ctx.fill();
   ctx.stroke();

   // mouth
   ctx.beginPath();
   ctx.moveTo(x+0.7*r, y);
   ctx.arc(x,y,0.7*r, 0, Math.PI, false);

   // eyes
   var reye = r/10;
   var f = 0.4;
   ctx.moveTo(x+f*r, y-f*r);
   ctx.arc(x+f*r-reye, y-f*r, reye, 0, 2*Math.PI);
   ctx.moveTo(x-f*r, y-f*r);
   ctx.arc(x-f*r+reye, y-f*r, reye, -Math.PI, Math.PI);

   // nose
   ctx.moveTo(x,y);
   ctx.lineTo(x, y-r/2);
   ctx.lineWidth = 1;
   ctx.stroke();


function drawSmileyRed(x,y,r) 
   // outer border
   ctx.lineWidth = 3;
   ctx.beginPath();
   ctx.arc(x,y,r, 0, 2*Math.PI);
  //red 
  ctx.fillStyle="rgba(255,0,0, 0.5)";
  //yellow ctx.fillStyle="rgba(255,255,0, 0.5)";
   ctx.fill();
   ctx.stroke();

   // mouth
   ctx.beginPath();
   ctx.moveTo(x+0.4*r, y+10);
   ctx.arc(x,y+10,0.4*r, 0, Math.PI, true);

   // eyes
   var reye = r/10;
   var f = 0.4;
   ctx.moveTo(x+f*r, y-f*r);
   ctx.arc(x+f*r-reye, y-f*r, reye, 0, 2*Math.PI);
   ctx.moveTo(x-f*r, y-f*r);
   ctx.arc(x-f*r+reye, y-f*r, reye, -Math.PI, Math.PI);

   // nose
   ctx.moveTo(x,y);
   ctx.lineTo(x, y-r/2);
   ctx.lineWidth = 1;
   ctx.stroke();



// --- Animation of smiley moving with constant speed and bounce back at edges of canvas ---
var tprev = 0;   // this is used to calculate the time step between two successive calls of run
function run(t) 
   requestAnimationFrame(run);
   if (t === undefined) 
      t=0;
   
   var h = t - tprev;   // time step 
   tprev = t;

   SmileyApp.xpos += SmileyApp.xspeed * h/1000;  // update position according to constant speed
   SmileyApp.ypos += SmileyApp.yspeed * h/1000;  // update position according to constant speed
   // change speed direction if smiley hits canvas edges
   if (lineDistance(SmileyApp.xpos, SmileyApp.ypos) + SmileyApp.radius > 300) 
     alert("Game Over");
   

   // redraw smiley at new position
   ctx.clearRect(0,0,canvas.height, canvas.width);
   drawBigCircle();
   drawSmiley(SmileyApp.xpos, SmileyApp.ypos, SmileyApp.radius);
   drawSmileyRed(SmileyRed.xpos, SmileyRed.ypos, SmileyRed.radius);


run();     

// --- Control smiley motion with left/right arrow keys                        
function arrowkeyCB(event) 
   event.preventDefault();

   if (event.keyCode === 37)  // left arrow
      SmileyApp.xspeed = -100; 
      SmileyApp.yspeed = 0;
    else if (event.keyCode === 39)  // right arrow
      SmileyApp.xspeed = 100; 
      SmileyApp.yspeed = 0;
    else if (event.keyCode === 38)  // up arrow
      SmileyApp.yspeed = -100;
      SmileyApp.xspeed = 0;   
    else if (event.keyCode === 40)  // right arrow
      SmileyApp.yspeed = 100; 
      SmileyApp.xspeed = 0;
   

document.addEventListener('keydown', arrowkeyCB, true);

JSFiddle : http://jsfiddle.net/X7gz7/

【问题讨论】:

你能设置一个工作的fiddle吗? @Rob 当然:jsfiddle.net/X7gz7 【参考方案1】:

您需要找出与碰撞点相切/垂直的线。然后从180度减去切线和半径线相交形成的角度,找到一个新的角度。

稍后我会发布更多解释的图片。

【讨论】:

以上是关于用java编写一个颜色为红色,粗线型的笑脸和哭脸,需要继承JFrame类,用java中的绘制图形做的主要内容,如果未能解决你的问题,请参考以下文章

Thinkphp怎么开启详细错误提示而不是出现哭脸

revit如何使不同系统的线显示不同的线型和颜色

matlab中画图线型怎么设置

表格四行四列,css样式边框粗为1像素,颜色为红色,只显示外围的边框颜色,怎么做才能实现内线也有颜色

Android TextView SpannableStringBuilder 图文混排颜色斜体粗体下划线删除线

matplotlib--设置线条颜色及形状