Guest User

Untitled

a guest
Feb 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.25 KB | None | 0 0
  1. package hs.algo.excercise2;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import java.awt.image.PixelGrabber;
  7.  
  8. @SuppressWarnings("serial")
  9. public class Algos2 extends Frame {
  10.    
  11.     private int W;
  12.     private int H;
  13.     private Image imgFile;
  14.     private int[] imgFilePixels, imgColors, imgColorFreq;
  15.    
  16.     @Override
  17.     public void paint(Graphics g) {
  18.         g.drawImage(imgFile, getInsets().left, getInsets().top, this);             
  19.     }
  20.    
  21.     @Override
  22.     public void update(Graphics g) {
  23.        
  24.         paint(g);
  25.        
  26.     }
  27.    
  28.     private int[] relinkArray(int[] a1) {
  29.         return a1;
  30.     }
  31.    
  32.     private void countAllColours(int[] pixels, int[] imgCol, int[] imgFreq) {
  33.         int colorNumber = 0;
  34.         int[] temp_colors = new int[pixels.length];
  35.         boolean hadWeZeroYet = false;
  36.         boolean zeroKick = false;
  37.        
  38.         for(int i = 0; i < pixels.length; i++) {
  39.             boolean colorAlreadyThere = false;
  40.             if (pixels[i] == 0) hadWeZeroYet = true;
  41.             for (int k = 0; k < temp_colors.length && !colorAlreadyThere; k++) {
  42.                 if (temp_colors[k] == pixels[i]) {
  43.                     colorAlreadyThere = true;
  44.                    
  45.                     break;
  46.                 }
  47.             }
  48.             if (hadWeZeroYet && colorAlreadyThere && !zeroKick) {
  49.                 temp_colors[colorNumber] = pixels[i];
  50.                 colorNumber++;
  51.                 zeroKick = true;
  52.             }
  53.             if (!colorAlreadyThere) {
  54.                 temp_colors[colorNumber] = pixels[i];
  55.                 colorNumber++;
  56.             }
  57.         }
  58.        
  59.         int[] ImgColRef = new int [colorNumber];
  60.         int[] imgFreqRef = new int [colorNumber];
  61.        
  62.         imgCol = relinkArray(ImgColRef);
  63.         imgFreq = relinkArray(imgFreqRef);
  64.        
  65.         for(int i = 0; i < colorNumber; i++) {
  66.             imgCol[i] = temp_colors[i];
  67.         }
  68.        
  69.         for (int i = 0; i < colorNumber; i++) {
  70.             for(int j = 0; j < pixels.length; j++) {
  71.                 if(imgCol[i] == pixels[j]) imgFreq[i]++;
  72.             }
  73.         }
  74.        
  75.         bubbleSort(imgFreq, imgCol);
  76.        
  77.         for (int i = 0; i < colorNumber; i++) {
  78. //          int blue1 = imgCol[i] & 0x000000ff;
  79. //          int green1 = (imgCol[i] & 0x0000ff00) >> 8;
  80. //          int red1 = (imgCol[i] & 0x00ff0000) >> 16;
  81. //          int alpha1 = (imgCol[i] & 0xff000000) >> 24;
  82.             String hex = Integer.toHexString(imgCol[i]);
  83. //          String hexstring = "#" + String.format("%02x%02x%02x%02x" , alpha1, red1, green1, blue1)  + " " + imgFreq[i];
  84.             String hexstring = "#" + hex  + " " + imgFreq[i];
  85.             System.out.println(hexstring);
  86.         }
  87.  
  88.     }
  89.    
  90.     public static void bubbleSort(int[] comparable, int[] comparable2) {
  91.         boolean changed = false;
  92.         do {
  93.             changed = false;
  94.             for (int a = 0; a < comparable.length - 1; a++) {
  95.                 if (comparable[a] > comparable[a + 1]) {
  96.                     int tmp = comparable[a];
  97.                     comparable[a] = comparable[a + 1];
  98.                     comparable[a + 1] = tmp;
  99.                    
  100.                     int tmp2 = comparable2[a];
  101.                     comparable2[a] = comparable2[a + 1];
  102.                     comparable2[a + 1] = tmp2;
  103.                     changed = true;
  104.                 }
  105.             }
  106.         } while (changed);
  107.     }
  108.    
  109.     private Image openImage(String imgPath) {
  110.         Image img = getToolkit().getImage(imgPath);
  111.        
  112.         try {
  113.             MediaTracker mt = new MediaTracker(this);
  114.             mt.addImage(img, 1);
  115.             mt.waitForAll();
  116.                
  117.         } catch (InterruptedException e) { e.printStackTrace(); }
  118.        
  119.         return img;
  120.     }
  121.    
  122.     private int[] grabPixels(Image img) {
  123.         int[] array = new int[W*H];
  124.        
  125.        
  126.         PixelGrabber grab = new PixelGrabber(img, 0, 0, W, H, array, 0, W);
  127.        
  128.         try {
  129.             grab.grabPixels();
  130.         } catch (InterruptedException e) { e.printStackTrace(); }
  131.        
  132.         return array;
  133.     }
  134.    
  135.     public Algos2() {
  136. //      imgFile = openImage("src/hs/algo/excercise2/PA221197.jpg");
  137.         imgFile = openImage("src/hs/algo/excercise2/3973516097_83f8a5de5c_o.jpg");
  138.         W = imgFile.getWidth(this);
  139.         H = imgFile.getHeight(this);
  140.         imgFilePixels = grabPixels(imgFile);
  141.         System.out.println(W*H);
  142.        
  143.         System.out.println(imgFilePixels.length);
  144.        
  145.         int[] a1 = { 0, 5, 5, 5, 3, 3, 0, 0, 1, 2, 1, 5};
  146.         countAllColours(imgFilePixels, this.imgColors, this.imgColorFreq);
  147.         System.out.println(imgFilePixels.length);
  148.         System.out.println(imgColors);
  149.        
  150.         setBounds(600, 200, W, H);
  151.         setVisible(true);
  152.         addWindowListener(new WindowAdapter(){
  153.              public void windowClosing(WindowEvent we){
  154.                     System.exit(0);
  155.             }
  156.         });
  157.     }
  158.    
  159.     public static void main(String[] args) {
  160.         new Algos2();
  161.  
  162.     }
  163.  
  164. }
Add Comment
Please, Sign In to add comment