Jump to content

Need help with Simple AWT program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
telemachus

telemachus

    Newbie

  • Members
  • Pip
  • 4 posts
Hi all,

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)


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
The paint method is a method from Canvas that is auto called by it. So without a Canvas you'll have a hard time :D

I believe that when you manually want to call the paint method you must give canvasName.getGraphics() as parameter.

So you should make a Canvas and add it to your GUI.

But that's not the only reason why you're not getting any output view. Because you must make the frame visible: using setVisible(true) (maybe "pack()" before setting visible if it ain't working) ... and maybe a setSize(x,y)

#3
telemachus

telemachus

    Newbie

  • Members
  • Pip
  • 4 posts
Hi Oxano,
Thanks for the tip. That was very useful!
Peter