This is my code so far......

import java.applet.Applet;
import java.awt.*;
import javax.swing.JOptionPane;

public class HelloWorld extends Applet {
    Font myFont = new Font ("Times Roman", Font.BOLD, 25);

    public void paint (Graphics g) {
        g.setFont (myFont);
        g.setColor (Color.BLUE);
        g.drawString ("My Name" , 20, 30);
            

                String first;
                first = JOptionPane.showInputDialog (null, "Enter first number: ");
                double f = Double.parseDouble (first);
                String second;
                second = JOptionPane.showInputDialog (null, "Enter second number: ");
                double s = Double.parseDouble (second);
                String third;
                third = JOptionPane.showInputDialog(null, "Enter third number: ");
                double t = Double.parseDouble (third);
                double avg = (f + s + t) / 3;
                JOptionPane.showMessageDialog (null, "Average = " + avg);
            }
    }

How do I print the average that I will get after entering three numbers in the applet?

Recommended Answers

All 3 Replies

Find first better place for JOptionPanes then paint method.

Here is a good example
http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/Printingsetting.htm
at its base you can build your program
Phase1
1. change "extends JFrame" --> "extends JApplet"
2. add "import javax.swing.JApplet;"
3. comment "setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);"
4. comment "public static void main(String arg[]) {..."
5. run program
Phase2
6. Inside public class PrinterSettingUpDialogPrint add new JButton "OPTION"
7. To this button add ActionListener with method "canvas.option();"
8. Inside class DrawingCanvas write your method

// inside is Your code
    public void option() {
        String first;
        first = JOptionPane.showInputDialog(null, "Enter first number: ");
        double f = Double.parseDouble(first);
        String second;
        second = JOptionPane.showInputDialog(null, "Enter second number: ");
        double s = Double.parseDouble(second);
        String third;
        third = JOptionPane.showInputDialog(null, "Enter third number: ");
        double t = Double.parseDouble(third);
        avg = (f + s + t) / 3;
        repaint(); // to refresh
        JOptionPane.showMessageDialog(null, "Average = " + avg);
    }

9. avg varible make local and declare as private "private double avg;
10. change something method

public void paintContent(Graphics2D g2D, int w, int h) {
        g2D.setFont(font);
        //"Java 2D" --> "Average = " + avg
        g2D.drawString("Average = " + avg, (float) (0.5 * (w - fontMetrics.stringWidth("Java 2D"))), (float) (0.5 * h - 1.25 * fontMetrics.getHeight()));
    }

11. run
I'ts all
12. debug

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.