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 is 399, 399. * * Our initial Position can be either 200, 0 (probably easier) * or 200, 399 (need to draw in the "negative" direction). * * We will also talk about how to scale this and modify it. * */ public void paint (Graphics g) { Graphics2D g2 = (Graphics2D)g; int currentPositionX, currentPositionY, position; double currentAngle; currentPositionX = 200; currentPositionY = 200; currentAngle = 0.0; for (position = 0; position < drawString.length(); position++) { if (drawString.charAt(position) == 'F') { // Draw 5 units along current direction Line2D line = new Line2D.Double(currentPositionX, currentPositionY, currentPositionX + 5.0 * Math.sin(currentAngle), currentPositionY + 5.0 * Math.cos(currentAngle)); g2.draw(line); currentPositionX = (int) (currentPositionX + 5.0 * Math.sin(currentAngle)); currentPositionY = (int) (currentPositionY + 5.0 * Math.cos(currentAngle)); } else if (drawString.charAt(position) == '-') { currentAngle -= angleIncrement; } else if (drawString.charAt(position) == '+') { currentAngle += angleIncrement; } else if (drawString.charAt(position) == '[') { } else if (drawString.charAt(position) == ']') { } } } Completed GUI:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; /** * */ /** * The GUI for Project 2 as we developed it in * class. You may alter this code for use with Project 2. * * */ public class Project2GUI extends JFrame implements ActionListener { protected JTextField lhs[], rhs[], angle, startSymbol; protected JButton drawButton; protected JSpinner iterationSpinner; protected JLabel ruleLabels[], angleLabel, startLabel, spinnerLabel; protected String rhsValue[], lhsValue[]; protected DrawingCanvas myCanvas; public Project2GUI () { rhsValue = new String[5]; lhsValue = new String[5]; this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.add(buildGUI()); this.pack(); this.setVisible(true); } /** * buildGUI builds the graphics components that make up * our GUI for Project 2. It returns a panel that contains * all of the components. It also hooks up the Draw Button * with this object as an actionListener. * * @return JPanel with the GUI. */ private JPanel buildGUI() { JPanel ourGUI = new JPanel(); GridBagConstraints gbc = new GridBagConstraints(); ourGUI.setLayout(new GridBagLayout()); /* * Allocate the widgets and add them to the GUI. I've * changed this to use a gridBagLayout to allow us a better * way to arrange the GUI. * * The routine is too long. It should be shortened * and simplified. We will do this to it in class. */ buildAngle(ourGUI, gbc); buildStartSymbol(ourGUI, gbc); drawButton = new JButton ("Draw"); drawButton.addActionListener(this); gbc.gridx = 0; gbc.gridy = 9; gbc.gridwidth = 10; gbc.anchor = GridBagConstraints.CENTER; ourGUI.add(drawButton, gbc); buildSpinner(ourGUI, gbc); lhs = new JTextField[5]; rhs = new JTextField[5]; ruleLabels = new JLabel[5]; for (int i = 0; i < 5; i++) { buildRules(ourGUI, gbc, i); } JLabel title = new JLabel ("L-System Project"); title.setSize(20, 200); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 10; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; ourGUI.add(title, gbc); myCanvas = new DrawingCanvas(); myCanvas.setAngleIncrement(90.0); myCanvas.setDrawString("F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F"); // myCanvas.setDrawString("F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F"); gbc.gridx = 0; gbc.gridy = 10; gbc.gridwidth = 10; gbc.gridheight = 10; ourGUI.add(myCanvas, gbc); return ourGUI; } /** * * The label and textfield for the start symbol * @param ourGUI The panel to which the items are to be added. * @param gbc The GridBagConstraint for this item -- should be removed -- local. */ private void buildStartSymbol(JPanel ourGUI, GridBagConstraints gbc) { startLabel = new JLabel ("Start Symbol: "); startSymbol = new JTextField(2); gbc.gridx = 8; gbc.gridy = 4; ourGUI.add(startLabel, gbc); gbc.gridx = 9; gbc.fill = GridBagConstraints.HORIZONTAL; ourGUI.add(startSymbol, gbc); gbc.fill = GridBagConstraints.NONE; } /** * * Build the label and text field for the angle input. * @param ourGUI The panel that the items are to be added to. * @param gbc The GridBagConstraint for this item -- should be removed -- local. */ private void buildAngle(JPanel ourGUI, GridBagConstraints gbc) { angleLabel = new JLabel ("Angle: "); angle = new JTextField (4); gbc.gridx = 8; gbc.gridy = 2; gbc.gridheight = 1; gbc.gridwidth = 1; ourGUI.add(angleLabel, gbc); gbc.gridx = 9; gbc.fill = GridBagConstraints.HORIZONTAL; ourGUI.add(angle, gbc); gbc.fill = GridBagConstraints.NONE; } /** * * Build the spinner that controls the number of times the Productions get * expanded. Add it to the given JPanel. * @param ourGUI The panel that the Spinner is to be added to. * @param gbc The gridBagConstraint for this item -- should be removed -- local. */ private void buildSpinner(JPanel ourGUI, GridBagConstraints gbc) { spinnerLabel = new JLabel("Number of Iterations: "); gbc.gridx = 8; gbc.gridy = 6; gbc.gridwidth = 1; ourGUI.add(spinnerLabel, gbc); iterationSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 20, 1)); gbc.gridx = 9; gbc.gridy = 6; gbc.gridwidth = 1; gbc.fill = GridBagConstraints.HORIZONTAL; ourGUI.add(iterationSpinner, gbc); gbc.fill = GridBagConstraints.NONE; } /** * Build the fields for the rules into the JPanel given. * @param ourGUI The panel that the rules are to be added to... * @param gbc The gridBagConstraint for this panel -- should be removed. * @param i The number of the rule that we are adding. */ private void buildRules(JPanel ourGUI, GridBagConstraints gbc, int i) { ruleLabels[i] = new JLabel("Rule "+i+" : "); gbc.gridx = 1; gbc.gridy = i+2; gbc.gridheight = 1; gbc.gridwidth = 1; ourGUI.add(ruleLabels[i], gbc); gbc.gridx = 2; lhs[i] = new JTextField(2); ourGUI.add(lhs[i], gbc); gbc.gridwidth = 5; gbc.gridx = 3; rhs[i] = new JTextField(10); ourGUI.add(rhs[i], gbc); } /** * grab actions and react to them.... This will be * used to detect the press of the draw button and * gather the information from the different fields. * * I've added code to pop up dialogs for various problems that * may occur with the data. The tests on the data are not * complete. They handle a number of simple errors. * */ public void actionPerformed(ActionEvent event) { if (event.getSource() == drawButton) { /* * Right now we will print out the state of * all the input fields... */ for (int i = 0; i < 5; i++) { System.out.println("Rule "+i+": " + lhs[i].getText() + " -> " + rhs[i].getText()); rhsValue[i] = rhs[i].getText().trim().toUpperCase(); lhsValue[i] = lhs[i].getText().trim().toUpperCase(); } System.out.println("Start Symbol = " + startSymbol.getText()); String ourStartSymbol = startSymbol.getText(); ourStartSymbol.trim(); if (ourStartSymbol.length() > 1) { JOptionPane.showMessageDialog(this, "Start Symbol must be a single character! Multiple characters found.", "Start Symbol Error", JOptionPane.ERROR_MESSAGE); } else if (ourStartSymbol.length() == 0) { JOptionPane.showMessageDialog(this, "You must have a start symbol! No start symbol found.", "Start Symbol Error", JOptionPane.ERROR_MESSAGE); } System.out.println("Angle = " + angle.getText()); try { float angleValue = Float.parseFloat(angle.getText()); myCanvas.setAngleIncrement(angleValue); } catch (NumberFormatException e) { // bad angle -- should notify user and try again... JOptionPane.showMessageDialog(this, "Angle must be a valid floating point number. Try again.", "Angle Error", JOptionPane.ERROR_MESSAGE); } myCanvas.setDrawString("F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F"); System.out.println("Number of iterations = " + iterationSpinner.getValue()); myCanvas.repaint(); } } public static void main(String[] args) { Project2GUI project2 = new Project2GUI(); } }
package lsystem;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import ch.aplu.turtle.Turtle; //get "https://sourceforge.net/projects/jturtle/" and add to build path
public class LsysGenerate {
public static void main(String[] args) {
HashMap<String , String> rules = new
HashMap<>();
rules.put("+", "+");
rules.put("-", "-");
rules.put("f", "f");
int noRules;
String axiom;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting symbol/
axiom");
axiom = sc.nextLine();
System.out.println("Enter no of rules");
noRules=sc.nextInt();
sc.nextLine();
System.out.println("Enter rules of format
<symbol>=<Expansion> (no spaces, only non caps,allowed
specials{[,],-,+)}");
for(int i=0;i<noRules;i++)
{
String temp = sc.nextLine();
String[] arr=temp.split("=");
rules.put(arr[0], arr[1]);
}
System.out.println("Enter no of iterations..");
int iter = sc.nextInt();
sc.nextLine();
Queue<String> q = new LinkedList<>();
System.out.println(axiom+" "+noRules+" "+iter);
for(int i=0;i<iter;i++)
{
StringBuffer next = new StringBuffer();
for(int j=0;j< axiom.length();j++)
{
if(axiom.charAt(j)=='[') {
q.add(next.toString());
}else if(axiom.charAt(j)==']') {
String top = q.element();
q.remove(top);
next.append(top);
}else {
next.append(rules.get(""+axiom.charAt(j)));
}
}
axiom = next.toString();
}
System.out.println("This is the expanded string representation\n"+axiom);
//logic to draw from string commands
//You can change the origin of turtle and customize step
size
//default angle is taken as 90 degrees you can customize it from input
//reduce step size to view bigger "plants" inside
window
double step = 10; double angle =90;
Turtle turtle = new Turtle();
for(int i=0;i<axiom.length();i++) {
if(axiom.charAt(i)=='+') {
turtle.left(angle);
continue;
}
if(axiom.charAt(i)=='-') {
turtle.right(angle);
continue;
}
turtle.forward(step); //will take a step forward for every symbol
except (+,-)
}
}
}
Here is a screenshot of result. Using single variable and single rule.
Growing Plant program following guidelines Drawing Canvas Code: import java.awt.Canvas; import java.awt.*; import java.awt.geom.*; /** *...
tart from the following code, and add Action Listener to make it functional to do the temperature conversion in the direction of the arrow that is clicked. . (Need about 10 lines) Note: import javax.swing.*; import java.awt.GridLayout; import java.awt.event.*; import java.text.DecimalFormat; public class temperatureConverter extends JFrame { public static void main(String[] args) { JFrame frame = new temperatureConverter(); frame.setTitle("Temp"); frame.setSize(200, 100); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public temperatureConverter() { JLabel lblC = new...
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); ...
***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...
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();...
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....
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[]...
Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file. Program...
Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...
I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 { public TextArea courseInput; public Label textAreaLabel; public JButton addData; public Dialog confirmDialog; HashMap<Integer, ArrayList<String>> students; public Fall_2017(){ courseInput = new TextArea(20, 40); textAreaLabel = new Label("Student's data:"); addData = new JButton("Add...
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...