JLayeredPane类及其在视频缓冲界面显示的应用

作者:金良([email protected]) csdn博客:http://blog.csdn.net/u012176591

JLayeredPane类及其在视频缓冲界面显示的应用_第1张图片

JLayeredPane简介

JLayeredPane实现了界面的重叠显示:
JLayeredPane类及其在视频缓冲界面显示的应用_第2张图片

代码如下:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class MyJLayeredPanel extends JFrame {
	public MyJLayeredPanel() {
		super("MyLayeredPanel");
		Integer[] layerConstants = {
				JLayeredPane.DEFAULT_LAYER,JLayeredPane.PALETTE_LAYER
		};
		Color[] colors = {
				Color.red,Color.blue
		};
		

		//////////////////////
		Point[] position = {new Point(10,10),new Point(-100,-100)};
		JLabel label1,label2;
		JLayeredPane layeredPane = getLayeredPane();
		label1 = createLabel("第"+1+"层",colors[0],position[0],false);
		label2 = createLabel("第"+2+"层",colors[1],position[1],true);
		Image imageBg = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/images/Lighthouse.jpg")); 
		layeredPane.add(label1,layerConstants);
		layeredPane.add(label2,layerConstants);
		layeredPane.setVisible(true);
		label1.setVisible(true);
		//setSize(new Dimension(280,280));
		show();
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	public JLabel createLabel(String content,Color color,Point position,boolean isBig) {
		JLabel label = new JLabel(content,JLabel.CENTER);
		label.setVerticalAlignment(JLabel.TOP);
		label.setBackground(color);
		label.setForeground(Color.black);
		label.setOpaque(true);
		if (isBig == true) {
			label.setBounds(position.x, position.y, 400, 400);
		} else {
			label.setBounds(position.x, position.y, 100, 100);
		}
		return label;
	}
	public static void main(String [] args) {
		new MyJLayeredPanel();
	}
}

JLayeredPane用于视频缓冲界面的显示

视频缓冲前:
JLayeredPane类及其在视频缓冲界面显示的应用_第3张图片

视频缓冲:
JLayeredPane类及其在视频缓冲界面显示的应用_第4张图片

实现代码:
package mediaplayer.myMediaPlayer;
/**
 * @author jin
 */

import javax.media.*; 
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*; 
import java.util.Timer;
import java.util.TimerTask;

class MediaPlayer extends Frame implements ActionListener,ControllerListener { 
	Player player; 
	Graphics g;
	Component videoComponent, controlComponent; 
	JLabel labelGif;
	boolean isHelloUI = true;
	String currentDirectory;
	
    private int videoWidth = 0;
    private int videoHeight = 0;
    private int controlHeight = 30;
    private int insetWidth = 10;
    private int insetHeight = 100;
	
	int TimeBeginPause = 5;
	int TimePause = 10;
	
	MediaPlayer (String title) 
	{ 
		super (title); 
		addWindowListener(new WindowAdapter () { 
			public void windowClosing (WindowEvent e) {
				dispose (); 
			} 
			public void windowClosed (WindowEvent e){ 
				if (player != null) 
					player.close (); 
				System.exit (0); 
			} 
		}); 
		Menu menu = new Menu ("File"); 
		MenuItem menuItem = new MenuItem ("Open"); 
		menuItem.addActionListener (this); 
		menu.add (menuItem); 
		menuItem = new MenuItem ("Exit"); 
		menuItem.addActionListener (this); 
		menu.add (menuItem); 
		MenuBar menuBar = new MenuBar (); 
		menuBar.add (menu); 
		setMenuBar (menuBar); 
		setSize (800, 800); 
		setVisible (true); 
	} 
	public void actionPerformed (ActionEvent e) { 
		if (e.getActionCommand ().equals ("Exit")) { 
			dispose (); 
			return; 
		} 
		FileDialog fileDialog = new FileDialog (this, "Open", FileDialog.LOAD); 
		fileDialog.setDirectory (currentDirectory); 
		fileDialog.show (); 
		if (fileDialog.getFile () == null) 
			return; 
		currentDirectory = fileDialog.getDirectory (); 
		if (player != null) 
			player.close (); 
		try { 
			player = Manager.createPlayer (new MediaLocator ("file:" + fileDialog.getDirectory () + fileDialog.getFile ())); 
		} catch (java.io.IOException e2) { 
			System.out.println (e2); 
			return; 
		} catch (NoPlayerException e2) { 
			System.out.println ("Cannot find video player."); 
			return; 
		} 
		if (player == null) { 
			System.out.println ("Cannot create video player."); 
			return; 
		} 
		isHelloUI = false; 
		setTitle ("正在播放"+fileDialog.getFile ()+"..."); 
		player.addControllerListener (this); 
		player.prefetch (); 
		new Timer().schedule(new Pause(),TimeBeginPause*1000);
	} 
	public void controllerUpdate (ControllerEvent e) { 
		if (e instanceof ControllerClosedEvent) {
			if (videoComponent != null) { 
				remove (videoComponent); 
				videoComponent = null; 
			} 
			if (controlComponent != null) { 
				remove (controlComponent); 
				controlComponent = null; 
			} 
			return; 
		} 
		if (e instanceof EndOfMediaEvent) { 
			return; 
		} 
		if (e instanceof PrefetchCompleteEvent) { 
			player.start (); 
			return; 
		} 
		if (e instanceof RealizeCompleteEvent) {
			controlComponent = player.getControlPanelComponent (); 
			videoComponent = player.getVisualComponent (); 
			if (videoComponent != null) {
				
                Dimension size = videoComponent.getPreferredSize();
                videoWidth = size.width;
                videoHeight = size.height;
                setSize(videoWidth + insetWidth, videoHeight + controlHeight + insetHeight);
                
				JLayeredPane layeredPane = new JLayeredPane();
					
				Point origin = new Point(10, 30);
				Dimension dimension = new Dimension(300, 310);
				
				//Dimension dimension = new Dimension(300*3, 310*3);
				Rectangle rectangle = new Rectangle(origin, dimension);
				
				Point origin2 = new Point(100, 130);
				Dimension dimension2 = new Dimension(100, 110);				
				Rectangle rectangle2 = new Rectangle(origin2, dimension2);
				
				Dimension dimension3 = new Dimension(500,600);
				
				//layeredPane.setPreferredSize(dimension3);
				
				layeredPane.setSize(500,600);
				
				Image imageGif = Toolkit.getDefaultToolkit()
						.getImage(this.getClass().getResource("/resources/bufferT2.gif"));
				ImageIcon gifBuffer = new ImageIcon(imageGif.getScaledInstance
						(dimension.width-200, dimension.height-200, Image.SCALE_FAST));

				labelGif = new JLabel(gifBuffer);
				labelGif.setBounds(rectangle2);
				labelGif.setVisible(false);
				
				JPanel panelVideo = new JPanel();
				panelVideo.setBounds(rectangle);
				panelVideo.add (videoComponent);
				
				layeredPane.add(labelGif, new Integer(1));
				layeredPane.add(panelVideo, new Integer(0));
				
				layeredPane.setBackground(Color.red);
				add(layeredPane);
			}
			if (controlComponent != null) 
				add (controlComponent, BorderLayout.SOUTH); 
			validate();
		} 
	} 

	public void paint (Graphics g) { 
		if (isHelloUI) {
			int w = getSize ().width; 
			int h = getSize ().height;
			String strHello = "*** 大家好***";
			g.setColor (Color.yellow); 
			g.fillRect (0, 0, w, h); 
			Font f = new Font ("DialogInput", Font.BOLD, 50); 
			g.setFont (f); 
			FontMetrics fm = g.getFontMetrics (); 
			int swidth = fm.stringWidth (strHello); 
			g.setColor (Color.red); 
			g.drawString (strHello, (w - swidth) / 2, (h + getInsets ().top) / 2);
		} 
		super.paint (g);
	} 

	public void update (Graphics g) { 
		paint (g); 
	} 

	public static void main (String [] args) { 
		new MediaPlayer ("视频演示"); 
	}	
    class Pause extends TimerTask {  
        public void run(){ 
        	//panelBufferring.setVisible(true);
        	labelGif.setVisible(true);
            player.stop();
            new Timer().schedule(new Begin(),TimePause*1000);
        }  
    } 
    class Begin extends TimerTask {  
        public void run(){
        	//panelBufferring.setVisible(false);
        	labelGif.setVisible(false);
            player.start();
        }  
    } 
} 


相关代码下载


你可能感兴趣的:(java,JLayeredPane,视频缓冲界面)