Thursday, 12 September 2013

Inscribing Square within Circle

Inscribing Square within Circle

This is my second programming homework assignment this semester and I am
totally new to the concept of java programming. I was looking to see if
anyone could give me some advice on my code because I'm really confused
right now. Here's what I have so far:
I can get it to work a little, but the given values have to be around 500
and the inscribed circle that is drawn isn't in the right location. If
anyone could help I would really appreciate it. My main question is
regarding the fact that I'm able to print my circle inside my square with
no problems but when I try to print the inscribed circle using the offset
as my graphical coordinates the drawing comes out skewed to the right. If
anyone could review code and offer me some advice that would be highly
appreciated.
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
/**
HW2.java
@author Xavier D Martin <martinx@email.sc.edu>
@version 1.1
Created on Sep 11, 2013
*/
public class HW2 extends JFrame{
public static double diameter ;
public static double insideSqside;
public static int boarder = 30;
public static int offset;
public void paint(Graphics canvas)
{
canvas.drawRect(boarder, boarder, (int)diameter, (int)diameter);
canvas.setColor(Color.blue);
canvas.fillRect(boarder, boarder, (int)diameter, (int)diameter);
canvas.drawOval(boarder, boarder, (int)diameter, (int)diameter);
canvas.setColor(Color.RED);
canvas.fillOval(boarder, boarder, (int)diameter,(int) diameter);
/**I use the offset variable here to get graphical coordinates of
my inscribed
rectangle comes out skewed to the right I'm trying to get
it centered in the JFrame
*/
canvas.drawRect(offset,offset, (int)insideSqside,(int)insideSqside);
canvas.setColor(Color.yellow);
canvas.fillRect(offset, offset,(int)insideSqside ,(int)insideSqside);
}
public HW2(int x, int y)
{
super("Draw Circle Square");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(x,y);
this.setVisible(true);
}
public static void main(String[] args) {
double circumference, radius, circleArea,outsideSqarea,largeSqside,
insideSqarea;
String diameterInput;
diameterInput = JOptionPane.showInputDialog("Enter the diameter of
the
cirlce: ");
diameter = Double.parseDouble(diameterInput);
HW2 hw = new HW2((int)diameter+100,(int)diameter+100);
radius = diameter/ 2;
circleArea = Math.pow(radius,2) * Math.PI;//circle area
circumference = 2 * Math.PI * radius;//circumference
outsideSqarea = Math.pow(diameter, 2);//area of outside square
largeSqside = diameter;//sides of outside square
insideSqside = (Math.sqrt(Math.pow(radius, 2) + Math.pow(radius,
2)));//side of inside square
offset = (int)diameter - (int)insideSqside;//difference between
diameter of
circle and and inside square to get graphical coordinates of
rectangle
insideSqarea = Math.pow((double)insideSqside,2);//area of inside
square
//prints out
JOptionPane.showMessageDialog(null, "Your circle's diameter is" + " " +
diameter);
JOptionPane.showMessageDialog(null, "Your circle's radius is" + " " +
radius);
JOptionPane.showMessageDialog(null, "Your cirlce's area is" + " " +
circleArea);
JOptionPane.showMessageDialog(null, "Your circle's circumference" + " " +
circumference);
JOptionPane.showMessageDialog(null,"The area of the smallest square
containing the circle is" + " "+ outsideSqarea);
JOptionPane.showMessageDialog(null,"The area of the largest square
contained in the circle is" +" "+ insideSqarea);
}
}

No comments:

Post a Comment