# Graphs and if else statement

• 12-03-2009, 09:16 PM
MapleLeafRag
Graphs and if else statement
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.applet.*;
import java.text.*; // For formatting numbers
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import java.awt.Graphics;

public class Finals extends JApplet{

double input;

public void init(){

String firstNumber;
String secondNumber;
String thirdNumber;
String fourthNumber;
String fifthNumber;
String sixthNumber;
String seventhNumber;

double number1;
double number2;
double number3;
double number4;
double number5;
double number6;
double number7;

firstNumber = JOptionPane.showInputDialog("What is the value of the price floor");
secondNumber = JOptionPane.showInputDialog("What is the equilbrium price?");
thirdNumber = JOptionPane.showInputDialog("What is the equilbrium quantity?");
fourthNumber = JOptionPane.showInputDialog("We now need another point along the demand curve, what is the price of this point?");
fifthNumber = JOptionPane.showInputDialog("What is the corresponding quantity?");
sixthNumber = JOptionPane.showInputDialog("We now need another point on the supply schedule, what is the price of the this point?");
seventhNumber = JOptionPane.showInputDialog("What is the corresponding quantity?");

number1 = Double.parseDouble(firstNumber);
number2 = Double.parseDouble(secondNumber);
number3 = Double.parseDouble(thirdNumber);
number4 = Double.parseDouble(fourthNumber);
number5 = Double.parseDouble(fifthNumber);
number6 = Double.parseDouble(sixthNumber);
number7 = Double.parseDouble(seventhNumber);

input = number1;
}

if(number1==0)
public void paint(Graphics g){

super.paint(g);

g.drawRect(15, 10, 270, 20);

g.drawString("The price floor is " + input, 25, 25);

String xlabel = "Quantity";
String ylabel = "Price";
String xlabel1 = "Q*";
String ylabel1 = "P";
String ldemand = "D*";
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;

BasicStroke stroke = new BasicStroke(2.0f);
BasicStroke wide = new BasicStroke(4.0f);

float dash1[] = {6.0f};

BasicStroke dashed = new BasicStroke(2.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
2.0f, dash1, 3.0f);

{
font = new Font("Helvetica", Font.BOLD, 14);
}

{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);

g2.setStroke(wide);
g2.draw(new Line2D.Double(xbeg, yend , xend, yend));
g2.draw(new Line2D.Double(xbeg, ybeg , xbeg, yend));

//Demand
g2.setStroke(stroke);
g2.setColor(Color.green);
g2.draw(new Line2D.Double(xbeg + 20, ybeg + 20, xend - 20, yend - 20));

//Supply
g2.setStroke(stroke);
g2.setColor(Color.red);
g2.draw(new Line2D.Double (xbeg + 20, yend - 20, xend - 20, ybeg +20 ));

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));

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(ldemand, xend-25, yend-30);

}
}

else{
public void paint1(Graphics g){
g.drawRect(15, 10, 270, 20);

}
}

Does anybody know whats wrong with my if/else statement? I'm just tinkering around. Basically, if the user enters 0 in the first box, I would like a graph to appear, otherwise, I just want a rectangle. Any helps is greatly appreciated!
• 12-03-2009, 11:56 PM
xcallmejudasx
Post code tags around code please. It's nearly impossible to check for matching brackets and such when there's no indentation.