简体   繁体   English

如何在我的Java小程序中的paint()方法之外使用Graphics?

[英]How to use Graphics out side the paint() method in my Java applet?

I want to use drawString() once a button is pushed to draw the answer on my applet but I can't figure it out. 一旦按下按钮以在我的applet上绘制答案,我想使用drawString(),但我无法弄清楚。 I have try many ways to do and my program compiles but won't drawString() when the button is pushed please help. 我尝试了很多方法,程序可以编译,但是按下按钮时不会drawString()。

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();

            }
        }   
    }
}

Do you need to do custom painting? 您需要做定制绘画吗?

Just use a Label that initially defaults to an empty string. 只需使用最初默认为空字符串的Label Then when the you want to display the answer you invoke the setText() method on the label to display the text. 然后,当您要显示答案时,可以在标签上调用setText()方法以显示文本。

Why are you using AWT? 为什么要使用AWT? I would learn Swing. 我会学习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. 我不使用AWT,但是我想如果您要进行自定义绘画,那么在绘画方法的开始应该有一个super.paint() I know this is important for Swing. 我知道这对Swing很重要。

Use a boolean value in paint method, like this: 在paint方法中使用布尔值,如下所示:

// 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. 按下按钮时,将布尔值设置为true,最初必须为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM