Using a text box to supply a graph
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.applet.*;
import java.text.*; // For formatting numbers
import javax.swing.*;
public class Final extends applet {
String xlabel = "Quantity";
String ylabel = "Price";
String xlabel1 = "Q0";
String ylabel1 = "P0";
String xlabel2 = "Q1";
String ylabel2 = "P1";
String ldemand = "D1";
String ldemand1 ="D2";
String lsupply = "S";
private Font font;
int xbeg = 50;
int ybeg = 50;
int xend = 350;
int yend = 350;
int xmiddle = ((xend-xbeg)/2)+xbeg;
int ymiddle = ((yend-xbeg)/2)+ybeg;
/* Set line widths */
BasicStroke stroke = new BasicStroke(2.0f);
BasicStroke wide = new BasicStroke(4.0f);
/* Set the length of each dash */
float dash1[] = {6.0f};
/* Set Dash - first # is width, second is miterlimit, third is dash length,
forth is dash_phase */
BasicStroke dashed = new BasicStroke(2.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
2.0f, dash1, 3.0f);
public void init()
{
font = new Font("Helvetica", Font.BOLD, 14);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
/* drawLine ( x1, y1, x2, y2) */
g2.setStroke(wide);
/* x-axis */
g2.draw(new Line2D.Double(xbeg, yend , xend, yend));
/* y-axis */
g2.draw(new Line2D.Double(xbeg, ybeg , xbeg, yend));
/* demand curve */
g2.setStroke(stroke);
g2.setColor(Color.green);
g2.draw(new Line2D.Double(xbeg + 20, ybeg + 20, xend - 20, yend - 20));
/*second demand curve*/
g2.setStroke(dashed);
g2.setColor(Color.blue);
g2.draw (new Line2D.Double(xbeg+100, ybeg+20, xend + 60, yend-20));
/* supply curve */
g2.setStroke(stroke);
g2.setColor(Color.red);
g2.draw(new Line2D.Double (xbeg + 20, yend - 20, xend - 20, ybeg +20 ));
/* Equilibrium Price and Quantity */
g2.setStroke(dashed);
g2.setColor(Color.red);
g2.draw(new Line2D.Double(xbeg, ymiddle, xmiddle, ymiddle));
g2.draw(new Line2D.Double(xmiddle, yend, xmiddle, ymiddle));
/* Second Equilibrium */
g2.setStroke(dashed);
g2.setColor(Color.blue);
g2.draw(new Line2D.Double(xbeg, ymiddle-38, xbeg+185,ymiddle-38));
g2.draw(new Line2D.Double(xbeg+185, ymiddle-38, xbeg+185, yend));
/* drawString ( String str, x, y) */
g2.setColor(Color.black);
g2.setFont(font);
g2.drawString(xlabel, xend-50, yend+15);
g2.drawString(ylabel, xbeg-40, ybeg+15);
g2.drawString(xlabel1, xmiddle-5, yend+15);
g2.drawString(ylabel1, xbeg-20, ymiddle+2);
g2.drawString(xlabel2, xbeg+185, yend+15);
g2.drawString(ylabel2, xbeg-20, ymiddle-38);
g2.drawString(ldemand, xend-25, yend-30);
g2.drawString(ldemand1, xend+55, yend-30);
g2.drawString(lsupply, xbeg+20, yend-30);
}
public static void main(String[] args) {
double v1 = 0;
String v1Val;
// Input values
v1Val = JOptionPane.showInputDialog("What is the value of the price floor" );
// Convert from a String to a double
v1 = Double.parseDouble(v1Val);
}
This right here is the program I'm writing. I'm having big time difficulty. Basically, what I'm trying to do is initially have a text box pop up, that asks the user for a number. Based on their response, theres one of two graphs that will than show up. My basic idea is to use an if, else statement for having these graphs appear. The only problem is that I'm completely unaware how to link the action of what the user types in the text box, to the type of graph that shows up. I don't know where you would put your public class final extends applet versus where you would put your public static void main(String[] args) { and where the if else statement should be