Hi, I need help with this java programing Create a Java applet file (import java
ID: 3819117 • Letter: H
Question
Hi, I need help with this java programing
Create a Java applet file (import javax.swing.JApplet) that has code for drawing at least three graphical images (be creative). The class should extend JApplet and declare a String variable named to hold the shape type.
In the public void init() method, obtain a parameter value from Run Configurations to determine which of the shapes to draw. Do this by setting your String variable equal to the value returned by the getParameter() method using the argument “shapeType”.
In the public void paint(Graphics g) method, use if statements to see if your string value for the shape type equals one of the three shapes you define, and then draw the corresponding graphic.
Below, list the three parameter values you are using for the three shapes. The instructor will run your Java programs by setting the Run Configuration parameter shapeType to each of the three values you list. Each one should cause a different graphic image to be drawn.
You can create interesting images by combining the basic Graphics drawing methods. For example, a stick figure human could be drawn with a circle for the head, eyes, ears, nose and mouth, and lines for the torso, arms, and legs, etc.
Explanation / Answer
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
public class Triangle extends Shape {
private final double a, b, c;
public Triangle() {
this(2,2,2);
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double area() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
public double perimeter() {
return a + b + c;
}
}
public class Circle extends Shape
{
private double radius;
double pi = Math.PI;
public Circle() {
this(1);
}
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return pi * Math.pow(radius, 2);
}
public double perimeter() {
return 2 * pi * radius;
}
}
public class TestShape {
public static void main(String[] args) {
double a = 5, b = 3, c = 4;
Shape triangle = new Triangle(a,b,c);
System.out.println("Triangle sides are: " + a + ", " + b + ", " + c+ " Resulting Area: " + triangle.area()+ " Resulting Perimeter: " + triangle.perimeter() + " ");
double radius = 5;
Shape circle = new Circle(radius);
System.out.println("Circle radius: " + radius+ " Resulting Area: " + circle.area()+ " Resulting Perimeter: " + circle.perimeter() + " ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.