Question
Prompt Design, implement and test a set of Java classes that allows a user to select a shape from a list of available shapes, enter
Prompt
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 with the draw method. For 3-D shapes, load an image from a file and display that as a representative. Your list of shapes should be:
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. Take your time on understanding how the graphical components and listeners work so you can easily display appropriate actions based on any event.
Expert help so far, but incomplete
import java.awt.GridBagLayout; import java.io.PrintWriter; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JPanel;
public class Shapes { public static JFrame window = new JFrame("Shapes"); public static JPanel panel = new JPanel(new GridBagLayout());
public static void main(String[] args) {
window.setBounds(0, 0,300, 300); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(panel); MApp m = new MApp(); m.setBounds(100,100,100,100); window.add(m);
Draw d = new Draw(panel) ; d.setBounds(0, 0, window.getWidth(), 90); window.add(d);
window.setVisible(true); }
}
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import javax.swing.*;
import javax.swing.JPanel;
public class MApp extends JPanel implements MouseListener { private boolean clicked; private Rectangle r; public MApp() { clicked = false; r = new Rectangle(15, 15, 50, 50); addMouseListener(this); } public void paintComponent(Graphics g) { if(clicked) { g.setColor(Color.BLUE); } else { g.setColor(Color.RED); } g.fillRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight()); } public void mouseClicked (MouseEvent e) { Point p = new Point(e.getX(),e.getY()); if(r.contains(p)) { clicked = !clicked; } repaint(); } public void Circle() { g.fillOval(0, 0, s, s); } public void mousePressed (MouseEvent evnt) {} public void mouseReleased (MouseEvent evnt) {} public void mouseEntered (MouseEvent evnt) {} public void mouseExited (MouseEvent evnt) {} }
import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.ButtonGroup; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField;
public class Draw extends JPanel implements ActionListener { JTextField tfInfo; JLabel lblColor, lblShapes; JCheckBox cbRed, cbBlue; ButtonGroup shapes; JRadioButton rbCircle, rbSquare; JButton btnSubmit; public Draw(JPanel panel) { GridBagConstraints c = new GridBagConstraints(); tfInfo = new JTextField("Color", 15); tfInfo = new JTextField("Shapes", 50); lblColor = new JLabel("Colors:"); cbRed = new JCheckBox("Red"); cbBlue = new JCheckBox("Blue"); lblShapes = new JLabel("Shapes:"); shapes = new ButtonGroup(); rbCircle = new JRadioButton("Circle"); rbSquare = new JRadioButton("Square"); btnSubmit = new JButton("Draw"); btnSubmit.addActionListener(this); this.setBackground(Color.WHITE);
add(lblColor); add(cbRed); add(cbBlue); add(lblShapes); add(rbCircle); add(rbSquare); add(btnSubmit); shapes.add(rbCircle); shapes.add(rbSquare); } public void actionPerformed(ActionEvent a) { if(a.getSource() == btnSubmit) { if(cbRed.isSelected()&&cbBlue.isSelected()) { if(rbCircle.isSelected()) {
} else if(rbSquare.isSelected()) {
} } else if(cbRed.isSelected()) { if(rbCircle.isSelected()) {
} else if(rbSquare.isSelected()) {
} } else if(cbBlue.isSelected()) { if(rbCircle.isSelected()) {
} } else if(rbSquare.isSelected()) {
} } repaint(); } }
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