In Java 1 Write a GUI program to display a circle and its at

In Java:

1. Write a GUI program to display a circle and its attributes. Your program must calculate the area,
radius, diameter, and circumference.   Use the constant Math.PI for pi where necessary. The drawing should be done on a subclass of JPanel, and the results of the calculation should be displayed in a read-only JTextArea.
2. Experiment with varying the weightx and weighty values of the program in Figure 22.21 in the
textbook. What happens when a slot has a nonzero weight but is not allowed to fill the whole area (e.g. the fill value is not BOTH)?

Solution

1.

// draw_circle.java
// This Java Program draws a circle
// and also, display_areas the area, radius, diameter, and circumference of the circle.

// Computations used in the program are:
// Diameter = 2 * radius
// Area = pi * radius * radius
// circumference = 2 * pi * radius

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

public class draw_circle extends JFrame {
private circle_canvas CircleCanvas;
private JTextArea display_area;

public draw_circle()
{
super( \"draw_circle\" );
CircleCanvas = new circle_canvas();
display_area = new JTextArea( 10, 50 );
  
   // display_area the computed values      
display_area.setText( \"The Circle Attributes are:\ Area is: \" + CircleCanvas.getArea() +
                   \"\ The Radius is: \" + CircleCanvas.getRadius() +
                   \"\ The Diameter is: \" + CircleCanvas.getDiameter() +                     
\"\ The Circumference is: \" +
CircleCanvas.getCircumference() );

getContentPane().add( CircleCanvas, BorderLayout.CENTER );
getContentPane().add( display_area, BorderLayout.SOUTH );
setSize( 200, 200 );
show();
}

public static void main( String args[] )
{
draw_circle app = new draw_circle();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}

class circle_canvas extends JPanel {
private int radius;

public circle_canvas()
{
radius = ( int )( 1 + Math.random() * 500 );
setSize( 500, 500 );
}

public void paintComponent( Graphics g )
{
g.drawOval( 0, 0, radius, radius );
}

public int getDiameter()
{
   return ( 2 * radius );
}

public int getCircumference()
{
   return ( int )( 2 * Math.PI * radius );
}

public int getArea()
{
return ( int )( radius * radius * Math.PI );
}

public int getRadius()
{
return radius;
}
}

2. Incase when the slot has a weight which is non-zero but the component is not allowed to fill in both directions, a blank space is created around the component. The amount of space allocated for the component in the panel is the same as if it could expand to fill the space, but the component does not stretch in that direction.

In Java: 1. Write a GUI program to display a circle and its attributes. Your program must calculate the area, radius, diameter, and circumference. Use the const
In Java: 1. Write a GUI program to display a circle and its attributes. Your program must calculate the area, radius, diameter, and circumference. Use the const

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site