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);
...
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...
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...
***This is a JAVA question***
------------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingPanel implements ActionListener {
public static final int DELAY = 50; // delay between repaints in
millis
private static final String DUMP_IMAGE_PROPERTY_NAME =
"drawingpanel.save";
private static String TARGET_IMAGE_FILE_NAME = null;
private static final boolean PRETTY = true; // true to
anti-alias
private static boolean DUMP_IMAGE = true; // true to write
DrawingPanel to file
private int width, height; // dimensions...
If anyone can please convert from Java to python. Thank you!! import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class CalenderProgram{ static JLabel lblMonth, lblYear; static JButton btnPrev, btnNext; static JTable tblCalendar; static JComboBox cmbYear; static JFrame frmMain; static Container pane; static DefaultTableModel mtblCalendar; //Table model static JScrollPane stblCalendar; //The scrollpane static JPanel pnlCalendar; static int realYear, realMonth, realDay, currentYear, currentMonth; public static void main (String args[]){ //Look and feel try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException...
Growing Plant program following guidelines
Drawing Canvas Code:
import java.awt.Canvas;
import java.awt.*;
import java.awt.geom.*;
/**
*
*/
/**
*/
public class DrawingCanvas extends Canvas {
protected String drawString;
protected double angleIncrement;
DrawingCanvas() {
this.setPreferredSize(new Dimension(400, 400));
}
public void setDrawString(String s) {
drawString = s;
}
public void setAngleIncrement(double d) {
angleIncrement = Math.PI * d/ 180.0;
}
/**
* Paint routine for our canvas. The upper Left hand corner
* is 0, 0 and the lower right hand corner...