Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.image.*;
- import java.io.*;
- public class circleProgrammingQuestion {
- private static long time = System.nanoTime();
- public static void main(String[] args) throws IOException{
- int imageWidth = 1000;
- int imageHeight = imageWidth;
- BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
- Graphics2D graphics2D = image.createGraphics();
- graphics2D.setColor(Color.WHITE);
- graphics2D.fillRect(0,0,imageWidth,imageHeight);
- graphics2D.setColor(Color.BLUE);
- for (int i = 0; i < 5; i++) {
- int x = (int)(Math.random()*(1+imageWidth));
- int y = (int)(Math.random()*(1+imageHeight));
- int rad = (int)(Math.random()*(.4*(imageWidth+imageHeight)/2+1)+(.1*(imageWidth+imageHeight)/2));
- graphics2D.fillOval(x-(rad/2),y-(rad/2),rad,rad);
- }
- ImageIO.write(image,"png", new File("image.png"));
- DisplayImage(imageWidth, imageHeight, "image.png");
- int filledCount = 0;
- int pixelCount = 0;
- for (int i = 0; i < imageHeight; i++) {
- for (int j = 0; j < imageWidth; j++) {
- if(image.getRGB(i,j)!=-1)
- filledCount++;
- pixelCount++;
- }
- }
- System.out.print("Area: ");
- System.out.printf("%.2f",((double)filledCount/pixelCount)*100);
- System.out.println("%");
- double timeTaken = System.nanoTime()-time;
- System.out.println("Time taken: "+timeTaken/1000000000+" Seconds");
- }
- public static void DisplayImage(int x, int y, String s) throws IOException
- {
- BufferedImage img=ImageIO.read(new File(s));
- ImageIcon icon=new ImageIcon(img);
- JFrame frame=new JFrame("circleProgrammingQuestion");
- frame.setResizable(false);
- frame.setLayout(new FlowLayout());
- frame.setSize(x,y);
- JLabel lbl=new JLabel();
- lbl.setIcon(icon);
- frame.add(lbl);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement