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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Noise Image in Graphics 
 

This Java Tutorial is all about creating Images with the use of graphics in Java. For this we have to import awt and swing package in our Java code.

 

Noise Image in Graphics

                         

This Java Tutorial is all about creating Images with the use of graphics in Java. For this we have to import awt and swing package in our Java code. In this tutorial you will learn how to create a noise image in graphics. Now lets find out what we have defined in this code for creating a noise image...

A primitive data type byte:  primitive data type byte is defined for containing the image data and a class DataBuffer defines the data storage formats which are used in various image operations.

 The object of DataBuffer class is then passed to the constructor of class WritableRaster with height, width of the image. WritableClass is extended from the Raster class which provides pixel writing capabilities and also store pixels. To render an image, the pixel values is translated into color components and alpha components by the class ColorModel. The BufferedImage class is comprised of ColorModel class and Raster class. It describes the buffer of image data. 

To generate a stream of pseudorandom numbers, Random class is called. To start the thread, new Thread(this).start() is called. The method random.nextBytes(b) generates the random bytes and place them into the specified byte array. Thread.sleep() blocks the current thread for the specified number of milliseconds. 

The method graphics.drawImage() draws the specified image.

Here is the code of NoiseImage.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Random;
import javax.swing.*;

public class NoiseImage extends JComponent implements Runnable {
  byte[] b;
  BufferedImage bufferedImage;
  Random ran;

  public void init() {
    int wid = getSize().width, ht = getSize().height;
    int length = ((wid + 5) * ht) / 7;
    b = new byte[length];
    DataBuffer dataBuffer = new DataBufferByte(b, length);
    WritableRaster raster = Raster.createPackedRaster(dataBuffer, wid, ht, 1null);
    ColorModel colorModel = new IndexColorModel(12new byte[] { 
      (
byte0, (byte) 250} ,  new byte[]  {(byte0,  (byte250 },  new byte[]  {      (byte0, (byte250 });
    bufferedImage = new BufferedImage(colorModel, raster, false, null);
    ran = new Random();
    new Thread(this).start();
  }
    public void run() {
    while (true) {
      ran.nextBytes(b);
      repaint();
      try {
        Thread.sleep(80);
      catch (Exception e) { 
      }
    }
  }
  public void paint(Graphics graphics) {
    if (bufferedImage == null)
    init();
    graphics.drawImage(bufferedImage, 00this);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new NoiseImage());
    frame.setSize(250250);
    frame.setVisible(true);
  }
}

Image will be displayed as:

Download Source Code

Steps to run the program:

1. Open the command prompt
2. Select your specific folder
3. Type javac filename.java (where file name is the name of your file)
4. Now Java filename

After running your program you will be able to see a noise image that we have created in java using graphics.

 

                         

» View all related tutorials
Related Tags: c com image class list object diff components stack icons user sed entity icon order component ai define container show

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
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
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.