Tech I Enjoy Logo

Custom Search




  Home >> Java
Have you ever thought of creating image using programmatically?
By using/controlling R, G, B values for getting any color on
screen.
Working with RGB (Red,Green, Blue) to create Image that is created
by setting R, G, B value in a array bits.

By creating ColorModel and changing image bits to MemoryImageSource
for creating an image in memory and displaying this on Applet or
an application using Java AWT.

Following section will be showing all the steps I followed to 
achieve this objective.

Steps includes: 1. creating a ColorModel as follows: ColorModel colorModel=ColorModel.getRGBdefault(); 2. Creating values of different colors such as red, green, red, white, black. int r=new Color(255,0,0).getRGB(); int g=new Color(0,255,0).getRGB(); int b=new Color(0,0,255).getRGB(); int w=new Color(255,255,255).getRGB(); int bl=new Color(0,0,0).getRGB(); 3. Creating the image bits in an array as follows: int[] imageBits = new int[] { w,w,w,w,w,w,w,w,w,w,w,w,w, w,w,w,w,w,w,w,w,w,w,w,w,w, w,w,w,w,b,b,b,b,b,w,w,w,w, w,w,w,w,b,b,b,b,b,w,w,w,w, w,w,w,w,b,b,b,b,b,w,w,w,w, w,w,g,g,g,g,b,b,b,w,w,w,w, w,w,g,g,g,g,b,b,b,w,w,w,w, w,w,g,g,g,g,w,w,w,w,w,w,w, r,r,g,g,g,g,w,w,w,w,w,w,w, r,r,r,w,w,w,w,w,w,w,w,w,w, r,r,r,w,w,w,w,w,w,w,w,w,w, w,w,w,w,w,w,w,w,w,w,w,w,w, w,w,w,w,w,w,w,w,w,w,w,w,w, }; 4. Creating MemoryImageSource by passing ColorModel and imageBits as, MemoryImageSource mis = new MemoryImageSource (13,12,colorModel,imageBits, 0,13); 5. creating Image by using MemoryImageSource. img = createImage(mis); 6. Displaying this image on Applet public void paint(Graphics g) { g.drawImage(img,10,10,40,40,this); } If you want to have this dynamic image created with a Java example then I have the complete source as follows: In the below source code, I am trying to achieve a graphical icon as shown in this Image CreateDynamicImage.java ------------------------------------------------------------------- import java.awt.Color; import java.awt.Frame; import java.awt.Image; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.ColorModel; import java.awt.image.MemoryImageSource; import java.awt.Toolkit; public class CreateDynamicImage { public CreateDynamicImage() { try { ColorModel colorModel=ColorModel.getRGBdefault(); int r=new Color(255,0,0).getRGB(); int g=new Color(0,255,0).getRGB(); int b=new Color(0,0,255).getRGB(); int w=new Color(255,255,255).getRGB(); int bl=new Color(0,0,0).getRGB(); int[] imageBits = new int[] { w,w,w,w,w,w,w,w,w,w,w,w,w,w, w,w,w,w,w,w,w,w,w,w,w,w,w,w, w,w,w,w,b,b,b,b,b,w,w,w,w,w, w,w,w,w,b,b,b,b,b,w,w,w,w,w, w,w,w,w,b,b,b,b,b,w,w,w,w,w, w,w,g,g,g,g,b,b,b,w,w,w,w,w, w,w,g,g,g,g,b,b,b,w,w,w,w,w, w,w,g,g,g,g,w,w,w,w,w,w,w,w, r,r,g,g,g,g,w,w,w,w,w,w,w,w, r,r,r,w,w,w,w,w,w,r,r,r,r,w, r,r,r,w,w,w,w,w,w,r,w,w,r,w, w,w,w,w,w,w,w,w,w,r,w,w,r,w, w,w,w,w,w,w,w,w,w,r,r,r,r,w, w,w,w,w,w,w,w,w,w,w,w,w,w,w }; MemoryImageSource mis = new MemoryImageSource (14,13,colorModel,imageBits, 0,14); Frame frm = new Frame(); Image img = Toolkit.getDefaultToolkit().createImage(mis); frm.setIconImage(img); frm.show(); frm.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); }}); } catch (Exception ex) { ex.printStackTrace(); } } private Image createImage(MemoryImageSource mis) { // TODO Auto-generated method stub return null; } public static void main(String args[]) { new CreateDynamicImage(); } } ------------------------------------------------------------------- After compilation of this code, one can run it to see the dynamic image created and shown as icon on the Frame header/title.
If you like to share your comment/suggestions/feedback relating to this Page, you can do so by droping us an email at usingframeworks @ gmail . com with the subject line mentioning URL for this Page (i.e, /inmemory-image-creation-java-awt.php) or use this LINK. As per this website's privacy policy, we never disclose your email id, though we shall post your comments/suggestions/feedback with your name (optional) and date on this Page. If you don't want your comments/suggestions/feedback to be shared in this Page, please mention so in your email to us. Thank you very much..... If anything missed out , please let me know at techienjoy at yahoo . com
RMI and Java Stub and Skeleton :
Example using RMI using Java Technology.
Example of Drag and Drop using Java Technology :
Example of Drag and Drop using Java Technology.
Example ThreadPoolExecutor using Java :
ThreadPoolExecutor Example using Java Technology.
Thread wait and notify With Example :
Example using wait and notify within Thread 
with code and explained
Thread Deadlock using Java :
Example reproducing a possible Thread 
using Java Technology.
Thread design scenario using Java :
Thread design scenario using Java Technology.
Image creation using Java AWT :
In-Memory Image creation using AWT on Java Platform.
Synchronized Block wait and notify With Example :
Example using wait and notify from within
synchronized block with code and explained
Java Reflection using Comparator :
Comparator using Reflection on Java Platform
and example discussed.
List of Examples on Java Platform :
List of Examples on Java Platform.
Interview Questions and answer of Java Technology :
Interview Questions and answer of Java Technology.
Event Handling using Java Technology :
Example on how to use Event
and handling code using Java Technology.


References :
Tags: java comparator reflection
Tags: java example drag n drop
Tags: Java Interview Questions
Tags: java rmi tutorial stub skeleton
Tags: Java Thread Deadlock
Tags: Java Thread Design Scenarios
Tags: Java threadpoolexecutor
Tags: Java



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions