go to previous page   go to home page   go to next page

Answer:

drawOval( 100-50, 300-50, 2*50, 2*50 )

Of course it would be better to use variables for this, as below.


Centered Circle

Output of Centered Circle Program

Here is a program that draws a circle placed in the center of the panel. The filled circle is drawn with fillOval() then the perimeter of the circle is drawn with drawOval().


import javax.swing.*;
import java.awt.*;

class CirclePanel extends JPanel
{

  public CirclePanel()
  {
    setPreferredSize( new Dimension(400, 400) );
    setBackground( Color.WHITE );   
  }
  
  public void paintComponent ( Graphics gr )
  { 
    super.paintComponent( gr );

    // Determine the center of the panel
    int cntrX = getWidth()/2;
    int cntrY = getHeight()/2;
    
    // Calculate the radius
    int radius = getWidth()/4;
    
    // Draw the Circle
    gr.setColor( Color.YELLOW );
    gr.fillOval( cntrX-radius, cntrY-radius, radius*2, radius*2 );
    gr.setColor( Color.GREEN );
    gr.drawOval( cntrX-radius, cntrY-radius, radius*2, radius*2 );
   }
}

public class CenteredCircle
{
   public static void main ( String[] args )
   {
      JFrame frame = new JFrame( "Centered Circle" );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      
      frame.getContentPane().add( new CirclePanel() );
      
      frame.pack();
      frame.setVisible( true );
   }
}


QUESTION 16:

Would it be OK to reverse the order of fillOval() and drawOval() ?