Remember stolen code

Eclipse written using XML file

Can greatly improve system scalability through the configuration file so that the software entity more in line with the principle of opening and closing. To make the system more in line with the principle of opening and closing and the Dependency Inversion Principle, need to be "abstract written in code, written in the configuration will be specifically" to improve the system by modifying the configuration files without having to compile the scalability and flexibility 1. XML file config.xml

2. Write a java program (XMLUtilTV.java) reads the XML information in this tool class, provided by the Java language DOM (Document Object Model, Document Object Model) API to implement operations on XML documents, in the DOM API in XML document tree structure stored in memory, can be read, modified and other operations related classes through XML

import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class XMLUtilTV {
	//该方法用于从XML配置文件中提取品牌名称,并返回该品牌名称
	public static String getBrandName() {
		try {
			//创建文档对象
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dFactory.newDocumentBuilder();
			Document doc;
			doc = builder.parse(new File("E:\\Java\\设计模式\\简单工厂模式例题\\src\\com\\practice\\SimpleFactory\\config.xml"));
			
			//获取包含品牌名称的文本节点
			NodeList nl = doc.getElementsByTagName("brandName");
			Node classNode = nl.item(0).getFirstChild();
			String brandName = classNode.getNodeValue().trim();
			return brandName;
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

3.Client

Java program gets to play audio

1. Obtain the specified audio format

package com.Practice.time;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *  音频播放
 */
public class AudioPlay extends Thread {
	private String fileName; 						// 播放音乐的文件名
	private Position curPosition; 					// 声道
	private final int EXTERNAL_BUFFER_SIZE = 524288; // 128k

	enum Position { // 声道
		LEFT, RIGHT, NORMAL
	};

	// 构造函数
	public AudioPlay(String wavFile) {
		this.fileName = wavFile;
		curPosition = Position.NORMAL;
	}

	public void run() {
		File soundFile = new File(fileName); // 播放音乐的文件名
		if (!soundFile.exists()) {
			System.err.println("音频文件未找到: " + fileName);
			return;
		}

		AudioInputStream audioInputStream = null; // 创建音频输入流对象
		try {
			audioInputStream = AudioSystem.getAudioInputStream(soundFile); // 创建音频对象
		} catch (UnsupportedAudioFileException e1) {
			e1.printStackTrace();
			return;
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}

		AudioFormat format = audioInputStream.getFormat(); 	// 音频格式
		SourceDataLine auline = null; 						// 源数据线
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

		try {
			auline = (SourceDataLine) AudioSystem.getLine(info);
			auline.open(format);
		} catch (LineUnavailableException e) {
			e.printStackTrace();
			return;
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		if (auline.isControlSupported(FloatControl.Type.PAN)) {
			FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
			if (curPosition == Position.RIGHT)
				pan.setValue(1.0f);
			else if (curPosition == Position.LEFT)
				pan.setValue(-1.0f);
		}

		auline.start();
		int nBytesRead = 0;
		byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

		try {
			while (nBytesRead != -1) {
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				if (nBytesRead >= 0)
					auline.write(abData, 0, nBytesRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} finally {
			auline.drain();
			auline.close();
		}
	}

}

2. Play Audio new AudioPlay ( "src // 7987.wav") start ().;

Java Timer program

Timer timer = new Timer();
//8s执行程序结束后每1s 后开始执行下一次程序
 timer.schedule(new TimerTask() {    	     
    public void run() {
  	 //TestNTP Time = new TestNTP();
  	 //Date now = Time.getDate();
  	 //setDate(now);
    }
 },8*1000,1*1000);

java get running process ID

		ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
		int noThreads = currentGroup.activeCount();
		Thread[] lstThreads = new Thread[noThreads];
		currentGroup.enumerate(lstThreads);
 
		for (int i = 0; i < noThreads; i++) {
			System.out.println(lstThreads[i].getName());
		} 
	}

JavaGUI set the background image in JPanel

public NewPanel(int mode)
{
	this.mode = mode;
}
 
public void paintComponent(Graphics g)
{
	int x=0,y=0;
	java.net.URL imgURL = null;
	if(mode == 0) {
	    imgURL=getClass().getResource("beijing2.jpg");
        }else if(mode == 1) {
	    imgURL=getClass().getResource("背景1.jpg");
	}
			
	ImageIcon icon=new ImageIcon(imgURL);
	g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
	while(true)
	{
	    g.drawImage(icon.getImage(),x,y,this);
	    if(x>getSize().width && y>getSize().height)break;
	    //这段代码是为了保证在窗口大于图片时,图片仍能覆盖整个窗口
	    if(x>getSize().width)
	    {
		x=0;
		y+=icon.getIconHeight();
	    }
	    else
		x+=icon.getIconWidth();
		}
            }
	}
	
	public void showworkModule() {
                ……
		NewPanel ClockPanel = new NewPanel(0);
		NewPanel titlePanel = new NewPanel(1);
                ……
        }

Guess you like

Origin www.cnblogs.com/miaowulj/p/12132284.html