Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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
0
craftyfirst
Asked:
craftyfirst
  • 4
  • 2
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now