[an error occurred while processing this directive]

Clip of image

                         

In this section, you will studied how to show a clip of image.

To show the image clip, we have used Ellipse2D class to show the oval shape in an image. To give the path with straight line, we have used the class GeneralPath. The AffineTransform class allows translation, shearing, scaling, rotation etc. To give the stylistic and impressive outline of the specified shape, class BasicStroke is used.

The method setToIdentity() sets the color-adjustment of a specified category. The paint() method calls the drawImage()  method. The drawImage() method describes the rotation of image.

The method thread.start() starts the thread to begin execution. The method currentThread() returns a reference to the currently executing thread object. The method thread.yield() pauses temporarily  the currently executing thread and allows other threads to execute.

Following code draws the image:

g.drawImage(bufferedImg, 0, 0, this)

Here is the code of ClipImageExample.java

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.image.BufferedImage;

public class ClipImageExample extends JApplet implements Runnable {
 private final double I[] = { 6.0,2.0 };
  private final double J[] = { 6.0,5.0 };
  private double p, q;
  private double ip = I[0];
  private double iq = J[1];
  private double iw = I[0];
  private double ih = J[1];
  private double width, height; 
  private GeneralPath path = new GeneralPath();
  private AffineTransform affineTransform = new AffineTransform();
  private BasicStroke stroke = new BasicStroke(20.0f);
  private Ellipse2D ellipse1 = new Ellipse2D.Float();
  private Ellipse2D ellipse2 = new Ellipse2D.Float();
  private Thread thread;
  private BufferedImage bufferedImg;
  private int w, h;
  
  public void drawImage(Graphics2D g2) {
    p += ip;
    q += iq;
    width += iw;
    height += ih;
    if (width > w / 3) {
      width = w / 3;
      iw = Math.random() * -w / 16 1;
    }
    if (width < w / 9) {
      width = w / 9;
      iw = Math.random() * w / 16 1;
    }
    if (height > h / 3) {
      height = h / 3;
      ih = Math.random() * -h / 16 1;
    }
    if (height < h / 9) {
      height = h / 9;
      ih = Math.random() * h / 16 1;
    }
    if ((p + width) > w) {
      p = (w - width) - 1;
      ip = Math.random() * -w / 32 1;
    }
    if (p < 0) {
      p = 3;
      ip = Math.random() * w / 32 1;
    }
    if ((q + height) > h) {
      q = (h - height) - 2;
      iq = Math.random() * -h / 32 1;
    }
    if (q < 0) {
      q = 3;
      iq = Math.random() * h / 32 1;
    }
    ellipse1.setFrame(p, q, width, height);
    g2.setClip(ellipse1);
    ellipse2.setFrame(p + 6, q + 5, width - 10, height - 10);
    g2.clip(ellipse2);

    path.reset();
    path.moveTo(-w / 3.0f, -h / 9.0f);
    path.lineTo(+w / 3.0f, -h / 9.0f);
    path.lineTo(-w / 5.0f, +h / 3.0f);
    path.lineTo(+0.0f, -h / 3.0f);
    path.lineTo(+w / 5.0f, +h / 3.0f);
    path.closePath();

    affineTransform.setToIdentity();
    affineTransform.translate(w * .5f, h * .5f);
    g2.transform(affineTransform);
    g2.setStroke(stroke);
    g2.setPaint(Color.cyan);
    g2.draw(path);
    affineTransform.setToIdentity();
  g2.setTransform(affineTransform);
    g2.setPaint(Color.red);

   for (int qq = 0; qq < h; qq += 40) {
      for (int pp = 0, k = 0; pp < w; k++, pp += 40) {
        switch (k) {
        case 0:
          ellipse1.setFrame(pp, qq, 2020);
          g2.fill(ellipse1);
          break;
        case 1:
          ellipse2.setFrame(pp, qq, 2020);
          g2.fill(ellipse2);
          k = -1;
        }
      }
    }
  }
  public Graphics2D createDemoGraphics2D(Graphics g) {
    Graphics2D g2 = null;
    if (bufferedImg == null || bufferedImg.getWidth() != w || bufferedImg.
         getHeight() != h) {

      bufferedImg = (BufferedImage) createImage(w, h);
      }
    if (bufferedImg != null) {
      g2 = bufferedImg.createGraphics();
      g2.setBackground(getBackground());
    }
    g2.clearRect(00, w, h);
    return g2;
  }
  public void paint(Graphics g) {
    w = getWidth();
    h = getHeight();
    if (w <= || h <= 0)
      return;
    Graphics2D g2 = createDemoGraphics2D(g);
    drawImage(g2);
    g2.dispose();
     if (bufferedImg != null && isShowing()) {
      g.drawImage(bufferedImg, 00this);
    }
}
  public void start() {
    thread = new Thread(this);
    thread.start();
  }
  public synchronized void stop() {
    thread = null;
  }
  public void run() {
    Thread me = Thread.currentThread();
    while (thread == me && isShowing()) {
      Graphics g = getGraphics();
      paint(g);
      g.dispose();
      thread.yield();
    }
  }
  public static void main(String s[]) {
    final ClipImageExample clip = new ClipImageExample();
    JFrame frame = new JFrame("Clip Image Example");
    frame.getContentPane().add("Center", demo);
    frame.setSize(new Dimension(450300));
    frame.show();
    clip.start();
  }
}

Output will be displayed as:

Download Source Code

 

                         

[an error occurred while processing this directive]