so please excuse any informalities.
I've successfully calculated the areas and volume of a cylinder using scanned values for the radius and height, but now want to build an applet which gives a graphical representation of the cylinder whilst showing the values below.
I've done half of this: I've built an applet which draws a cylinder and prints information below it (using drawOval, drawLine and drawString) -- but the varibles have to be constant. I can't use scanner.in while using java Applet. Is there anyway around this, some other method or library which I should use?
Code below:--
import java.util.Scanner;
/**
* Draw a cylinder using radius and height; and calculate base, lateral and total area; and volume.
* vrsn 0.1 draw text data below cylinder
* @author (thkoby)
* @vrsn 0.1 dt. Jan 6, 2015
* @vrsn 0.0 dt. Jan. 5, 2015
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.*;
public class cyl extends Applet {
double Pi = 3.1415926; //global value for constant pi
public void paint (Graphics g) {
int bottX; //x pos. bottom cylinder
int bottY;
int topX; //x pos. top cylinder
int topY;
double bArea; //base area
double tArea; //total area
double lArea; //lateral area
double vol; //volume
double rad = 10; //radius
double height = 20; //height
int r = (int) rad;
int h = (int) height;
int d = 2*r; //diameter
int fontSize = 12;
bArea = baseArea(rad);
tArea = totalArea(rad,height);
lArea = lateralArea(rad,height);
vol = volume(rad,height);
//x,y pos. for bottom oval
bottX = 20;
bottY = 20 + h;
//x,y pos. for top oval
topX = 20;
topY = 20;
//Graphical representation of the cylinder
g.drawOval (topX, topY, d, r); //radius is used to give angled appearance
g.drawOval (bottX, bottY, d, r);
g.drawLine (topX,topY+(r/2),bottX,bottY+(r/2));
g.drawLine (topX+d,topY+(r/2),bottX+d,bottY+(r/2));
//text information below cylinder
g.setFont(new Font("Courier", Font.PLAIN, fontSize));
g.setColor(Color.black);
g.drawString("Radius: "+r, 20, 40+h+r);
g.drawString("Height: "+h, 20, 54+h+r);
g.drawString("Base Area: "+bArea, 20, 68+h+r);
g.drawString("Total Area: "+tArea, 20, 82+h+r);
g.drawString("Lateral Area: "+lArea, 20, 96+h+r);
g.drawString("Volume: "+vol, 20, 110+h+r);
}
public double baseArea(double rad) { //base area = radius squared * pi
return (rad*rad)*Pi;
}
public double totalArea(double rad,double height) { //total area = 2pi * radius * (height + radius)
return 2*Pi*rad*(height + rad);
}
public double lateralArea(double rad, double height) { //lateral area = 2pi * radius * height
return 2*Pi*rad*height;
}
double volume(double rad, double height) { //volume = 2pi * radius squared * height
return Pi*(rad*rad)*height;
}
}