Results 1 to 3 of 3
  1. #1
    MapleLeafRag is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default 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
    Last edited by MapleLeafRag; 12-02-2009 at 08:00 PM.

  2. #2
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Are you sure you can use the work 'Final' for your class name? i know that final is a reserved word.

    Will have a look

  3. #3
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    I haven't really played with graphics and graphs etc.. but when i run the program as an 'applet' all it loads is the graphics in there standard position in an applet window. But If i run it as an application it loads the main JPanel box and then does nothing (mostly because your main does nothing else... - Not sure how to accompany them together!

Similar Threads

  1. creating a graph from a text file
    By chinolee in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 08:36 AM
  2. ECG Graph
    By bharath333 in forum Java Applets
    Replies: 4
    Last Post: 02-14-2009, 10:26 PM
  3. Replies: 2
    Last Post: 01-23-2009, 04:09 PM
  4. Replies: 0
    Last Post: 05-26-2008, 04:26 PM
  5. Bar Graph
    By Zosden in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2008, 06:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •