Write a program that draws 2 white circles somewhere in a da

Write a program that draws 2 white circles somewhere in a dark gray rectangular (not square) PGM. You may hard-code the row and column locations, radius and brightness levels for the two circles, or make it a little more challenging and prompt the user for the circle information.

For this, it may be helpful to use the sqrt( ) function, which returns the square root of a number. For example:

y = sqrt(2.0);

sets y to ~1.414. If the distance of a pixel from the center of the circle is less than the radius, you\'re inside the circle. Otherwise you\'re outside.

Outside the circle, assign the each pixel a random number between 0 & 64. (See the rand(...) function in chapter 3. The modulus operator will come in handy to truncate your random numbers to a value between 0 and 255.

For more excitement (this IS exciting, isn\'t it?) you could shade the the circles as you get closer to the center, or shade the random numbers outside the circle. Here\'s a circle example, with shading towards the center of the circle. (This is from a previous semester, where the challenge was to draw a single black circle on a random background, centered in the image):

Solution

we can use the RadialGradientPaint library of java to complete the task.

package test;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Paint;

import java.awt.RadialGradientPaint;

import java.awt.RenderingHints;

import java.awt.geom.Point2D;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class DrawCircle extends JFrame {

   /**

   *

   */

   private static final long serialVersionUID = 1L;

   public DrawCircle() {

       super(\"Draw Circle\");

       JPanel panel = new JPanel();

       panel.add(new CircleComponent());

       add(panel);

       pack();

       setLocationRelativeTo(null);

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }

   public static void main(String... args) {

       SwingUtilities.invokeLater(new Runnable() {

           public void run() {

               new DrawCircle ().setVisible(true);

           }

       });

   }

}

class CircleComponent extends JComponent {

   /**

   *

   */

   private static final long serialVersionUID = 1L;

   public CircleComponent() {

   }

   @Override

   public Dimension getPreferredSize() {

       //dimension of the rectangle

       return new Dimension(360, 250);

   }

   @Override

   protected void paintComponent(Graphics g) {

       //drawing the circle

      

       Graphics2D g2d = (Graphics2D) g;

       g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

       int w = getWidth();

       int h = getHeight();

       Color color1 = new Color(169, 169, 169);

       Color color2 = new Color(169, 169, 169);

       GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);

       g2d.setPaint(gp);

       g2d.fillRect(0, 0, w, h);

       setFont(getFont().deriveFont(70.f).deriveFont(Font.BOLD));

       Graphics2D g2 = (Graphics2D) g;

       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

       Paint oldPaint = g2.getPaint();

       g2.setColor(new Color(255, 255, 255));

       g2.fillOval(0, 0, getHeight() - 1, getHeight() - 1);

       Paint p;

       p = new GradientPaint(0, 0, new Color(0.0f, 0.0f, 0.0f, 0.4f), 0, getHeight(),

               new Color(0.0f, 0.0f, 0.0f, 0.0f));

       g2.setPaint(p);

       g2.fillOval(0, 0, getHeight() - 1, getHeight() - 1);

       p = new GradientPaint(0, 0, new Color(1.0f, 1.0f, 1.0f, 0.0f), 0, getHeight(),

               new Color(1.0f, 1.0f, 1.0f, 0.4f));

       g2.setPaint(p);

       g2.fillOval(0, 0, getHeight() - 1, getHeight() - 1);

       p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() / 2.0), getWidth() / 2.0f,

               new float[] { 0.0f, 1.0f },

               new Color[] { new Color(255, 255, 255, 127), new Color(0.0f, 0.0f, 0.0f, 0.8f) });

       g2.setPaint(p);

       g2.fillOval(0, 0, getHeight() - 1, getHeight() - 1);

       p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() / 2.0), getWidth() / 1.4f,

               new Point2D.Double(45.0, 25.0), new float[] { 0.0f, 0.5f },

               new Color[] { new Color(1.0f, 1.0f, 1.0f, 0.4f), new Color(1.0f, 1.0f, 1.0f, 0.0f) },

               RadialGradientPaint.CycleMethod.NO_CYCLE);

       g2.setPaint(p);

       g2.fillOval(0, 0, getHeight() - 1, getHeight() - 1);

       g2.setPaint(oldPaint);

   }

}

Write a program that draws 2 white circles somewhere in a dark gray rectangular (not square) PGM. You may hard-code the row and column locations, radius and bri
Write a program that draws 2 white circles somewhere in a dark gray rectangular (not square) PGM. You may hard-code the row and column locations, radius and bri
Write a program that draws 2 white circles somewhere in a dark gray rectangular (not square) PGM. You may hard-code the row and column locations, radius and bri
Write a program that draws 2 white circles somewhere in a dark gray rectangular (not square) PGM. You may hard-code the row and column locations, radius and bri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site