📄 mainframe.java
字号:
import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import java.net.InetAddress;import javax.media.*;import javax.swing.*;import javax.swing.border.*;public class MainFrame extends Frame{ MainPlayer mainPlayer = null; // 播放器类对象 RTPTransmit rtpTransmit = null; // RTP传输类对象 Label labelTopLeft = new Label(); Label labelX = new Label(); TextField textFieldX = new TextField(); // 截图起点的x坐标编辑框 Label labelY = new Label(); TextField textFieldY = new TextField(); // 截图起点的y坐标编辑框 Label labelArea = new Label(); CheckboxGroup checkboxGroupSize = new CheckboxGroup(); Checkbox checkboxSize1 = new Checkbox(); // 图像尺寸大小单选按钮 Checkbox checkboxSize2 = new Checkbox(); Checkbox checkboxSize3 = new Checkbox(); Label labelFrameRate = new Label(); TextField textFieldFrameRate = new TextField(); // 每秒采样帧数编辑框 JLabel jLabel1 = new JLabel(); Label labelIP = new Label(); TextField textIPAdd1 = new TextField(); // IP地址编辑框 TextField textIPAdd4 = new TextField(); TextField textIPAdd3 = new TextField(); TextField textIPAdd2 = new TextField(); Label labelPort = new Label(); TextField textPort = new TextField(); // 端口号编辑框 JLabel jLabel2 = new JLabel(); Button buttonBeginTransmit = new Button(); // “传输”按钮 Button buttonStopTransmit = new Button(); // “停止”按钮 // 设置界面和添加事件的监听 private void jbInit() throws Exception { this.setLayout(null); this.setBackground(Color.lightGray); labelTopLeft.setText("截图起点坐标:"); labelTopLeft.setBounds(new Rectangle(42, 70, 86, 20)); labelX.setText("X:"); labelX.setBounds(new Rectangle(130, 55, 20, 20)); textFieldX.setBounds(new Rectangle(155, 55, 40, 20)); textFieldX.setText("20"); // 缺省的截图起点横坐标为:20 labelY.setText("Y:"); labelY.setBounds(new Rectangle(130, 85, 20, 20)); textFieldY.setBounds(new Rectangle(155, 85, 40, 20)); textFieldY.setText("20"); // 缺省的截图起点纵坐标为:20 labelArea.setText("图像大小:"); labelArea.setBounds(new Rectangle(210, 70, 60, 20)); checkboxSize1.setLabel("352×288"); checkboxSize1.setBounds(new Rectangle(275, 50, 80, 20)); checkboxSize1.setCheckboxGroup(checkboxGroupSize); checkboxSize2.setLabel("176×144"); checkboxSize2.setBounds(new Rectangle(275, 70, 80, 20)); checkboxSize2.setCheckboxGroup(checkboxGroupSize); checkboxSize3.setLabel("128×96"); checkboxSize3.setBounds(new Rectangle(275, 90, 80, 20)); checkboxSize3.setCheckboxGroup(checkboxGroupSize); checkboxGroupSize.setSelectedCheckbox(checkboxSize1); labelFrameRate.setText("每秒采样帧数(1-30):"); labelFrameRate.setBounds(new Rectangle(42, 125, 136, 20)); textFieldFrameRate.setBounds(new Rectangle(200, 125, 138, 20)); textFieldFrameRate.setText("10"); // 缺省的每秒截图数为:10帧 jLabel1.setBorder(BorderFactory.createEtchedBorder()); jLabel1.setBounds(new Rectangle(22, 30, 338, 138)); labelIP.setText("IP地址:"); labelIP.setBounds(new Rectangle(42, 206, 52, 20)); textIPAdd1.setBounds(new Rectangle(130, 206, 40, 20)); textIPAdd2.setBounds(new Rectangle(186, 206, 40, 20)); textIPAdd3.setBounds(new Rectangle(242, 206, 40, 20)); textIPAdd4.setBounds(new Rectangle(298, 206, 40, 20)); labelPort.setText("端口号:"); labelPort.setBounds(new Rectangle(42, 244, 52, 20)); textPort.setBounds(new Rectangle(130, 244, 40, 20)); jLabel2.setBorder(BorderFactory.createEtchedBorder()); jLabel2.setBounds(new Rectangle(22, 186, 338, 96)); buttonBeginTransmit.setLabel("传输"); buttonBeginTransmit.setBounds(new Rectangle(104, 300, 60, 20)); buttonBeginTransmit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonBeginTransmit_actionPerformed(e); } }); buttonStopTransmit.setLabel("停止"); buttonStopTransmit.setBounds(new Rectangle(226, 300, 60, 20)); buttonStopTransmit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonStopTransmit_actionPerformed(e); } }); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); this.add(labelTopLeft, null); this.add(labelX, null); this.add(textFieldX, null); this.add(labelY, null); this.add(textFieldY, null); this.add(labelArea, null); this.add(checkboxSize1, null); this.add(checkboxSize2, null); this.add(checkboxSize3, null); this.add(labelFrameRate, null); this.add(textFieldFrameRate, null); this.add(jLabel1, null); this.add(labelIP, null); this.add(textIPAdd1, null); this.add(textIPAdd2, null); this.add(textIPAdd3, null); this.add(textIPAdd4, null); this.add(labelPort, null); this.add(textPort, null); this.add(jLabel2, null); this.add(buttonBeginTransmit, null); this.add(buttonStopTransmit, null); this.setSize(new Dimension(381, 335)); this.setTitle("Picture Capture and Transmit"); this.setVisible(true); } // 构造函数 public MainFrame() { try { jbInit(); // 显示主界面 } catch(Exception e) { e.printStackTrace(); } } // 得到用户所设定的截图起点 Point getLocationPicture() { int left = Integer.parseInt(textFieldX.getText()); int top = Integer.parseInt(textFieldY.getText()); return new Point(left,top); } // 得到用户所设定的截图尺寸 Dimension getSizePicture() { Dimension size = null; if(checkboxGroupSize.getSelectedCheckbox() == checkboxSize1) { size = new Dimension(352,288); } else if(checkboxGroupSize.getSelectedCheckbox() == checkboxSize2) { size = new Dimension(176,144); } else { size = new Dimension(128,96); } return size; } // 判断输入的截图开始点是否合适 boolean IsRangeValid() { boolean bValid = true; Point point = getLocationPicture(); Dimension dim = getSizePicture(); Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize(); // 得到屏幕尺寸 if ( point.x + dim.width > dimScreen.width ) { // 限制截图区域在屏幕宽度之内 System.err.println("the X must be in the range of [0," + (dimScreen.width - dim.width) + "]"); bValid = false; } if ( point.y + dim.height > dimScreen.height ) { // 限制截图区域在屏幕高度之内 System.err.println("the Y must be in the range of [0," + (dimScreen.height - dim.height) + "]"); bValid = false; } return bValid; } // 响应鼠标点击“停止”按钮的消息 void buttonBeginTransmit_actionPerformed(ActionEvent e) { if(!IsRangeValid()) // 判断输入的截图开始点是否合适 return; String left = textFieldX.getText(); String top = textFieldY.getText(); Dimension sizePicture = getSizePicture(); String width = new Integer(sizePicture.width).toString(); String height = new Integer(sizePicture.height).toString(); String frameRate = textFieldFrameRate.getText(); String strMedia = "screen://"+left+","+top+","+width+","+height+"/"+frameRate; System.err.println(strMedia); mainPlayer = createNewFrame (); // 产生一个新的播放窗口 mainPlayer.open ( strMedia ); // 打开播放窗口开始播放 MediaLocator mediaLoc = new MediaLocator ( strMedia ); // 产生一个媒体定位器 String strIPAddr = textIPAdd1.getText()+"."+textIPAdd2.getText()+"."+textIPAdd3.getText()+"."+textIPAdd4.getText(); // 得到完整的IP地址 String strPort = textPort.getText(); // 得到端口号 Format fmt = null; rtpTransmit = new RTPTransmit(mediaLoc,strIPAddr, strPort, fmt); String result = rtpTransmit.start(); // 开始RTP传输 if (result != null) { // 如果不能成功开始传输,则输出错误原因 System.err.println("Error : " + result); } else { System.err.println("Start transmission ..."); } } // 响应鼠标点击“停止”按钮的消息 void buttonStopTransmit_actionPerformed(ActionEvent e) { if(rtpTransmit == null) return; rtpTransmit.stop(); // 停止传输 mainPlayer.killCurrentPlayer(); // 删除当前的播放窗口 System.out.println("...transmission ended."); } // 产生一个新的播放窗口 public MainPlayer createNewFrame () { MainPlayer mainPlayer = new MainPlayer (); mainPlayer.setVisible ( true ); // 显示播放窗口 mainPlayer.invalidate (); mainPlayer.pack (); return ( mainPlayer ); } // 主函数 public static void main(String [] args) { new MainFrame(); } // 关闭窗口,退出程序 void this_windowClosing(WindowEvent e) { System.exit(0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -