1

I want to use drawString() once a button is pushed to draw the answer on my applet but I can't figure it out. I have try many ways to do and my program compiles but won't drawString() when the button is pushed please help.

import java.applet.Applet;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.awt.*;

public class FortuneTellerApplet extends Applet {

    Image image;
    Button getFortune = new Button("Get Your Fortune");
    Button tryAgain = new Button("Clear And Try Again");
    TextField input = new TextField("Enter Question Here", 30);

    public void init() {

        image = getImage(getDocumentBase(), "webimages/crystalball.jpg");

        getFortune.setBackground(Color.black);
        getFortune.setForeground(Color.orange);
        tryAgain.setBackground(Color.black);
        tryAgain.setForeground(Color.orange);
        input.setBackground(Color.black);
        input.setForeground(Color.orange);

        setLayout(new FlowLayout());
        setBackground(Color.green);
        add(getFortune);
        add(tryAgain);
        add(input);

        MyHandler handler = new MyHandler();
        getFortune.addActionListener(handler);
        tryAgain.addActionListener(handler);

        }

    public void paint(Graphics g) {

        g.drawImage(image, 12, 34, this);

        }

    public class MyHandler extends Button implements ActionListener {

        public void actionPerformed(ActionEvent ev) {

        if (ev.getSource()==getFortune) {

                 // >>>>>>>>> I want be able to use drawString() here <<<<<<<

        } else if (ev.getSource()==tryAgain) {

            input.setText("");
            input.requestFocus();

            }
        }   
    }
}
Brandon Cruz
  • 11
  • 1
  • 2

2 Answers2

3

Do you need to do custom painting?

Just use a Label that initially defaults to an empty string. Then when the you want to display the answer you invoke the setText() method on the label to display the text.

Why are you using AWT? I would learn Swing. I don't use AWT but I would guess that if you are going to do custom painting, then you should have a super.paint() at the start of your painting method. I know this is important for Swing.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I agree (1+), but he'll need to use a *Label* not a *JLabel* since this is an AWT Applet, not a Swing JApplet. – Hovercraft Full Of Eels Oct 30 '11 at 16:03
  • 1
    Oops, just a habit of always adding the "J" since I usually answer Swing questions. The "J" has been removed. – camickr Oct 30 '11 at 16:07
  • Okay, this is my first java applet it is for school, I like the label idea but how do I postion the items on the applet? I have done it in JFrame application but not applet. – Brandon Cruz Oct 30 '11 at 16:38
  • lol never mind just figured out how to postion things, now gonna try the label idea. – Brandon Cruz Oct 30 '11 at 16:45
  • If you have used a JFrame before then why are you not using a JApplet? Positioning components on a JApplet is the same as positioning components on a JFrame. You use the appropriate combination of layout managers. – camickr Oct 30 '11 at 16:48
  • Yeah just figured it out setLayout(null); and then setBounds. I used Jframe And Jpanel, was use to adding the items to the panel and positioning them. Everyone been a great help so far thanks. – Brandon Cruz Oct 30 '11 at 16:58
  • *"just figured it out setLayout(null);"* No you haven't. Use layouts for a robust GUI. – Andrew Thompson Oct 30 '11 at 17:01
2

Use a boolean value in paint method, like this:

// Add this to the top
boolean stringVisible = false;    

// Change paint method accordingly
public void paint(Graphics g) {
  g.drawImage(image, 12, 34, this);
  if( stringVisible )
  {
    // draw string
  }
}

Set the boolean value to true when button is pressed, it must be false initially.

quaertym
  • 3,917
  • 2
  • 29
  • 41