import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.image.*; public class PickNTrackOld extends Frame { static int kWidth ; //The overall size of your video static int kHeight ; static int x = 0; //these are the x and y position of the dot you are drawing static int y = 0; static int redGoal = 210; //these describe the color you are chasing static int greenGoal = 20; //255, 255, 255 would be white static int blueGoal = 20; static PixelSource ps; static ImageWranglerOld iw; static PickNTrackOld myWindow; static Image myImage; static boolean scanning = true; PickNTrackOld() { //this is like startmovie kWidth =320; kHeight =240; } public static void main(String args[]) //this is like exit frame { myWindow = new PickNTrackOld(); myWindow.resize(kWidth,kHeight); myWindow.show(); myWindow.toFront(); myWindow.setLayout (null); //This is the object that give you the video pixels, the last parameter is for dialog box ps = new PixelSource(kWidth,kHeight); ps.videoSettings();//pop up the settings window iw = new ImageWranglerOld(ps.getPixelArray(),ps.vidWidth,kHeight,ps.getMasks()); //add a listener for shutting the window, give it a method to call (thisWindowClosing) myWindow.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { myWindow.thisWindowClosing(e); } }); //add a listener for clicking the mouse in the window, give it a method to call (MouseClicked) myWindow.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { myWindow.MouseClicked(e); } }); //add a listener for pressing a key , give it a method to call (KeyPressed) myWindow.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent e) { myWindow.KeyPressed(e); } }); /////////In every frame (like exit frame)/////// while (scanning) //you may want to farm this out to a thread { LookAtAFrame(); //we look through all the pixels myWindow.repaint(); // we paint the winner } } static void LookAtAFrame() //this is where the action is, where we go pixel by pixel through the video { int[] rgb; int worldRecord = 60000; //some huge number ps.grabFrame(); //grab a frame for (int row = 0; row < kHeight; row++) //REPEAT FOR EACH ROW OF PIXELS { for (int column = 0; column < kWidth; column++) //REPEAT FOR EACH PIXEL IN THE ROW { rgb = ps.getPixel(column,row); ps.setPixel(column,row,255); //set the alpha to opaque int differenceInColor = Math.abs(rgb[0] - redGoal) + Math.abs(rgb[2] - blueGoal) + Math.abs(rgb[1 ] - greenGoal); if (differenceInColor < worldRecord) { x = column; y = row; worldRecord = differenceInColor ; } //System.out.println ("red" + red + " blue" + blue + " green" + green); // } }//END FOR EACH PIXEL IN A ROW }//END FOR EACH ROW OF PIXELS try {Thread.sleep(33);} catch(InterruptedException e) {} myImage = iw.imageFromArray(); } // end of lookat frame public void update(Graphics g){ paint(g); } public void paint(Graphics g) { //this is where we paint if (myImage != null){ g.drawImage(myImage,0,0,this);//the background video g.setColor(new Color(0, 0, 0));//black g.fillOval( x,y,10,10);//the dot for tracking } } public boolean MouseClicked(java.awt.event.MouseEvent evt){ //this repicks the color you are chasing int[] rgb; //find a color to track int x = evt.getX(); int y = evt.getY(); rgb = ps.getPixel(x,y); redGoal = rgb[0]; greenGoal =rgb[1]; blueGoal = rgb[2]; System.out.println ("clicked x" + x + " y" + y + " R" + redGoal + " G" + greenGoal + " B" + blueGoal); return(true); } public boolean KeyPressed(java.awt.event.KeyEvent e){ //pop up dialog //System.out.println ( "whichkey" + e.getKeyText(e.getKeyCode())); String whichKey =e.getKeyText(e.getKeyCode()); if (whichKey.equals("S")) { ps.videoSettings(); } return(true); } void thisWindowClosing(java.awt.event.WindowEvent e) { scanning = false; System.out.println ("quit"); ps.killSession(); dispose(); System.exit(5); } }