Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

 

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.