⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 face.java

📁 一个非常有意思,并且带图像的语音项目,能读出相应的英文,并有口型
💻 JAVA
字号:
/*******************************************************************************
 * Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Common
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Berthold Daum
 ******************************************************************************/

package com.bdaum.dukeSpeaks;
import java.awt.*;

import javax.swing.JPanel;

import com.sun.speech.freetts.relp.AnimationEvent;
import com.sun.speech.freetts.relp.AnimationListener;

/**
 * @author Berthold Daum
 *
 * created: 1.03.2003 
 */
public class Face extends JPanel implements AnimationListener {

	// Relative mouth dimensions, derived from current phoneme
	float mouthWidth = 0.80f;
	float mouthHeight = 0.05f;
	// Relative eye pupil position, derived from current phoneme
	float eyePos = 0.16f;

	/**
	 * @see com.sun.speech.freetts.relp.AnimationListener#processAnimationEvent(AnimationEvent)
	 */
	public void processAnimationEvent(AnimationEvent e) {
		// Set current phoneme
		String phone = e.phone;
		if (phone.equals("pau")) {
			// In pauses pupils must look upwards
			eyePos = 0.15f;
			// In pauses mouth remains closed
			mouthWidth = 0.80f;
			mouthHeight = 0.05f;
		} else {
			// Otherwise pupils look downwards
			eyePos = 0.4f;
			// Analyze first character of phoneme
			char p1 = phone.charAt(0);
			switch (p1) {
				// Uh's and Oh's
				case 'o' :
				case 'u' :
					mouthWidth = 0.5f;
					mouthHeight = 0.5f;
					break;
					// Ah's
				case 'a' :
					mouthWidth = 0.75f;
					mouthHeight = 0.75f;
					break;
					// Eh's and Ih's
				case 'e' :
				case 'i' :
					mouthWidth = 1f;
					mouthHeight = 0.1f;
					break;
					// Alles andere
				default :
					mouthWidth = 0.6f;
					mouthHeight = 0.3f;
					break;
			}
		}
		repaint();
	}
	
	/**
	 * @see javax.swing.JComponent#paintComponent(Graphics)
	 */
	protected void paintComponent(Graphics cg) {
		super.paintComponent(cg);
		// Cast for Java2D
		Graphics2D g = (Graphics2D) cg;
		// Compute component size
		Dimension d = getSize();
		int width = (int) d.getWidth();
		int height = (int) d.getHeight();
		// Switch off Antialiasing
		g.setRenderingHint(
			RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
		// Draw face
		g.setColor(Color.white);
		g.fillOval(0, 0, width, height);
		// Some face dimensions
		int midX = width / 2;
		int midY = height * 3 / 4;
		int eyeDia = height / 10;
		int eyeInner = eyeDia / 2;
		int eyeY = height / 4;
		int eyeX = midX - eyeDia / 3;
		int eyeOff = width / 6;
		int noseY = height / 3;
		int noseLength = height / 4;
		int noseWidth = width / 12;
		// Draw eyes
		g.setColor(Color.blue);
		g.drawOval(midX - eyeOff - eyeDia / 3, eyeY, eyeDia, eyeDia);
		g.drawOval(midX + eyeOff - eyeDia / 3, eyeY, eyeDia, eyeDia);
		// Draw eye pupils
		int ey = eyeY + ((int) (eyeDia * eyePos));
		g.fillOval(eyeX - eyeOff, ey, eyeInner, eyeInner);
		g.fillOval(eyeX + eyeOff, ey, eyeDia / 2, eyeDia / 2);
		// Draw nose
		g.drawPolyline(
			new int[] { midX, midX + noseWidth, midX },
			new int[] { noseY, noseY + noseLength, noseY + noseLength },
			3);
		// Compute mouth dimensions
		int mw = (int) (width * mouthWidth / 4);
		int mh = (int) (height * mouthHeight / 4);
		int mx = midX - mw / 2;
		int my = midY - mh / 4;
		// Draw mouth
		g.fillOval(mx, my, mw, mh);
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -