Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

The Frame and Paint call  RSS feed

 
david solomon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can I call the paint method, any idea to what is needed to be done so I can get a string appears in the second window once the 1st button is pressed?.
Best of regards and have a happy Christmas and a happy New years
David
-----------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
//========================================================================
public class AppWindow
{
ParFrame f ;
//------------------------------------------------------------
public static void main(String [] args)
{
//setting the parameter frame
ParFrame f = new ParFrame();
f.setSize(200, 200);
f.setBackground(Color.GREEN);
f.addWindowListener(new WindowAdapter() { public void
windowClosing(WindowEvent e) {System.exit(0);}});
f.show();
}
//------------------------------------------------------------
}
//========================================================================
class WindowGraph extends Window {
Label label;
//------------------------------------------------------------
WindowGraph(ParFrame af)
{
super(af);
//next, is for the next grapics window
setLayout(new FlowLayout());
label = new Label(" This is from WindowGraph class ");
add(label);
}
//------------------------------------------------------------
public void paint (Graphics g)
{
g.drawString("xxx", 10,10);
}
//------------------------------------------------------------
}
//========================================================================
class ParFrame extends Frame implements ActionListener
{
Button b1, b2;
WindowGraph window;
//------------------------------------------------------------
ParFrame()
{
//relating to parameter frame----------------
setLayout(new FlowLayout());
b1 = new Button("Display the window");
add(b1);
b1.addActionListener(this);
b2 = new Button("Hide the window");
add(b2);
b2.addActionListener(this);
//relating to the next window---------------
window = new WindowGraph(this);
window.setSize(300, 200);
window.setLocation(300, 300);
window.setBackground(Color.YELLOW);
}
//------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == b1)
{
Graphics g = window.getGraphics();
window.setVisible(true);
//window.paint(g); //???
}
if(event.getSource() == b2)
{
window.setVisible(false);
}
}
//------------------------------------------------------------
}
//========================================================================
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should actually workj fine as written; no need to call the paint() method explicitly, as it will be called automatically by the AWT when the window is shown.
 
david solomon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dr. Friedman-Hill,
Many thanks for your response, you are right it does work now; but it did not before I rebooted the machine. If you wish/can, will you please tell me to why and how can I call the paint method from another class.
Best of regards
David
Note: your book seems to be quite interesting, I will try to purchase it after the holidays..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!