Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

GUI in java: Run the below code before doing the actionlistener: import java.awt

ID: 3575731 • Letter: G

Question

GUI in java:

Run the below code before doing the actionlistener:

import java.awt.*;
import javax.swing.*;

public class DrawingFrame extends JFrame {
   JButton loadButton, saveButton, drawButton;
   JComboBox colorList, shapesList;
   JTextField parametersTextField;
  
   DrawingFrame() {
       super("Drawing Application");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JToolBar toolbar = new JToolBar();
       toolbar.setRollover(true);

       toolbar.add(loadButton=new JButton("Load"));
       toolbar.add(saveButton=new JButton("Save"));

       toolbar.addSeparator();

       toolbar.add(drawButton=new JButton("Draw"));
      
       toolbar.addSeparator();
       toolbar.addSeparator();

       toolbar.add(new JLabel("Shape"));
       shapesList=new JComboBox(new String[] { "Circle", "Rectangle", "Line","Triangle" });
       toolbar.add(shapesList);
      
       toolbar.addSeparator();

       toolbar.add(new JLabel("Parameters"));
       toolbar.add(parametersTextField=new JTextField());

       toolbar.add(new JLabel("Color "));
       colorList=new JComboBox(new String[] { "black", "red", "blue",
               "green", "yellow", "orange", "pink", "magenta", "cyan",
               "lightGray", "darkGray", "gray", "white" });
       toolbar.add(colorList);

       getContentPane().add(toolbar, BorderLayout.NORTH);
      
   }

   public static void main(final String args[]) {
       DrawingFrame frame = new DrawingFrame();
       frame.setBounds(100, 100, 600, 500);
       frame.setVisible(true);
   }
}

Now,Add Actionlistener to each below clicked buttons:

- The user can select a shape from a list of shapes (circle, rectangle, triangle, line) and specify its parameters in the field Parameters. The color is selected from a list of colors.
- The parameters for each shape are as follows:
o Circle: the center of the circle (x,y) and the radius r.
o Rectangle: the coordinates of the left upper corner (x,y), the width w and the height h.
o Triangle: coordinates of the three vertices of the triangle (x1,y1), (x2,y2), (x3,y3)
o Line: the coordinates of the 2 end points (x1,y1), (x2,y2).
For example if the user wants to draw a blue circle with center (100,150) and radius 50, he has to:
1.Select circle from the shapes list.
2.Enter 100,150,50 in the parameters text field
3.Select blue from the colors list
4.Press the button draw.
- Note that if the parameters list the user enters is not appropriate for the shape he selected, he gets and error message.

Explanation / Answer

I have readd the complete question and I am answering the same. Here, it is mentioned to add actionlisteners to the buttons and to check these conditions on pressing draw button. So, I am not supposed to be concerned with what the LOAD and SAVE button does as it is not provided in question. Moreover, whether draw method should draw the actual shape or not is also not specified so I left it as it is. This is just the code having action listener and the condition checking as per mentioned in the question. Do tell me if there is any modification required, Thank you.

Code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class DrawingFrame extends JFrame implements ActionListener {

JButton loadButton, saveButton, drawButton;
JComboBox colorList, shapesList;
JTextField parametersTextField;
JLabel msg;

DrawingFrame() {
super("Drawing Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
toolbar.add(loadButton = new JButton("Load"));
toolbar.add(saveButton = new JButton("Save"));
toolbar.addSeparator();
toolbar.add(drawButton = new JButton("Draw"));

toolbar.addSeparator();
toolbar.addSeparator();
toolbar.add(new JLabel("Shape"));
shapesList = new JComboBox(new String[]{"Circle", "Rectangle", "Line", "Triangle"});
toolbar.add(shapesList);

toolbar.addSeparator();
toolbar.add(new JLabel("Parameters"));
toolbar.add(parametersTextField = new JTextField());
toolbar.add(new JLabel("Color "));
colorList = new JComboBox(new String[]{"black", "red", "blue",
"green", "yellow", "orange", "pink", "magenta", "cyan",
"lightGray", "darkGray", "gray", "white"});
toolbar.add(colorList);

getContentPane().add(toolbar, BorderLayout.NORTH);

getContentPane().add(msg = new JLabel("msg"),BorderLayout.AFTER_LAST_LINE);

loadButton.addActionListener(this);
saveButton.addActionListener(this);
drawButton.addActionListener(this);
}

public static void main(final String args[]) {
DrawingFrame frame = new DrawingFrame();
frame.setBounds(100, 100, 600, 500);
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String buttonClicked = e.getActionCommand();

if (buttonClicked == "Draw") {
String shape = shapesList.getSelectedItem().toString();
String param = parametersTextField.getText();
String color = colorList.getSelectedItem().toString();

String[] parameters = param.split(",");

if (shape.equals("Circle") && parameters.length != 3) {
msg.setText("Circle requires 3 parameters: center(x,y), radius r");
}
  
else if (shape.equals("Rectangle") && parameters.length != 4) {
msg.setText("Rectangle requires 4 parameters: upper left corner (x,y), width w, height h");
}
  
else if (shape.equals("Triangle") && parameters.length != 6) {
msg.setText("Triangle requires 6 parameters: co-ordinates of 3 vertices (x1,y1), (x2,y2), (x3,y3)");
}
  
else if (shape.equals("Line") && parameters.length != 4) {
msg.setText("Line requires 4 parameters: 2 end points (x1,y1), (x2,y2)");
}
  
else
{
msg.setText("You selected to draw " + shape + " with " + color + " color"+" with parameters: "+param);
  
}
}
}
}