0

很难解释,所以我拿出我的手机并录下了我遇到的问题:

http://www.youtube.com/watch?v=KdOeNdE8W2Q

简单地说,图像(您可以看到底部的边框,实现为 JLabel)应该始终保留在那里,而是出现 1 秒钟,然后消失,然后偶尔闪烁。

import java.awt.Button;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.imgscalr.Scalr;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;

public class WebcamPanelExample extends JFrame {
    
    static Webcam webcam;
    static JFrame window;
    

    public static void main(String[] args) throws IOException {
        
        window = new JFrame("Test webcam panel");
        
        Dimension[] nonStandardResolutions = new Dimension[] {
                WebcamResolution.HD720.getSize(),
                };

        webcam = Webcam.getDefault();
        webcam.setCustomViewSizes(nonStandardResolutions);
        webcam.setViewSize(WebcamResolution.HD720.getSize());
        
        WebcamPanel panel = new WebcamPanel(webcam);
        panel.setFPSLimited(false); 
        panel.setFillArea(true); 
        window.add(panel);

        ////////////

        BufferedImage image2 = ImageIO.read(new File("F:/DATA/images/image_area.png"));
        JLabel guiBorder = new JLabel(new ImageIcon( image2 ));
        guiBorder.setSize(new Dimension(1280,720));
        guiBorder.setLocation(0, 0);
        window.add( guiBorder );
        
        ///////////
        
        Button button = new Button("Do Something");
        button.setSize(new Dimension(200,32));
        button.setLocation(540, 670);
        window.add(button);
        
        ///////////
        
        window.setComponentZOrder(button, 0);
        window.setComponentZOrder(guiBorder, 1);
        window.setComponentZOrder(panel, 2);
        
        ///////////
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(null);
        window.validate();
        window.setSize(1280,720);
        window.setResizable(false);
        window.setVisible(true);

    }
}

此外,如果它很重要:

关于问题的潜在原因的任何想法?

编辑(安德鲁·汤普森)

 import javax.swing.JOptionPane;
 import javax.swing.JFileChooser;
 import javax.sound.sampled.*;
 import java.net.URL;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
 import java.util.Date;
 import java.io.File;

 class AcceleratePlayback {

     public static void main(String[] args) throws Exception {
         int playBackSpeed = 3;
     File soundFile;
         if (args.length>0) {
             try {
                 playBackSpeed = Integer.parseInt(args[0]);
             } catch (Exception e) {
                 e.printStackTrace();
                 System.exit(1);
             }
         }
         System.out.println("Playback Rate: " + playBackSpeed);

         JFileChooser chooser = new JFileChooser();
         chooser.showOpenDialog(null);
         soundFile = chooser.getSelectedFile();

         System.out.println("FILE: " + soundFile);
         AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
         AudioFormat af = ais.getFormat();

         int frameSize = af.getFrameSize();

         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte[] b = new byte[2^16];
         int read = 1;
         while( read>-1 ) {
             read = ais.read(b);
             if (read>0) {
                 baos.write(b, 0, read);
             }
         }
         System.out.println("End entire: \t" + new Date());

        //This is the important bit

         byte[] b1 = baos.toByteArray();
         byte[] b2 = new byte[b1.length/playBackSpeed];
         for (int ii=0; ii<b2.length/frameSize; ii++) {
             for (int jj=0; jj<frameSize; jj++) {
                     int b3=0;  
             for (int kk = 0; kk < playBackSpeed; kk++){
              b3 = b3+(int)b1[(ii*frameSize*playBackSpeed)+jj+kk];  
             }
             b3 = b3/playBackSpeed;
             b2[(ii*frameSize)+jj] = (byte)b3;
             }
         }
        //ends here

         System.out.println("End sub-sample: \t" + new Date());

         ByteArrayInputStream bais = new ByteArrayInputStream(b2);
         AudioInputStream aisAccelerated = new AudioInputStream(bais, af, b2.length);
         Clip clip = AudioSystem.getClip();
         clip.open(aisAccelerated);
         clip.loop(2*playBackSpeed);
         clip.start();

         JOptionPane.showMessageDialog(null, "Exit?");
     }
}
4

1 回答 1

0

切换到使用 JLayeredPane。

工作流程本质上是:

  1. 创建 JFrame
  2. 创建 JLayeredPane
  3. 将 JLayeredPane 添加到 JFrame
  4. 创建 JPanel(根据需要,我有三个,一个用于网络摄像头背景,两个用于按钮等前景层)
  5. 将您的 JPanel 添加到 JLayeredPane。您在添加它们时设置 z-index/order。

完整示例: http ://www.roseindia.net/java/example/java/swing/jlayered-overlap-panel.shtml

于 2013-05-06T11:46:38.727 回答