Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import javax.imageio.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.io.*;
  6.  
  7. public class circleProgrammingQuestion {
  8.     private static long time = System.nanoTime();
  9.  
  10.     public static void main(String[] args) throws IOException{
  11.         int imageWidth = 1000;
  12.         int imageHeight = imageWidth;
  13.         BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
  14.         Graphics2D graphics2D = image.createGraphics();
  15.         graphics2D.setColor(Color.WHITE);
  16.         graphics2D.fillRect(0,0,imageWidth,imageHeight);
  17.         graphics2D.setColor(Color.BLUE);
  18.         for (int i = 0; i < 5; i++) {
  19.             int x = (int)(Math.random()*(1+imageWidth));
  20.             int y = (int)(Math.random()*(1+imageHeight));
  21.             int rad = (int)(Math.random()*(.4*(imageWidth+imageHeight)/2+1)+(.1*(imageWidth+imageHeight)/2));
  22.             graphics2D.fillOval(x-(rad/2),y-(rad/2),rad,rad);
  23.         }
  24.         ImageIO.write(image,"png", new File("image.png"));
  25.         DisplayImage(imageWidth, imageHeight, "image.png");
  26.         int filledCount = 0;
  27.         int pixelCount = 0;
  28.         for (int i = 0; i < imageHeight; i++) {
  29.             for (int j = 0; j < imageWidth; j++) {
  30.                 if(image.getRGB(i,j)!=-1)
  31.                     filledCount++;
  32.                 pixelCount++;
  33.             }
  34.         }
  35.         System.out.print("Area: ");
  36.         System.out.printf("%.2f",((double)filledCount/pixelCount)*100);
  37.         System.out.println("%");
  38.         double timeTaken = System.nanoTime()-time;
  39.         System.out.println("Time taken: "+timeTaken/1000000000+" Seconds");
  40.     }
  41.     public static void DisplayImage(int x, int y, String s) throws IOException
  42.     {
  43.         BufferedImage img=ImageIO.read(new File(s));
  44.         ImageIcon icon=new ImageIcon(img);
  45.         JFrame frame=new JFrame("circleProgrammingQuestion");
  46.         frame.setResizable(false);
  47.         frame.setLayout(new FlowLayout());
  48.         frame.setSize(x,y);
  49.         JLabel lbl=new JLabel();
  50.         lbl.setIcon(icon);
  51.         frame.add(lbl);
  52.         frame.setVisible(true);
  53.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement