Advertisement
Guest User

Untitled

a guest
Jul 16th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main.display;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.EventQueue;
  6. import java.awt.Image;
  7. import java.awt.geom.Point2D;
  8. import java.awt.image.BufferedImage;
  9. import java.io.FileNotFoundException;
  10. import java.io.PrintWriter;
  11. import java.io.UnsupportedEncodingException;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Collections;
  15.  
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.border.EmptyBorder;
  19.  
  20. public class Display extends JFrame {
  21.  
  22.     public static int FRAME_WIDTH = 640;
  23.     public static int FRAME_HEIGHT = 480;
  24.     private JPanel contentPane;
  25.     private static ImgComponent imgComp;
  26.     private static Img image;
  27.  
  28.     /**
  29.      * Launch the application.
  30.      */
  31.     public static void main(String[] args) {
  32.         Display frame = new Display();
  33.         frame.setVisible(true);
  34.         System.out.println("Done");
  35.     }
  36.    
  37.     void process_pixel(PriorityQueue<Pixel> toppixels,Pixel p)
  38.     {
  39.         toppixels.add(p);
  40.         if(toppixels.size() > 32)       //if the heap has more than 32 pixels, remove the least-red pixel)
  41.         {
  42.             toppixels.poll();   //O(log(n) time)
  43.         }
  44.     }
  45.  
  46.     public void getDot(Img i)
  47.     {
  48.         BufferedImage img = image.getBufferedImage();
  49.         PriorityQueue<Pixel> toppixels=new PriorityQueue<Pixel>();//we want a binary heap to keep our top 10 pixels in (or 100)
  50.        
  51.         for( int y=0;y<img.getHeight();y++) //You want to go in row-major order.  It DRAMATICALLY helps with cache
  52.         {
  53.             for( int x=0;x<img.getWidth();x++)
  54.             {
  55.                 int rgb=img.getRGB(x,y);
  56.                 Pixel p=new Pixel(x,y,(rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF,rgb & 0xFF);
  57.                 process_pixel(toppixels,p);
  58.             }
  59.         }
  60.        
  61.         Pixel[] closest=toppixels.toArray();
  62.  
  63.         Point2D.Double centerOfRed = getCenter(closest);
  64.         double x0 = centerOfRed.getX();
  65.         double y0 = centerOfRed.getY();
  66.        
  67.         Cursor.update(x0 * FRAME_WIDTH / image.buffImg.getWidth(), y0
  68.                 * FRAME_HEIGHT / image.buffImg.getHeight(), 5);
  69.  
  70.     }
  71.  
  72.     public Point2D.Double getCenter(Pixel [] dotPixels){
  73.         int x0 = 0;
  74.         for (Pixel p : dotPixels) {
  75.             x0 += p.x;
  76.         }
  77.         x0 /= dotPixels.size();
  78.  
  79.         int y0 = 0;
  80.         for (Pixel p : dotPixels) {
  81.             y0 += p.y;
  82.         }
  83.         y0 /= dotPixels.size();
  84.        
  85.         return new Point2D.Double(x0, y0);
  86.     }
  87.    
  88.     public void writeToFile(String filename, ArrayList<Pixel> data){
  89.         PrintWriter printer;
  90.         try {
  91.             printer = new PrintWriter("resources/" + filename + ".txt");
  92.             for (Pixel p : data) {
  93.                 printer.print(p.toString());
  94.                 //printer.printf("(%d, %d) r:%d g:%d b:%d", p.x, p.y, p.r, p.g, p.b);
  95.                 //printer.println();
  96.             }
  97.             printer.close();        //FORMATTED PRINTING!  O(n)!  OMG!
  98.         }
  99.         catch (FileNotFoundException e) {e.printStackTrace();}
  100.     }
  101.  
  102.     /**
  103.      * Create the frame.
  104.      */
  105.     public Display() {
  106.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107.         setBounds(100, 100, FRAME_WIDTH, FRAME_HEIGHT);
  108.         contentPane = new JPanel();
  109.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  110.         contentPane.setLayout(new BorderLayout(0, 0));
  111.         setContentPane(contentPane);
  112.         image = new Img(
  113.                 "D:\\Programing\\Java\\Eclipse\\projects\\LaserDistanceCalc\\src\\main\\display\\image3.jpeg");
  114.         imgComp = new ImgComponent(image);
  115.         contentPane.add(imgComp);
  116.         try {
  117.             getDot(image);
  118.         } catch (FileNotFoundException e) {
  119.             e.printStackTrace();
  120.         } catch (UnsupportedEncodingException e) {
  121.             e.printStackTrace();
  122.         }
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement