Academic Java    Java Tutorial  >  Graphics  >  graphics context

graphics context Example

Java graphics context EXAMPLE output This program uses the surface of a JApplet as its graphics context. It draws a yellow rectangle on a black one.

The paint() method of JApplet does the drawing. It is overridden with the one shown here.

It's the Graphics parameter of paint() that provides the graphics context. And this has various methods that are called to achieve the visual effect.
// Graphics Context Applet Example
import java.awt.*;
import javax.swing.*;

class GraphicsContextExample extends JApplet {

   @Override public void paint(Graphics g) {
      super.paint(g);
      g.fillRect(0,0,getWidth(),getHeight());
      g.setColor(Color.yellow);
      g.fillRect(100,100,100,100);

   }
}


* graphics context Examples