Advertisement
Guest User

Untitled

a guest
Jul 4th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Polygon;
  4. import java.awt.image.BufferedImage;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import javax.imageio.ImageIO;
  10.  
  11. public class C0169_Hard2 {
  12.  
  13.     private static Vertex[] vertices;
  14.    
  15.     private static int SIZE = 2000;
  16.     private static BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
  17.     private static File file = new File("Polygon.txt");
  18.    
  19.     public static void main(String[] args) {
  20.         readFromFile();
  21.         draw(vertices);
  22.         saveToFile();
  23.         System.out.println(getPixels());
  24.     }
  25.    
  26.     private static int getPixels(){
  27.         int count = 0;
  28.         for(int i = 0; i < image.getWidth(); i++){
  29.             for(int j = 0; j < image.getHeight(); j++){
  30.                 Color c = new Color(image.getRGB(j, i));
  31.                 if(c.getRed() > 10){
  32.                     count++;
  33.                 }
  34.             }
  35.         }
  36.         return count;
  37.     }
  38.    
  39.     private static void draw(Vertex[] v){
  40.         Graphics2D graphics = image.createGraphics();
  41.         drawPolygon(v, graphics);
  42.     }
  43.    
  44.     private static void drawPolygon(Vertex[] v, Graphics2D g){
  45.         int[] xPoints = new int[v.length];
  46.         int[] yPoints = new int[v.length];
  47.         for(int i = 0; i < xPoints.length; i++){
  48.                 xPoints[i] = ((int)v[i].x)*SIZE/10;
  49.                 yPoints[i] = ((int)v[i].y)*SIZE/10;
  50.         }
  51.         Polygon p = new Polygon(xPoints, yPoints, v.length);
  52.         g.drawPolygon(p);
  53.         g.fillPolygon(p);
  54.     }
  55.    
  56.     private static void saveToFile(){
  57.         File outputFile = new File("poly.png");
  58.         try {
  59.             ImageIO.write(image, "png", outputFile);
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.    
  65.     private static void readFromFile(){
  66.         try{
  67.             BufferedReader buffRead = new BufferedReader(new FileReader(file));
  68.             String line = buffRead.readLine();
  69.             int count = -1;
  70.             while(line != null){
  71.                 if(count == -1) vertices = new Vertex[Integer.parseInt(line)+1];
  72.                 else            vertices[count] = new Vertex(line);
  73.                 count++;
  74.                 line = buffRead.readLine();
  75.             }
  76.             vertices[vertices.length-1] = new Vertex(vertices[0].x, vertices[0].x);
  77.             buffRead.close();
  78.         } catch(IOException e){
  79.             e.printStackTrace();
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement