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.
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);
}
}
}
}
GUI in java: Run the below code before doing the actionlistener: import java.awt.*; import javax.swing.*; public...
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener { private JLabel titleLabel; private JTextField titleTxtFd; private JComboBox typeCmb; private ButtonGroup ratingGP; private JButton processBnt; private JButton endBnt; private JButton clearBnt; private JTextArea entriesTxtAr; private JRadioButton excellentRdBnt; private JRadioButton veryGoodRdBnt; private JRadioButton fairRdBnt; private JRadioButton poorRdBnt; private String ratingString; private final String EXCELLENT = "Excellent"; private final String VERYGOOD = "Very Good"; private final String FAIR = "Fair"; private final String POOR = "Poor"; String...
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame { public static void createAndShowGUI() { JFrame frame = new JFrame("Q1"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Font courierFont = new Font("Courier", Font.BOLD, 40); Font arialFont = new Font("Arial", Font.BOLD, 40); Font sansFont = new Font("Sans-serif", Font.BOLD, 20); JLabel label = new JLabel("Enter a word"); label.setFont(sansFont); Font font1 = new Font("Sans-serif", Font.BOLD, 40); ...
please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....
Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener { public static final int width = 500; public static final int height = 300; public static final int no_of_lines = 10; public static final int chars_per_line = 20; private JTextArea lan1; private JTextArea lan2; public static void main(String[] args){ translatorApp gui = new translatorApp();...
JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...
Simple java questions Q2.java: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Q2 extends JFrame { public static void createAndShowGUI() { JFrame frame = new JFrame("Lab"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Font font = new Font("Sans-serif", Font.BOLD, 20); JLabel label = new JLabel("Enter a word"); label.setFont(font); JTextField textField = new JTextField(10); textField.setFont(font); JButton button = new JButton("Enter"); button.setFont(font); Font font1 = new Font("Sans-serif", Font.BOLD, 30); JCheckBox sansSerif = new JCheckBox("Sans-serif"); sansSerif.setFont(font1); JCheckBox serif= new JCheckBox("Serif"); serif.setFont(font1); Font font2 = new Font("Sans-serif", Font.ITALIC, 15); JRadioButton...
With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...