java - JFrame acting strangely. Sporadically displays JLabel -
its hard explain whipped out phone , video'd problem i'm having:
http://www.youtube.com/watch?v=kdoende8w2q
simply put, image (border @ bottom can see, implemented jlabel) should remain there @ times, instead appears 1 second, disappears , flashes sporadically.
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); } }
in addition, if matters:
- using https://github.com/sarxos/webcam-capture webcam library.
- using windows 8 64bit java 32bit (need things work)
any ideas on potential causes of problem?
edit (by andrew thompson)
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 important bit byte[] b1 = baos.tobytearray(); byte[] b2 = new byte[b1.length/playbackspeed]; (int ii=0; ii<b2.length/framesize; ii++) { (int jj=0; jj<framesize; jj++) { int b3=0; (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?"); } }
swapped using jlayeredpane.
workflow essentially:
- create jframe
- create jlayeredpane
- add jlayeredpane jframe
- create jpanels (as needed, have three, 1 webcam background, , 2 foreground layers buttons etc)
- add jpanels jlayeredpane. set z-index/order add them.
complete example: http://www.roseindia.net/java/example/java/swing/jlayered-overlap-panel.shtml
Comments
Post a Comment