I'm a Java noob and learning AWT for the first time. I'm playing with a program that calculates a simple percentage of a mortgage and then outputs it to a picture using or AWT. The idea is to take a simple percentage (downPayment divided by mortgage) and output it into a simple colored bar to show how much of the mortgage has been paid off using AWT.
BTW, should I use AWT or should I use swing for the visual output?
I want to use rectangles to describe the amount of the mortgage paid off.
I used BlueJ to develop the code. The code compiles fine but when I run the method, BlueJ asks me to enter a parameter for mortgage1.paint and I can't go further. It won't output rectangles that should show how much of the mortgage is paid off.
Please suggest better code for my method below.
Thanks!
Peter
(My background: I studied art and literature in college previously but I got interested in I.T. Last spring, I took a semester of first year university java programming course.)
Code follows:
import java.awt.*;
/**
* class MortgageDraw - calculates simple percentage of mortgage and then outputs it to picture using or AWT.
* The idea is to take a simple percentage (downPayment divided by mortgage) and output it into
* colored bars using AWT.
*/
public class MortgageDraw extends Frame {
private int mortgage;
private int downPayment;
Rectangle outside;
Rectangle inside;
Stroke drawingStroke = new BasicStroke(3);
private Graphics g;
/**
* Constructor for objects of class MortgageDraw
*/
public MortgageDraw(int setMortgage, int setDownPayment)
{
mortgage = setMortgage;
downPayment = setDownPayment;
}
/**
* Method to show the colored output of mortgage down
* payment calculation.
*/
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.gray);
float insideBar1;
int insideBar2;
float percent = downPayment/mortgage;
g2d.fillRect(50, 50, 30, 200);
insideBar1 = percent * 200; //takes the percentage of the down payment and converts into pixel size for bar graph
insideBar2 = (int) insideBar1; //converts the float insideBar1 to an integer
g2d.setColor(Color.blue);
g2d.fillRect(50, 50, 30, insideBar2);
}
}
Edited by WingedPanther, 09 October 2009 - 07:41 AM.
add code tags (the # button)


Sign In
Create Account

Back to top









