副标题#e#
媒介
本文通过Java的Runtime接口来实现挪用其他语言实现的应用措施,进而来实现对计较机硬件信息的监控和节制.本文是多媒体信息系统的一个部门,就是调解计较机音量。
利用VC编写音量节制措施
本节制示例利用VC6.0编写,主要是挪用系统的API来实现,
mixerGetLineInfo 获取Master Volume Control.
mixerGetControlDetails 获取 Volume Control 信息
mixerSetControlDetails 配置 Volume Control 信息
最终编译成Console范例的Dos执行的措施VolumeControl.exe。这个措施实现三个成果:
1.获取音量 VolumeControl.exe 0
2.增加音量 VolumeControl.exe 1
3.淘汰音量 VolumeControl.exe 2
#p#副标题#e#
下面我们用Java设计建设一个Panel用于显示音量并挪用应用措施实现对音量的实际节制,本例中利用自界说Progress显示VolumeTracker.java
实现道理如下:
利用一个线程动态刷新页面,主线程用来实现对音量的节制.其实现代码如下:
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.text.AttributedString;
import java.text.AttributedCharacterIterator;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
public class VolumeTracker extends JPanel implements Runnable
{
String welcomeStr = "Welcome to Java Sound";
Thread pbThread;
Color background = Color.white;
//new Color(20, 20, 20);
Color jfcBlue = Color.blue;
//new Color(204, 204, 255);
Color jfcDarkBlue = jfcBlue.darker();
Font font24 = new Font("serif", Font.BOLD, 24);
Font font28 = new Font("serif", Font.BOLD, 28);
Font font42 = new Font("serif", Font.BOLD, 42);
FontMetrics fm28, fm42;
String errStr=null;
String currentName=null;
double duration = 100.0;
double seconds = 82.0;
boolean midiEOM, audioEOM;
public VolumeTracker()
{
fm28 = getFontMetrics(font28);
fm42 = getFontMetrics(font42);
initVolume();
start();
}
private void initVolume()
{
try
{
//这一段小措施实现对VC建设措施的挪用
Runtime rt = Runtime.getRuntime(); //Time and Date.
//mngPathTool类,提供了一个获取当前路径的要领
mngPathTool tool = new mngPathTool();
String sexec = tool.getCurPath()+ "\\binex\\VolumeControl.exe 0";
Process child = rt.exec(sexec);
//获取节制台输出的内容,进而得到音量的巨细
InputStreamReader reader = new InputStreamReader(child.getInputStream());
char[] chr = new char[5];
reader.read(chr) ;
String s="";
for(int i=0;i<5;i++)
{
if(chr[i]>='0' && chr[i]<='9') s+=chr[i];
}
//System.out.println(s);
Integer nVolume = new Integer(s);
seconds = nVolume.intValue();
child.waitFor();
//这一段小措施实现对VC建设措施的挪用
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
public void paint(Graphics g)
{
//绘图来实现百分比Tracker
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
g2.setBackground(background);
g2.clearRect(0, 0, d.width, d.height);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(jfcBlue);
double tseconds = duration-seconds;
if (tseconds > 0.0)
{
int num = 20;
int progress = (int) (tseconds / duration * num);
double hh = ((double) (d.height - 4) / (double) num);
double ww = (int)(d.width-4);
double x = 0.0;
for ( ; x < progress; x+=1.0)
{
g2.fill(new Rectangle2D.Double(d.width-ww-2, x*hh+2, ww, hh));
g2.fill3DRect((int)(d.width-ww-2),(int) (x*hh+2),(int) ww, (int)hh,true);
}
g2.setColor(jfcDarkBlue);
for ( ; x < num; x+=1.0)
{
g2.fill(new Rectangle2D.Double(d.width-ww-2, x*hh+2, ww, hh));
g2.fill3DRect((int)(d.width-ww-2),(int) (x*hh+2),(int) ww, (int)hh,true);
}
}
}
public void start()
{
pbThread = new Thread(this);
pbThread.setName("PlaybackMonitor");
pbThread.start();
}
public void stop()
{
if (pbThread != null)
{
pbThread.interrupt();
}
pbThread = null;
}
public void run()
{
while (pbThread != null)
{
try
{
pbThread.sleep(99);
}
catch (Exception e)
{
break;
}
repaint();
}
pbThread = null;
}
public void addVolume()
{
changeVolume(false);
initVolume();
}
public void minusVolume()
{
changeVolume(true);
initVolume();
}
//control sound volume.
private void changeVolume(boolean bIsMinus)
{
try
{
Runtime rt = Runtime.getRuntime();
//Sound Control mngPathTool
tool = new mngPathTool();
String sexec;
if(bIsMinus)
sexec= tool.getCurPath()+ "\\binex\\VolumeControl.exe 2";
else
sexec= tool.getCurPath()+ "\\binex\\VolumeControl.exe 1";
rt.exec(sexec);
}catch(Exception e1){e1.printStackTrace(); }
}
}
// End VolumeTracker
建设一个JFrame用于显示 VolumeControl.java
配置页面配景,建设显示上述Panel的容器.
建设对话框用于弹出显示音量节制界面 JVolumeDlg.java
建设一个对话框来显示上个步调生成的Frame,并提供事件节制容器.
事件流向—> JVolumeDlg — VolumeControl — VolumeTracker
总结
#p#分页标题#e#
Java Runtime 接口提供了挪用其他应用措施的接口,通过这个接口,可以实现对计较机硬件的节制和监控. 同时通过界面线程可以实现相对巨大的应用措施界面的开拓。
