Results 1 to 3 of 3
- 12-02-2009, 07:57 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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 beLast edited by MapleLeafRag; 12-02-2009 at 08:00 PM.
- 12-02-2009, 10:56 PM #2
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
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
- 12-02-2009, 11:04 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
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
-
creating a graph from a text file
By chinolee in forum New To JavaReplies: 3Last Post: 11-23-2009, 08:36 AM -
ECG Graph
By bharath333 in forum Java AppletsReplies: 4Last Post: 02-14-2009, 10:26 PM -
how to use live validation with autocomplete in dojo text boxes in <s:text box>
By subashm28 in forum Suggestions & FeedbackReplies: 2Last Post: 01-23-2009, 04:09 PM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM -
Bar Graph
By Zosden in forum Advanced JavaReplies: 2Last Post: 04-28-2008, 06:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks