problem about calling paint method

I have created a class to ask user input 3 floating-point numbers, using JApplet. the code list below

import java.awt.Graphics;

import javax.swing.*;

public class Numbers

{

double average,sum,product;

String result;

public void init()

{

String firstNumber,secondNumber,thirdNumber;

double num1,num2,num3;

firstNumber=JOptionPane.showInputDialog("enter first number");

secondNumber=JOptionPane.showInputDialog("Enter second number");

thirdNumber=JOptionPane.showInputDialog("Enter thrid number");

num1=Double.parseDouble(firstNumber);

num2=Double.parseDouble(secondNumber);

num3=Double.parseDouble(thirdNumber);

sum=num1+num2+num3;

product=num1*num2*num3;

average=(num1+num2+num3)/3;

result="";

if(num1<num2 && num2><num3)

result=result+num3+" is the largest number";

if(num2>num1 && num2>num3)

result=result+num2 +" is the largest number";

if(num1>num2 && num2>num3)

result=result+ num1+" is the largest number";

}

public void paint(Graphics g)

{

super.paint(g);

g.drawString("sum is"+sum,25,25);

g.drawString("product is"+ product,25,40);

g.drawString("average is"+ average,25,60);

g.drawString(" the largest number is"+result,25,80);

}

}

however, after I compiled, it gave me the error message as:

java:40: cannot resolve symbol

symbol : method paint (java.awt.Graphics)

location: class java.lang.Object

super.paint(g);

^(pointer should point to the dot)

don't know why,

Message was edited by:

ritchie_lin

[1735 byte] By [ritchie_lina] at [2007-11-27 10:27:25]
# 1

your class doesn't extend japplet or any other object for that matter. It does extend Object as all classes do, and Object has no paint method.

Consider changing:

public class Numbers

to

public class Numbers extends JApplet

Also, please use code tags next time you're posting code.

Addendum: If this is not the JApplet portion of your code, it still has to subclass a Swing component that can accept painting such as JPanel. Also, with Swing you use paintComponent rather than paint.

Message was edited by:

petes1234

petes1234a at 2007-7-28 17:45:22 > top of Java-index,Java Essentials,Java Programming...
# 2

Also, there is no need to override paint() anyway. You simply create 3 JLabels and assign text to the labels and then add the labels to the panel.

Finally, in a Swing application, you should never need to override the paint() method. If you really feel you need to do custom painting then you override the paintComponent() method of JComponent or JPanel.

camickra at 2007-7-28 17:45:22 > top of Java-index,Java Essentials,Java Programming...
# 3

> Also, there is no need to override paint() anyway.

> You simply create 3 JLabels and assign text to the

> labels and then add the labels to the panel.

Merde! that's what happens to me when I don't read the code carefully enough! I answer the superficial question but overlook the underlying problem.... sigh.

petes1234a at 2007-7-28 17:45:22 > top of Java-index,Java Essentials,Java Programming...
# 4

thx so much, yes, I forgot to extends the JApplet.

ritchie_lina at 2007-7-28 17:45:22 > top of Java-index,Java Essentials,Java Programming...
# 5

Why are you overriding paint to generate text? It would be much simpler to use JLabel or JTextField. Much simpler.

BigDaddyLoveHandlesa at 2007-7-28 17:45:22 > top of Java-index,Java Essentials,Java Programming...