JAVA! In this assignment, you will create a Graphical User Interface (GUI) to pr
ID: 3803691 • Letter: J
Question
JAVA! In this assignment, you will create a Graphical User Interface (GUI) to prompt the user for information about a shape and then display a labelled graphic of that shape along with its calculated area.
You must include:
1) A GUI that uses a JComboBox to prompt the user for the type of shape they would like to create (circle, square, or triangle), and then uses JLabel and JTextField objects to prompt them for measurements that are appropriate for that shape (i.e. radius for circle, side length for square, base & height for triangle). Your GUI should only include appropriate prompts (e.g. don't ask for radius if the user selects a triangle). You may assume that the triangle is a right triangle (Links to an external site.).
2) A display of the shape described by the user including a graphic of the shape, labelled measurements, and the calculated area. Note: Shapes do not need to be drawn to scale.
Explanation / Answer
import java.awt.*;
import javax.swing.*;
public class app extends JFrame{
CirclePanel drawing = new CirclePanel();
Circle() {
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER);
this.setTitle("Circles");
}
public static void main(String[] args) {
JFrame myWindow = new Circle();
JFrame myWindow = new Square();
JFrame myWindow = new triangle();
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}
}
class CirclePanel extends JPanel {
public CirclePanel() {
setPreferredSize(new Dimension(100, 100));
setBackground(Color.white);
}//end constructor
//=========================================== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircle(g, 40, 40, 30); // center (40,40) r=30
}
// Convenience method to draw from center with radius
public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);
}//end drawCircle
}
square() {
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER);
this.setTitle("Square");
}
class SquarePanel extends JPanel {
public SquarePanel() {
setPreferredSize(new Dimension(800, 600));
setBackground(Color.white);
}//end constructor
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw square(g, 200, 75, 400,400);
}
public void drawsquare(Graphics cg, int x, int y) {
cg.drawOval(startX,startY,lightDiameter,lightDiameter);
}
triangle() {
Container content = this.getContentPanel();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.Centre);
this.setTitle("triangle");
}
class TrianglePanel extends JPanel {
public panelTriangle() {
setPreferredSize(new Dimension(30, 30,20));
setBackground(Color.white);
}//end constructor
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw tiangle(g, 75, 400,400);
}
public void drawtriangle(Graphics cg, int x, int y,intz) {
cg.drawOval(base*hight/2);
}
import java.awt.*;
import javax.swing.*;
public class app extends JFrame{
CirclePanel drawing = new CirclePanel();
Circle() {
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER);
this.setTitle("Circles");
}
public static void main(String[] args) {
JFrame myWindow = new Circle();
JFrame myWindow = new Square();
JFrame myWindow = new triangle();
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}
}
class CirclePanel extends JPanel {
public CirclePanel() {
setPreferredSize(new Dimension(100, 100));
setBackground(Color.white);
}//end constructor
//=========================================== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircle(g, 40, 40, 30); // center (40,40) r=30
}
// Convenience method to draw from center with radius
public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);
}//end drawCircle
}
square() {
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER);
this.setTitle("Square");
}
class SquarePanel extends JPanel {
public SquarePanel() {
setPreferredSize(new Dimension(800, 600));
setBackground(Color.white);
}//end constructor
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw square(g, 200, 75, 400,400);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.