The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
March 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

using Graphics in an application

Posted by Raj Muthusamy on January 21, 2002 at 1:01 AM


1. get Graphics object g by calling getGraphics() method of a component.

2. use drawing methods of the graphics object g for rendering.

Note: make sure that the component is made visible before calling
the getGraphics() method. Otherwise, g will be born null.

I have given a simple example below to illustrate the points.

-------------------------------------------------------
import java.awt.*;
import java.awt.event.*;

public class test{
Frame ff;
Button bb;
public static void main(String args[]){
test tt = new test();
tt.display();
}//main

public void display(){
ff=new Frame("test");
bb=new Button("click");
ff.setLayout(new FlowLayout());
ff.add(bb);
ff.setVisible(true);
ff.setSize(400,400);
actionListener act = new actionListener();
bb.addActionListener(act);
}//display

class actionListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
Graphics g = ff.getGraphics();
g.fillRect(100,100,200,200);
}
}

}//class



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us