Aoami
I am a FH squatter
- Joined
- Dec 22, 2003
- Messages
- 11,223
Hey guys,
I've been tasked to write a simple java applet to work out the width of 'golden rectangle' after the user inputs the height.
All the basics are done, but i'm having a problem outputting the results, i'm assuming there is a problem with a way i've coded the forumla.
The maths behind 'Golden Rectangles' is;
Say the height is 1cm, then the width is going to be 1.618cm as it follows the golden ratio (1hi obviously). The formula is 1:1+square root of 5 all divided by 2. explained coherently here
Okay, so my program compiles, but outputs 0.0 whatever I enter. Here is my code:
Do i need to import anything else to use math.sqrt?
Thanks in advance!
inb4 olololol n00b etc.
I've been tasked to write a simple java applet to work out the width of 'golden rectangle' after the user inputs the height.
All the basics are done, but i'm having a problem outputting the results, i'm assuming there is a problem with a way i've coded the forumla.
The maths behind 'Golden Rectangles' is;
Say the height is 1cm, then the width is going to be 1.618cm as it follows the golden ratio (1hi obviously). The formula is 1:1+square root of 5 all divided by 2. explained coherently here
Okay, so my program compiles, but outputs 0.0 whatever I enter. Here is my code:
Code:
import java.awt.*;
import javax.swing.*;
public class GoldenRatio extends JApplet {
double doheight;
public void init()
{
String height; // height of the rectangle entered by the user
double doheight; // height int converted to a double
// read in the height of the rectangle from the user
height =
JOptionPane.showInputDialog(
"Enter the height of the rectangle" );
// convert height from tpye String to type Double
doheight = Double.parseDouble( height );
}
public void paint(Graphics g)
{
// draw the results with g.drawString
g.drawRect( 15, 10, 270, 20 );
g.drawString( "The width is " + doheight*(doheight*(Math.sqrt(5)+1)/2), 25, 25 );
}
}
Do i need to import anything else to use math.sqrt?
Thanks in advance!
inb4 olololol n00b etc.