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