Paint method in Java

Paint method allows you to draw a text message ,draw an image,draw a rectangle,oval etc. You can set the color of the text and the diagram(oval,rect,fillrect).

 

import java.awt.*;

import java.awt.event.*;

public class fra5 extends Frame

{

Image image;

public static void main(String[] args)

{

new fra5();

}

public fra5()

{

setTitle("Paint example!");

setSize(500,300);

setVisible(true);

   }

public void paint(Graphics graphic)

{

Toolkit tool = Toolkit.getDefaultToolkit();

image = tool.getImage("jj.jpg");

graphic.setColor(Color.RED);

graphic.drawRect(20,50,100,100);

graphic.setFont(new Font("serif", Font.ITALIC + Font.BOLD, 20));

graphic.drawString("Draw Images and Diagram using PAINT method--->:",120,50);

graphic.setColor(Color.gray);

graphic.fillRect(20,50,50,50);

graphic.drawImage(image,180,100,130,185,this);

image = tool.getImage("wel1.jpg");

graphic.drawImage(image,320,100,180,185,this);

}

}

 Paint method in Java

Explanation….

public void paint(Graphics graphic)......

This method takes a Graphics object(graphics) which provides to draw anything you like.

graphic.setColor(Color.RED);.......

graphic.setFont(new Font("serif", Font.ITALIC + Font.BOLD, 30));

graphic.drawString("Draw Images and Diagram using PAINT method--->:",120,50);

This is a simple text message(“Images and Diagram using PAINT method -à”)in a italic and bold of size 30.Here,1 20 is pixels from left of the frame and 50 from the top of the frame. The color of the text is RED using setColor method.

graphic.setColor(Color.RED);

graphic.drawRect(20,50,100,100);

This draw a rectangle 20 pixels from left and 50 from top of the frame. The rectangle is 100 pixels wide and 100 pixels high.

To draw an image we use..

Toolkit tool = Toolkit.getDefaultToolkit();

image = tool.getImage("jj.jpg");

graphic.drawImage(image,160,300,130,185,this);

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @