Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main.display;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.EventQueue;
- import java.awt.Image;
- import java.awt.geom.Point2D;
- import java.awt.image.BufferedImage;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- public class Display extends JFrame {
- public static int FRAME_WIDTH = 640;
- public static int FRAME_HEIGHT = 480;
- private JPanel contentPane;
- private static ImgComponent imgComp;
- private static Img image;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- Display frame = new Display();
- frame.setVisible(true);
- System.out.println("Done");
- }
- void process_pixel(PriorityQueue<Pixel> toppixels,Pixel p)
- {
- toppixels.add(p);
- if(toppixels.size() > 32) //if the heap has more than 32 pixels, remove the least-red pixel)
- {
- toppixels.poll(); //O(log(n) time)
- }
- }
- public void getDot(Img i)
- {
- BufferedImage img = image.getBufferedImage();
- PriorityQueue<Pixel> toppixels=new PriorityQueue<Pixel>();//we want a binary heap to keep our top 10 pixels in (or 100)
- for( int y=0;y<img.getHeight();y++) //You want to go in row-major order. It DRAMATICALLY helps with cache
- {
- for( int x=0;x<img.getWidth();x++)
- {
- int rgb=img.getRGB(x,y);
- Pixel p=new Pixel(x,y,(rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF,rgb & 0xFF);
- process_pixel(toppixels,p);
- }
- }
- Pixel[] closest=toppixels.toArray();
- Point2D.Double centerOfRed = getCenter(closest);
- double x0 = centerOfRed.getX();
- double y0 = centerOfRed.getY();
- Cursor.update(x0 * FRAME_WIDTH / image.buffImg.getWidth(), y0
- * FRAME_HEIGHT / image.buffImg.getHeight(), 5);
- }
- public Point2D.Double getCenter(Pixel [] dotPixels){
- int x0 = 0;
- for (Pixel p : dotPixels) {
- x0 += p.x;
- }
- x0 /= dotPixels.size();
- int y0 = 0;
- for (Pixel p : dotPixels) {
- y0 += p.y;
- }
- y0 /= dotPixels.size();
- return new Point2D.Double(x0, y0);
- }
- public void writeToFile(String filename, ArrayList<Pixel> data){
- PrintWriter printer;
- try {
- printer = new PrintWriter("resources/" + filename + ".txt");
- for (Pixel p : data) {
- printer.print(p.toString());
- //printer.printf("(%d, %d) r:%d g:%d b:%d", p.x, p.y, p.r, p.g, p.b);
- //printer.println();
- }
- printer.close(); //FORMATTED PRINTING! O(n)! OMG!
- }
- catch (FileNotFoundException e) {e.printStackTrace();}
- }
- /**
- * Create the frame.
- */
- public Display() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, FRAME_WIDTH, FRAME_HEIGHT);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- image = new Img(
- "D:\\Programing\\Java\\Eclipse\\projects\\LaserDistanceCalc\\src\\main\\display\\image3.jpeg");
- imgComp = new ImgComponent(image);
- contentPane.add(imgComp);
- try {
- getDot(image);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement