Question: Can someone please help with code needed to create separate classes and whats needed to run ( Square and Circle are below ) Triangle Rectangle

Can someone please help with code needed to create separate classes and whats needed to run (Square and Circle are below)
Triangle Rectangle Sphere Cube Cone Cylinder Torus
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShapeSelectorGUI extends JFrame {
private JComboBox shapeComboBox;
private JComboBox sizeComboBox;
private JPanel displayPanel;
public ShapeSelectorGUI(){
setTitle("Shape Selector");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Shape selection
String[] shapes ={"Circle", "Square"};
shapeComboBox = new JComboBox<>(shapes);
shapeComboBox.addActionListener(new ShapeSelectionListener());
// Size selection
String[] sizes ={"Small", "Medium", "Large"};
sizeComboBox = new JComboBox<>(sizes);
// Control panel
JPanel controlPanel = new JPanel();
controlPanel.add(new JLabel("Select Shape:"));
controlPanel.add(shapeComboBox);
controlPanel.add(new JLabel("Select Size:"));
controlPanel.add(sizeComboBox);
// Display panel
displayPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
drawShape(g);
}
};
add(controlPanel, BorderLayout.NORTH);
add(displayPanel, BorderLayout.CENTER);
}
private void drawShape(Graphics g){
String selectedShape =(String) shapeComboBox.getSelectedItem();
String selectedSize =(String) sizeComboBox.getSelectedItem();
int size = getSizeFromSelection(selectedSize);
if ("Circle".equals(selectedShape)){
g.drawOval(50,50, size, size);
} else if ("Square".equals(selectedShape)){
g.drawRect(50,50, size, size);
}
}
private int getSizeFromSelection(String size){
switch (size){
case "Small":
return 50;
case "Medium":
return 100;
case "Large":
return 150;
default:
return 50;
}
}
private class ShapeSelectionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
displayPanel.repaint();
}
}
public static void main(String[] args){
SwingUtilities.invokeLater(()->{
ShapeSelectorGUI gui = new ShapeSelectorGUI();
gui.setVisible(true);
});
}
}
package Week4Project;
public class Circle {
private double radius;
public Circle(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
package Week4Project;
public class Square {
private double side;
public Square(double side){
this.side = side;
}
public double getSide(){
return side;
}
public void setSide(double side){
this.side = side;
}
public double getArea(){
return side * side;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!