This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Applets and the fly likes Paint() method...  Easy Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Paint() method...  Easy Question" Watch "Paint() method...  Easy Question" New topic

Paint() method... Easy Question

Mark Owens

Joined: Feb 24, 2000
Posts: 26
There is one good thing about being a newbie: Your questions are usually easier to answer...
Can I put the Paint() Method inside another method?
I am trying to get an eventlistener applet running from an example in a book. The line in the example:
System.out.println("action performed");
in the program does not work because it is an applet (I found this out the hard way). So I tried replacing it with
public void paint(Graphics g){
g.drawString("action performed");
but then the program wont compile. It does not seem to like the paint() method inside the ActionListener method. How can I get it to print a line of text in my ActionListener?
All help appreciated. Sorry for the newbie question...
Here is my code if you are interested... The question is what to replace the System.out with and where to put it...
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

class MyActionListener implements ActionListener
public void actionPerformed(ActionEvent ae)
System.out.println("Action Performed");
public class ListenerTest extends Applet
public void init()
Button btn = new Button("OK");
MyActionListener listener = new MyActionListener();

Joined: Mar 01, 2000
Posts: 23

public void paint(Graphics g)
alberto sierra

Joined: Mar 21, 2000
Posts: 4
You can try following:

class MyActionListener implements ActionListener {
ListenerTest my_applet;
public MyActionListener(ListenerTest my_applet) {
this.my_applet = my_applet;

public void actionPerformed(ActionEvent ae) {
public class ListenerTest extends Applet {
public void init() {
setSize(100, 100);
Button btn = new Button("OK");
MyActionListener listener = new MyActionListener(this);
btn.setBounds(0, 0, 30, 30);
paintString() {

Graphics g = getGraphics();
g.drawString("Hello", 40, 20);
I agree. Here's the link:
subject: Paint() method... Easy Question
jQuery in Action, 3rd edition