Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Applet - Calculate Shapes

$
0
0
Hello,
Just beginning this Java Applet subject. I am trying to complete a program to calculate a circle and rectangle.

It looks like my calculations are right but when I click on the buttons the rectangle doesn't calculate right, but when the values are inserted I can press the Circle button and works fine.. Also how would I get to display a circle and rectangle at the bottom under the button of either a circle or rectangle depending on what is pressed?

Thanks

package Yes;

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

/** This program calculates a Circle and Rectangle */

public class g extends JApplet implements ActionListener {
    
    private Label displayLabel, radiusCLabel, diameterCLabel, circumferenceCLabel, areaCLabel;
    private Label lengthRLabel, breadthRLabel, areaRLabel, permiterRLabel;
    private TextField radiusCField, diameterCField, circumferenceCField, areaCField;
    private TextField lengthRField, breadthRField, areaRField, permiterRField;
    private Button calculateRec, calculateCircle;
    private double radius, diameter, circumference, areaC, areaR, length, breadth, perimeter;


    public void init()
    {
        
               setLayout(new FlowLayout(FlowLayout.CENTER));		
        displayLabel 	= new Label ( "Currency Converter" ) ;
        add ( displayLabel ) ;
                
	Panel panel = new Panel(new GridLayout(12,2));

	// create label and text field for the radius
	radiusCLabel = new Label("Radius");
	panel.add( radiusCLabel );
	radiusCField = new TextField( 10 );
	panel.add( radiusCField );

	// create label and text field for the diameter
	diameterCLabel = new Label( "Diameter" );
	panel.add( diameterCLabel );
	diameterCField = new TextField( 10 );
	diameterCField.setEditable( false );
	panel.add( diameterCField );

	// create label and text field for the circumference
        circumferenceCLabel = new Label( "Circumference" );
	panel.add( circumferenceCLabel );
	circumferenceCField = new TextField( 10 );
	circumferenceCField.setEditable( false );
	panel.add( circumferenceCField );

	// create label and text field for the area
	areaCLabel = new Label( "Area" );
	panel.add( areaCLabel );
	areaCField = new TextField( 10 );
	areaCField.setEditable( false );
	panel.add( areaCField );

	// create button user clicks to calculate values
        panel.add(new Label(""));
	calculateCircle = new Button("Calculate Circle");		
	panel.add(calculateCircle);					
	calculateCircle.addActionListener(this);                      
	getContentPane().add(panel);
 
//
        // create label and text field for the  Length
	lengthRLabel = new Label( "Length" );
	panel.add( lengthRLabel );
	lengthRField = new TextField( 10 );
	panel.add( lengthRField );

	// create label and text field for the Breadth
	breadthRLabel = new Label( "Breadth" );
	panel.add( breadthRLabel );
	breadthRField = new TextField( 10 );
	panel.add( breadthRField );


	// create label and text field for the Area
	areaRLabel = new Label( "Area" );
	panel.add( areaRLabel );
	areaRField = new TextField( 10 );
	areaRField.setEditable( false );
	panel.add( areaRField );
        
        // create label and text field for the Perimeter
	permiterRLabel = new Label( "Perimiter" );
	panel.add( permiterRLabel );
	permiterRField = new TextField( 10 );
	permiterRField.setEditable( false );
	panel.add( permiterRField );

	// create button user clicks to calculate values
        panel.add(new Label(""));
	calculateRec = new Button("Calculate Rectangle");	
	panel.add(calculateRec);				
	calculateRec.addActionListener(this);                  
	getContentPane().add(panel);
	
    }


    public void actionPerformed( ActionEvent event )
    {
        
	radius = Double.parseDouble( radiusCField.getText() );
	calculateValues();
	displayValues();
        
        length = Double.parseDouble( lengthRField.getText() );
	calculateValues();
	displayValues();
        
        breadth = Double.parseDouble( breadthRField.getText() );
	calculateValues();
	displayValues();
        
    }

    /** calculate the values */
    public void calculateValues()
    {
	diameter = 2 * radius;
	circumference = 2 * Math.PI * radius;
	areaC = Math.PI * radius * radius;
        
        perimeter = (2 * length) + (2 * breadth);
	areaR = length * breadth;
    }

    /** display the values */
    public void displayValues()
    {
	DecimalFormat df = new DecimalFormat( "0.00" );

	diameterCField.setText( df.format(diameter) + "" );
	circumferenceCField.setText( df.format(circumference) + "" );
	areaCField.setText( df.format(areaC) + "" );
    
        areaRField.setText( df.format(areaR) + "" );
	permiterRField.setText( df.format(perimeter) + "" );
    }

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles