Question
Design, implement and test a set of Java classes that allows a user to select a shape from a list of available shapes, enter appropriate
Design, implement and test a set of Java classes that allows a user to select a shape from a list of available shapes, enter appropriate dimensional parameters and then display that shape in a frame of your Swing-based GUI. For 3-D shapes consider loading an image from a file and displaying that as a representative. Your list of shapes should be similar, if not identical to the ones used in project one: Circle Square Triangle Rectangle Sphere Cube Cone Cylinder Torus Take advantage of various Swing AWT components including Layout Managers, Event Handlers, Listener Interfaces, Adapter Classes, Inner Classes, Buttons and other widgets as needed.
I cannot my program to work. Based on my GUI, when I select a shape, it should create a shape object then when I click on the button, it should draw the shape, I have tried everything but I am only getting a button
Here is what I have. Please help!
import java.awt.*;
public class Shapes { // All shapes must start at point(2,2.5) public static final Point p = new Point(96,192); public int x() { return Shapes.p.x; }
public int y() { return Shapes.p.y; }
}
package Project2;
public class Circle extends Shapes{ int rs1; int rs2 = rs1; public Circle(int rs1, int rs2) { super(); this.rs1 = rs1; this.rs2 =rs2; } public int getRs1() { return rs1; } public int getRs2() { return rs2; }
}
package Project2;
public class Square extends Shapes{ int s1; int s2 = s1; public Square(int s1, int s2) { this.s1 = s1; this.s2 = s2; }
}
package Project2;
public class Rectangle extends Shapes{ int w; int l; public Rectangle(int w, int l) { this.w = w; this.l = l; } }
/* * File: ShapesPanel.java * Author: Arlette Watson * Date: February 9, 2021 * Description: This class will create the Shapes Panel class * subclass to the mainPanel class, and will define all Panels */ package Project2;
import javax.swing.*; import javax.swing.border.TitledBorder;
import Project1.Circle;
import java.awt.*; import java.awt.event.*;
public class ShapesPanels extends JPanel{ JRadioButton circle,square,triangle,rectangle,sphere,cube,cone,cylinder,torus; ButtonGroup cbg; Button bttnPaintShape; JPanel panelNorth = new JPanel(); JPanel panelSouth = new JPanel(); Project2.Circle s; buttonHandler bh = new buttonHandler(); // Constructor************************************************************ public ShapesPanels() { super(); setLayout(new BorderLayout(10, 8)); add(getPanelNorth(),BorderLayout.NORTH); add(getPanelSouth(),BorderLayout.SOUTH); bttnPaintShape.addActionListener(bh); circle.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { } }); rectangle.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { repaint(); } }); square.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { } }); }// end constructor******************************************************* public JPanel getPanelNorth() { panelNorth.setLayout(new GridLayout(2, 5, 5, 5)); panelNorth.setBorder(BorderFactory.createTitledBorder( BorderFactory.createMatteBorder(1,1,1,1,Color.BLACK), "Select a shape from the list", TitledBorder.LEFT, TitledBorder.TOP)); // Create a check box group cbg = new ButtonGroup();
// Create Controls circle = new JRadioButton("Circle",true); square = new JRadioButton("Square"); triangle = new JRadioButton("Triangle"); rectangle = new JRadioButton("Rectangle"); sphere = new JRadioButton("Sphere"); cube = new JRadioButton("Cube"); cone = new JRadioButton("Cone"); cylinder = new JRadioButton("Cylinder"); torus = new JRadioButton("Torus"); cbg.add(circle); cbg.add(square); cbg.add(triangle); cbg.add(rectangle); cbg.add(sphere); cbg.add(cube); cbg.add(cone); cbg.add(cylinder); cbg.add(torus); // add check boxes to the north panel panelNorth.add(circle); panelNorth.add(square); panelNorth.add(triangle); panelNorth.add(rectangle); panelNorth.add(sphere); panelNorth.add(cube); panelNorth.add(cone); panelNorth.add(cylinder); panelNorth.add(torus); return panelNorth; }// end getPanelNorth() public JPanel getPanelSouth() { panelSouth.setLayout(new FlowLayout()); bttnPaintShape = new Button("Paint Shape"); panelSouth.add(bttnPaintShape); return panelSouth; }// end getPanelSouth() int txt1, txt2, txt3; // Listener handler class buttonHandler implements ActionListener{ public void actionPerformed(ActionEvent ae) { if(circle.isSelected()) { s = new Project2.Circle(2, 2); }else if(rectangle.isSelected()) { repaint(); }else if(square.isSelected()) { repaint(); } } } protected void paintComponent(Graphics g) { super.paintComponent(g); if(circle.isSelected()) { g.fillOval(96, 192,s.getRs1() , s.getRs2()); }else if(rectangle.isSelected()) { g.fillRect(96, 192, 2, 1); }else if(square.isSelected()) { g.fillRect(96, 240, 2, 2); } }
}// End ShapesPanels
package Project2;
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.TitledBorder;
@SuppressWarnings("serial") public class ShapeFrameGUI extends JFrame{ private int frameWidth; private int frameHeight; ShapesPanels sp = new ShapesPanels(); buttonHandler bh = new buttonHandler(); Shapes s = new Shapes(); // Constructor************************************************************ public ShapeFrameGUI(String title, int w, int h) { createMyFrame(title,w,h); customizeFrame(); setContentPane(sp); } // End Constructor******************************************************* // method to create a myFrame object private void createMyFrame(String t, int w, int h) { frameWidth = w * 96; frameHeight = w * 96; // Title setTitle(t); // Size setSize(new Dimension(frameWidth, frameHeight)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Center my frame on my screen setLocationRelativeTo(null); // make the frame visible //pack(); setVisible(true); }// end createMyFrame() method /************************************************************************/ private void customizeFrame() { // Set up margins all around getRootPane().setBorder(BorderFactory.createMatteBorder( 15, 15, 15, 15, getBackground())); // Create main Container to hold all of my panels this.setLayout(new BorderLayout(10, 8)); }// end customizeFrame() method
// Listener handler class buttonHandler implements ActionListener{ public void actionPerformed(ActionEvent ae) { repaint(); } }
/************************************************************************/ // main() method public static void main(String[] args) { String title = "Project 2 - Shape GUI - Arlette Watson"; int w = 6; int h = 4; new ShapeFrameGUI(title,w,h);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started