Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Paint() method... Easy Question

 
Mark Owens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public void paint(Graphics g)
{
g.drawString("ActionPerformed",10,10);
}
 
alberto sierra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic