Getting the chart to work in my Java program

I need to get my chart to work in the Java program I have developed. I have gotten the basic layout to work, but have not gotten the information from the calculations to work to make the graph. Let me know what I'm doing wrong.  MortgageCalculator.java Chart.java InterestRate-Term.txt
craftyfirstAsked:
Who is Participating?
 
for_yanCommented:

Replace your chart with this code:
and it should draw red line


// Program draws a bar chart.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*; //for the class Rectangle2D
import javax.swing.*;

public class Chart extends JFrame
{
   //to store the number of yeras

   private int number_years;

   // constructor
   public Chart( int number_months)
   {
            //set title for the window
            super( "Mortgage Payment Calculator Chart" );

            this.number_years = number_months/12 ;


	  // set the size of the window and let it be visible
	  setSize( 740, 490 );
	  setVisible( true );

   }

   // Draw chart
   // The method 'paint' will be executed right away after the constructor.
   public void paint( Graphics g )
   {
	  super.paint( g );

	  // creates a 2D object to draw
	  Graphics2D g2d = ( Graphics2D )g;

	  // set color and draw the bar for the first quarter sales
	  g2d.setColor( Color.blue );
	  g2d.drawLine(110,30,110,460);
	  g2d.drawLine(110,460,715,460);
          g2d.setFont(new Font("Sans Serif", Font.BOLD, 14));

	  g2d.drawString("A", 10, 150);
          g2d.drawString("M", 7, 170);
          g2d.drawString("O", 7, 190);
          g2d.drawString("U", 7, 210);
          g2d.drawString("N", 7, 230);
          g2d.drawString("T", 7, 250);

int amount = 200000;
int line = 9 ;
for(int i=0; i< 460; i+=41)
{

g2d.drawString("$"+ amount, 30, 55+i);
g2d.drawLine(105,line+i ,115,line+i);

amount =  amount- 20000;

}
g2d.drawString("YEARS", 300, 480);

if(number_years == 30)
{
int count =0;
for(int i=0; i<= 720; i+=30)
{
g2d.drawString(""+count , 107+i, 475);
g2d.drawLine(110+i,450,110+i,460);
count++;
}
g2d.setColor( Color.red );

g2d.drawLine(110,30,710,455);
}
else
if(number_years == 20)
{
int count =0;
for(int i=0; i<= 720; i+=45)
{
g2d.drawString(""+count , 107+i, 475);
g2d.drawLine(110+i,450,110+i,460);
count++;
}
g2d.setColor( Color.red );

g2d.drawLine(110,55,710,455);
}

else
if(number_years == 10)
{
int count =0;
for(int i=0; i<= 720; i+=60)
{
g2d.drawString(""+count , 107+i, 475);
g2d.drawLine(110+i,450,110+i,460);
count++;
}
g2d.setColor( Color.red );

g2d.drawLine(110,55,740,490);
}


g2d.setFont( new Font("Serif", Font.BOLD, 20));
g2d.drawString("Mortgage Payment Calculator Chart", 300, 50);

}

}

Open in new window

0
 
for_yanCommented:
It is not printing because yiou use number of months instaed of number of yaerss
and when it comapres number of montsh with 10 in your chart
it odes not go
to that picve which should darw your red line
0
 
for_yanCommented:

This code is from your caclulator part
    public void onButtonChart()
            {
                new Chart((int)(number_of_months));
                System.out.println(""+number_of_months);

        }

Open in new window



This code is from Chart:


public Chart( int number_years)
   {
            //set title for the window
            super( "Mortgage Payment Calculator Chart" );

            this.number_years = number_years ;


	  // set the size of the window and let it be visible
	  setSize( 740, 490 );
	  setVisible( true );

   }

So you pass number of months and read it as number of years.

Open in new window



0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
craftyfirstAuthor Commented:
Thanks. Looks like it working.
0
 
for_yanCommented:

Yes, it does.
In general it is definitely not a good practice to
use actual numbers in the paint(g) method.
You should first retrieve the size of yoour panel
and then exprsess everithyng as height and width mmultuiplied by the
ratio which you need for each point, line, etc
which you are drawing.
Otherwise, when you resize your window, your
drawing will be screwed up.
Well, for the first try - it is OK.
But in general, this is good to keep in mind.
 
0
 
craftyfirstAuthor Commented:
Thanks

I will keep this in mind for next time.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.