Academic Java    Java Tutorial  >  Graphics  >  graphics context (frame)

graphics context Example

Java graphics context EXAMPLE output This program uses the surface of a JFrame to draw a cyan rectangle on a black one.

It is the paint() method of JFrame that does the drawing inside a panel. It is overridden with the one shown here. Notice that its Graphics parameter provides the graphics context.
// Graphics Context JFrame Example
import java.awt.*;
import javax.swing.*;

class GraphicsContextExample2 extends JFrame {

   public GraphicsContextExample2() {
      super();
      setSize(300,300);
      setVisible(true);
   }

   @Override public void paint(Graphics g) {
      super.paint(g);

      g.fillRect(0,0,getWidth(),getHeight());
      g.setColor(Color.cyan);
      g.fillRect(100,100,100,100);
   }

   static public void main(String[] args) {
      new GraphicsContextExample2();
   }

}


* graphics context Examples