<h3>Details</h3> <p>The DrawingPanel should be 400x300.</p> <h4>Circles</h4> <p>
ID: 3623949 • Letter: #
Question
<h3>Details</h3><p>The DrawingPanel should be 400x300.</p>
<h4>Circles</h4>
<p>Ask the user for the center and radius of each circle.  Use the <tt>fillOval</tt> method to draw the circles.  Note that the parameters to <tt>fillOval</tt> (the upper-left corner and the width and height of the bounding box) need to be calculated from the information provided by the user (the center and the radius).  Also, fill the circles using three different colors.</p>
<h4>Information</h4>
<p>Use <tt>println</tt> statements to provide information about the circles.  Remember to put the results of the <tt>println</tt> statements into a file named <tt>CirclesOutput.txt</tt>.  Each of the following items should be performed by a separate static method.  Each method might have up to six parameters.  Each circle is described by three values: the x and y values of the center, and the radius.</p>
<ol>
<li>Given two circles, return <tt>-1</tt> if the first circle is smaller, return <tt>0</tt> if the two circles have the same size, or return <tt>1</tt> if the first circle is larger.  The program should print a line of output like:
<blockquote>
<pre>The green circle is smaller than the red circle.
</pre>
</blockquote>
</li>
<li>Given two circles, return <tt>1</tt> if the circles intersect or return <tt>0</tt> if the circles do not intersect (alternatively, you could return <tt>true</tt> or <tt>false</tt>).  If <tt>(x1,y1)</tt> and <tt>(x2,y2)</tt> are the centers of the circles and if <tt>r1</tt> and <tt>r2</tt> are their radiuses, then the circles intersect if the distance between the centers, the square root of
<blockquote>
<pre>(x1 - x2)<sup>2</sup> + (y1 - y2)<sup>2</sup>
</pre>
</blockquote>
is less than or equal to <tt>r1 + r2</tt>.  The program should print a line of output like:
<blockquote>
<pre>The blue circle intersects the red circle.
</pre>
</blockquote>
</li>
<li>(Optional) Given a circle, return <tt>1</tt> if the circle is completely within the window, return <tt>0</tt> if the circle is partially within the window, or return <tt>-1</tt> if the circle is completely outside the window.  The program should print something appropriate.</li>
</ol>
<p>Note that each method needs to be called three times.  If your circles are blue, green, and red, then the first method needs to be called for three pairs: blue and green, blue and red, and green and red.</p>
<p>If you find it is more convenient to return values other than <tt>-1</tt>, <tt>0</tt>, and <tt>1</tt>, you are free to do so.  However, your methods must return values that your main method uses to produce the text output.  You are also free to implement any additional methods that you like.</p>
<p> </p>
<p>Here is the code for  the drawing pannel:</p>
<p>/*Stuart Reges and Marty SteppFebruary 24, 2007Some modifications by Tom Bylander in 2010<br />The DrawingPanel class provides a simple interface for drawing persistentimages using a Graphics object.  An internal BufferedImage object is usedto keep track of what has been drawn.  A client of the class simplyconstructs a DrawingPanel of a particular size and then draws on it withthe Graphics object, setting the background color if they so choose.<br />To ensure that the image is always displayed, a timer calls repaint atregular intervals.*/<br />import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;import javax.swing.event.*;<br />public class DrawingPanel implements ActionListener {    private static final int DELAY = 100;  // delay between repaints in millis    private static final boolean PRETTY = false;  // true to anti-alias<br />    private int width, height;    // dimensions of window frame    private JFrame frame;         // overall window frame    private JPanel panel;         // overall drawing surface    private BufferedImage image;  // remembers drawing commands    private Graphics2D g2;        // graphics context for painting    private JLabel statusBar;     // status bar showing mouse position<br />    // construct a drawing panel of given width and height enclosed in a window    public DrawingPanel(int width, int height) {        this.width = width;        this.height = height;        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);<br />        statusBar = new JLabel(" ");        statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));<br />        panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));        panel.setBackground(Color.WHITE);        panel.setPreferredSize(new Dimension(width, height));        panel.add(new JLabel(new ImageIcon(image)));<br />        // listen to mouse movement        MouseInputAdapter listener = new MouseInputAdapter() {            public void mouseMoved(MouseEvent e) {                statusBar.setText("(" + e.getX() + ", " + e.getY() + ")");            }<br />            public void mouseExited(MouseEvent e) {                statusBar.setText(" ");            }        };        panel.addMouseListener(listener);        panel.addMouseMotionListener(listener);<br />        g2 = (Graphics2D)image.getGraphics();        g2.setColor(Color.BLACK);        if (PRETTY) {            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);            g2.setStroke(new BasicStroke(1.1f));        }<br />        frame = new JFrame("Drawing Panel");        frame.setResizable(false);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.getContentPane().add(panel);        frame.getContentPane().add(statusBar, "South");        frame.pack();        frame.setVisible(true);        toFront();<br />        // repaint timer so that the screen will update        new Timer(DELAY, this).start();    }<br />    // used for an internal timer that keeps repainting    public void actionPerformed(ActionEvent e) {        panel.repaint();    }<br />    // obtain the Graphics object to draw on the panel    public Graphics2D getGraphics() {        return g2;    }<br />    // set the background color of the drawing panel    public void setBackground(Color c) {        panel.setBackground(c);    }<br />    // show or hide the drawing panel on the screen    public void setVisible(boolean visible) {        frame.setVisible(visible);    }<br />    // makes the program pause for the given amount of time,    // allowing for animation    public void sleep(int millis) {        panel.repaint();        try {            Thread.sleep(millis);        } catch (InterruptedException e) {}    }<br />    // makes drawing panel become the frontmost window on the screen    public void toFront() {        frame.toFront();    }}</p>
<p> </p>
<p> </p>
<p></p>
Explanation / Answer
import java.awt.*;
import javax.swing.*;
public class Circles extends JFrame
{
private int screen = 160;
public Circles ()
{
super("Three Circles")
setSize( 400, 300 );
setVisible( true );
}
public void paint( Graphics g )
{
super.paint( g );
for ( int i = 1; i <= 3; i++ )
{
int origin = screent + 80 - i * 10;
g.drawOval( origin, origin, i * 20, i * 20);
}
}
public static void main( String[] args)
{
Circles c = new Circles ();
c.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.