自定义java.awt.Canvas—趣味聊天

2024-06-18 03:48

本文主要是介绍自定义java.awt.Canvas—趣味聊天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近用java模拟“会说话的汤姆猫”做了一个超级简陋的小程序。重点研究了一下JFrame(javax.swing.JFram)、SequentialGroup(javax.swing.GroupLayout.SequentialGroup)、ParallelGroup(javax.swing.GroupLayout. ParallelGroup)、Canvas(java.awt.Canvas)、Graphics(java.awt.Graphics)的使用。

程序效果如下: 欢迎大家下载这个小程序的可运行jar包!趣味聊天demo.jar

可以在问题框中输入任意问题与小猫对话。当输入问题时,小猫会做出聆听状态。

点击发送后,小猫做回答状态。对于各种问题的答案,这里调用的是欧拉蜜智能语音api,详情可参考http://cn.olami.ai

主要代码如下:

1、界面MyJFrame:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.URL;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.alibaba.fastjson.JSONObject;public class MyJFrame extends javax.swing.JFrame {private static final long serialVersionUID = 1L;private NLIProcess nlp = new NLIProcess();private JButton jButton_send;private JLabel jLabel1;private JLabel jLabel2;private JPanel jPanel1;private JPanel jPanel2;private JTextField jTextField1;private JTextField jTextField2;private Image act1;private Image act2;// 初始图像编号int actPhase = 1;   Canvas c = null;public MyJFrame() {getImage();     initComponents();}private void getImage(){setBackground(Color.BLACK);URL imageUrl = getClass().getResource("image/img0.png");act1 = Toolkit.getDefaultToolkit().createImage(imageUrl);imageUrl = getClass().getResource("image/img3.png");act2 = Toolkit.getDefaultToolkit().createImage(imageUrl);}   private void initComponents() {jPanel1 = new JPanel();jLabel1 = new JLabel();jLabel2 = new JLabel();jTextField1 = new JTextField();jTextField2 = new JTextField();jButton_send = new JButton();jPanel2 = new JPanel();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setMinimumSize(new java.awt.Dimension(400, 300));jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("智能问答"));jLabel1.setText("问题:");jLabel2.setText("回答:");jTextField1.setText("你多大了?");jTextField2.setEnabled(false);jButton_send.setText("send");jButton_send.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton_sendActionPerformed(evt);}});jTextField1.addFocusListener(new FocusListener(){@Overridepublic void focusGained(FocusEvent arg0) {// TODO Auto-generated method stubactPhase = 2;c.repaint();}@Overridepublic void focusLost(FocusEvent arg0) {// TODO Auto-generated method stubactPhase = 1;c.repaint();}});GroupLayout jPanel1Layout = new GroupLayout(jPanel1);GroupLayout.SequentialGroup hGroup = jPanel1Layout.createSequentialGroup();hGroup.addGap(8);hGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel1).addComponent(jLabel2));hGroup.addGap(5);hGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jTextField1).addComponent(jTextField2));hGroup.addGap(5);hGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jButton_send));hGroup.addGap(8);jPanel1Layout.setHorizontalGroup(hGroup);GroupLayout.SequentialGroup vGroup = jPanel1Layout.createSequentialGroup();vGroup.addGap(8);vGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel1).addComponent(jTextField1).addComponent(jButton_send));vGroup.addGap(3);vGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel2).addComponent(jTextField2));vGroup.addGap(8);//设置垂直组jPanel1Layout.setVerticalGroup(vGroup);jPanel1.setLayout(jPanel1Layout);jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("可爱小猫"));jPanel2.setLayout(new FlowLayout());          c = new MyCanvas();jPanel2.add(c);       GroupLayout layout = new GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addContainerGap()));layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));pack();} private void jButton_sendActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:JSONObject result = nlp.process(jTextField1.getText());jTextField2.setText(nlp.getAnswer(result));}public static void main(String args[]) {java.awt.EventQueue.invokeLater(new Runnable() {public void run() {MyJFrame frame = new MyJFrame();frame.setTitle("自然语言理解试验小程序——趣味聊天");frame.setLocationRelativeTo(null);frame.setVisible(true);}});}class MyCanvas extends Canvas {private static final long serialVersionUID = 1L;public MyCanvas () {setBackground (Color.GRAY);setSize(300, 320);}public void paint (Graphics g) {Graphics2D g2;g2 = (Graphics2D) g;update(g2);}public void update(Graphics g) {if (actPhase == 1) {g.drawImage(act1, getWidth() / 2- act1.getWidth(null) / 2, getHeight() / 2- act1.getHeight(null) / 2, act1.getWidth(null), act1.getHeight(null), this);} else {g.drawImage(act2, getWidth() / 2- act2.getWidth(null) / 2, getHeight() / 2- act2.getHeight(null) / 2, act2.getWidth(null), act2.getHeight(null), this);}}}}

2、语义理解NLIProcess:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;public class NLIProcess {private static final String url = "https://cn.olami.ai/cloudservice/api";private static final String Appkey = "beaf1c8e85894e1c90b333670d479581";private static final String Appsecret = "52b158f9c8b741218dcf3931136611b6";private static final String api = "nli";    public String getAnswer(JSONObject semantic) {String error = "你刚才说什么,我没有听懂。";if (semantic == null || !"ok".equalsIgnoreCase(semantic.getString("status"))) return error;JSONObject data = semantic.getJSONObject("data");JSONArray s_list = data.getJSONArray("nli");JSONObject s_first = s_list.getJSONObject(0);JSONObject desc_obj = s_first.getJSONObject("desc_obj");int status = desc_obj.getIntValue("status");if (0 == status) {return desc_obj.getString("result");}return error;}public JSONObject process (String input) {JSONObject NLIresult = new JSONObject();List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("appkey", Appkey));params.add(new BasicNameValuePair("api", api));long timestamp = Calendar.getInstance().getTimeInMillis();params.add(new BasicNameValuePair("timestamp", String.valueOf(timestamp)));params.add(new BasicNameValuePair("sign", generateSign(timestamp)));JSONObject request = new JSONObject();JSONObject data = new JSONObject();try {data.put("input_type", 0);data.put("text", input);request.put("data_type", "stt");request.put("data", data);} catch (JSONException e1) {e1.printStackTrace();return NLIresult;}params.add(new BasicNameValuePair("rq", request.toString()));params.add(new BasicNameValuePair("cusid", "asdfghj"));CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost(url);try {httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));CloseableHttpResponse response = httpclient.execute(httppost);try {HttpEntity entity = response.getEntity();if (entity != null) {String contnt = EntityUtils.toString(entity);NLIresult = JSONObject.parseObject(contnt);}} finally {response.close();}} catch (Exception e) {e.printStackTrace();return NLIresult;} finally {try {httpclient.close();} catch (Exception e) {e.printStackTrace();}}return NLIresult;}private String generateSign(long timestamp) {String sign = Appsecret + "api=" + api + "appkey=" + Appkey + "timestamp=" + timestamp + Appsecret;return MD5String(sign);}public String MD5String(String str) {try {MessageDigest msgDigest = MessageDigest.getInstance("MD5");msgDigest.reset();msgDigest.update(str.getBytes("UTF-8"));byte[] byteArrary = msgDigest.digest();StringBuffer md5StrBuff = new StringBuffer();for (int i = 0; i < byteArrary.length; i++) {String tmp = Integer.toHexString(0xFF & byteArrary[i]);if (tmp.length() == 1) {md5StrBuff.append(0).append(tmp);} else {md5StrBuff.append(tmp);}}return md5StrBuff.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();return null;} catch (UnsupportedEncodingException e) {e.printStackTrace();return null;}}   
}
欢迎大家下载这个小程序的可运行jar包!趣味聊天demo.jar

关于其他根据olami平台开发的小程序还有:

Olami智能语义解析—学习娱乐小能手

根据OLAMI平台开发的日历Demo

用olami开放语义平台做汇率换算应用

微信小程序+OLAMI自然语言API接口制作智能查询工具–快递、聊天、日历等

用语义开放平台olami开发翻译应用

热门自然语言理解和语音API开发平台对比

使用OLAMI SDK和讯飞语音合成制作一个语音回复的短信小助手

告诉你如何使用OLAMI自然语言理解开放平台API制作自己的智能对话助手

这篇关于自定义java.awt.Canvas—趣味聊天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1071224

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听