Painting in Awt and Swing

When you fill a shape, its inside is covered with paint. You use the setPaint method to set the paint style to an object whose class implements the paint interface. In Java2d, there are three classes 1)Color 2)GradientPaint   3)TexturePaint.

 

import java.awt.geom.*;

import java.awt.image.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class PaintTest

{

public static void main(String aa[])

{

JFrame frame=new PaintTestFramee();

frame.show();

}

}

class PaintTestFramee extends JFrame implements ActionListener

{

public PaintTestFramee()//constructor

{

setTitle("PaintTestt");

setSize(500,500);

Container contentPan=getContentPane();

canvas=new PaintPanel();

contentPan.add(canvas,"Center");

JPanel buttonPanell=new JPanel();

ButtonGroup groupp=new ButtonGroup();

colorButtonn=new JRadioButton("Colored",true);

buttonPanell.add(colorButtonn);

groupp.add(colorButtonn);

colorButtonn.addActionListener(this);

gradientPaintButtonn=new JRadioButton("Fill Gradient Paintt",false);

buttonPanell.add(gradientPaintButtonn);

groupp.add(gradientPaintButtonn);

gradientPaintButtonn.addActionListener(this);

texturePaintButtonn=new JRadioButton("Fill Texture Paintt",false);

buttonPanell.add(texturePaintButtonn);

groupp.add(texturePaintButtonn);

texturePaintButtonn.addActionListener(this);

contentPan.add(buttonPanell,"North");

}

public void actionPerformed(ActionEvent aventt)

{

Object source=aventt.getSource();

if(source==colorButtonn)

canvas.setColor();

else if(source==gradientPaintButtonn)

canvas.setGradientPaint();

else if(source==texturePaintButtonn)

canvas.setTexturePaint();

}

private PaintPanel canvas;

//three radio buttonsfor Color,GradientPaint and TexturePaint class

private JRadioButton colorButtonn;

private JRadioButton gradientPaintButtonn;

private JRadioButton texturePaintButtonn;

}

class PaintPanel extends JPanel

{

public PaintPanel()

{

//need a media tracker to read in an image file

Image imagee=Toolkit.getDefaultToolkit().getImage("jj.jpg");//image for TexturePaint

MediaTracker trackerr=new MediaTracker(this);

trackerr.addImage(imagee,0);

try

{

trackerr.waitForID(0);

}

catch(InterruptedException ie)

{

}

       /*To construct a TexturePaint object, you need to specify a BufferedImage and an anchor rectangle*/

bufferedImagee=new BufferedImage(imagee.getWidth(null),imagee.getHeight(null), BufferedImage.TYPE_INT_ARGB);

Graphics2D g2=bufferedImagee.createGraphics();/*obtain a graphics context to draw into the

buffered Image*/

g2.drawImage(imagee,0,0,null);

setColor();

}

public void paintComponent(Graphics gg)

{

super.paintComponent(gg);

Graphics2D g2=(Graphics2D)gg;

g2.setPaint(paint);

Ellipse2D circlee=new Ellipse2D.Double(0,0,getWidth(),getHeight());

g2.fill(circlee);

}

public void setColor()

{

paint=Color.red;

repaint();

}

public void setGradientPaint()

{

paint=new GradientPaint(0,0,Color.red,(float)getWidth(),(float)getHeight(),Color.gray);

repaint();

}

public void setTexturePaint()

{

Rectangle2D anchorr=new Rectangle2D.Double(0,0,

2*bufferedImagee.getWidth(),

2*bufferedImagee.getHeight());

paint=new TexturePaint(bufferedImagee,anchorr);

repaint();

}

private Paint paint;

private BufferedImage bufferedImagee;

}

OUTPUT-----Color Paint

Coloured Paint

OUTPUT--Texture Paint

TexturePaint

Explanation..

The Color class implements the paint interface. To fill shapes with a solid color, we call setPaint with a Color object(g2.setPaint(Color.red));

The GradientPaint class(object by giving two points) varies colors(combination of two colors) by interpolating between two given color values.

paint=new GradientPaint(0,0,Color.red,(float)getWidth(),(float)getHeight(),Color.gray);

The TexturePaint class fills an area(image is scaled to fit the anchor) with repetitions of an image(Media Tracker).

About Us

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