This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Paint() method...  Easy Question" Watch "Paint() method...  Easy Question" New topic
Author

Paint() method... Easy Question

Mark Owens
Greenhorn

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...
Mark
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();
btn.addActionListener(listener);
add(btn);
}
}
sai
Greenhorn

Joined: Mar 01, 2000
Posts: 23

public void paint(Graphics g)
{
g.drawString("ActionPerformed",10,10);
}
alberto sierra
Greenhorn

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) {
my_applet.paintString();
}
}
public class ListenerTest extends Applet {
public void init() {
setSize(100, 100);
setLayout(null);
Button btn = new Button("OK");
MyActionListener listener = new MyActionListener(this);
btn.addActionListener(listener);
btn.setBounds(0, 0, 30, 30);
add(btn);
}
paintString() {

Graphics g = getGraphics();
g.setColor(Color.black);
g.drawString("Hello", 40, 20);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Paint() method... Easy Question
 
Similar Threads
Sorry - Newbie question...
Not able to run ActionListener
problem with parse int method
ActonListener
java.awt.*; and java.awt.event.*