Friday, July 19, 2013

Graphics

Graphics class is an abstract class in AWT package. By using Graphics class, you can draw text, shapes, use colors and fonts, display images on the window, etc. As you already know, it is not allowed to create an object from any abstract class. So, You can invoke its methods directly in your code.
Generally, in a desktop application, any drawing is made on the canvas drawing area (instace of Canvas class). Your drawing code has to be place in the paint(Graphics g) method of the Canvas class. Then the instance of the Canvas class is added to the Frame window by using its add(Component) method.
The origin of the drawing area is (0,0) (x=0,y=0). The unit of x and y coordinates is pixel. If you want to start drawing on any location rather than the origin, you will use the translate(int x,int y) method.

Useful methods of the Graphics class:
 -drawLine(int x1,int y1,int x2,int y2):void
 -drawArc(int x,int y,int width,int height, int startAngle,int arcAngle):void
 -drawImage(Image img,int x,int y,int width,int height, ImageObserver obs):void
 -drawOval(int x,int y, int width,int height):void
 -drawPolygon(int[] xPoints,int[] yPoints,int numPoints):void
 -drawRect(int x,int y,int width,int height):void
 -drawString(String str,int x,int y):void
 -fillArc(int x,int y,int width,int height, int startAngle,int arcAngle):void
 -fillOval(int x,int y, int width,int height):void
 -fillPolygon(int[] xPoints,int[] yPoints,int numPoints):void
 -fillRect(int x,int y,int width,int height):void
 -setClip(Shape shape):void
 -setColor(Color color):void
 -setFont(Font font):void
 -translate(int x,int y):void

Example 1: draw Hello, World! text on the window
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;
class DrawArea extends Canvas implements ImageObserver{
 public void paint(Graphics g){
  g.drawString("Hello, World!", 300,100);
 }
}
f
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,400);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Draw text");

 }
}


Example 2: draw lines
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
 public void paint(Graphics g){
  g.drawLine(10, 50, 100, 200);
  g.drawLine(10, 20, 100, 100);

 }
}
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,300);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter()Rf{
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Drawing lines");
 }
}

Example 3: draw rectangles
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
 public void paint(Graphics g){

  g.drawRect(10, 50, 400, 50);
  g.fillRect(10, 150, 400, 100);

 }
}
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,300);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Drawing");
 }
}

Example 4: draw ovals
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
 public void paint(Graphics g){

  g.drawOval(150, 50, 100,100 );
  g.fillOval(10, 150, 400, 100);

 }
}
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,300);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Drawing ellipses and circles");
 }
}

Example 5: draw arcs
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
 public void paint(Graphics g){
  g.drawArc(10, 50, 100,100,0,90 );
  g.fillArc(10, 150, 100,100,0,90);
  g.fillArc(300, 150, 100,100,90,180 );


 }
}
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,300);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Drawing arcs");
 }
}

Example 6: draw polygon
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
 public void paint(Graphics g){

  int[] xpoints={10, 50,100,200,300,10};
  int[] ypoints={150, 100,50,100,300,150};
  g.drawPolyline(xpoints, ypoints, 6);


 }
}
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,400);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Drawing polygons");
 }
}

Example 7: draw an image
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
class DrawArea extends Canvas implements ImageObserver{
 public void paint(Graphics g){

  Image img=Toolkit.getDefaultToolkit().getImage("d:/image02.png");
  g.drawImage(img, 0, 0,img.getWidth(this), img.getHeight(this),this);

 }
}
class WindowShow extends Frame{
 public WindowShow(String title){
 setTitle(title);
 setSize(600,400);
 setVisible(true);
 add(new DrawArea());

 addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 });

 }
}
public class AWTTEST {
 public static void main(String[] args){
  new WindowShow("Image show");
 }

}

No comments:

Post a Comment