codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Hi guys need some help with this j applet error  delek at 16:01 on Thursday, April 01, 2004
 

java.lang.NullPointerException
at myProj2.init(myProj2.java:64)
at sun.applet.AppletPanel.run(AppletPanel.java:353)
at java.lang.Thread.run(Thread.java:534)

there is no compiling error
i am using text pad

the main aim of the this applet is to load the image , change it to grey , and reduce it to 2 bits and use floyd steinberg algorithm to dither it

can any experts please help~!

below is the code


import java.net.URL;
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.net.*;

public class myProj2 extends Applet
{
private Image im;
private Image cropped;
private Image cropped1;
private Image cropped2;
private int w, h,transparency,blue,green,red, rgb;
private int[] alphaPixels, redPixels, greenPixels, bluePixels ,outputPixels;
private int DitherValueK=0, error=0, test=2;


public void init()
{
MediaTracker mt = new MediaTracker(this);
URL url = getClass().getResource("/forum/pic.jpg");

try {
im = createImage((ImageProducer)url.getContent());

mt.addImage(im, 0);
mt.waitForID(0);
}
catch(Exception e)
{ e.printStackTrace();}

h=im.getHeight(this);
w=im.getWidth(this);
int[] pixels = new int[w*h];
int[] grayPixels = new int[w*h];
int[] bitPixels = new int[w*h];
int[] burkePixels = new int[w*h];



PixelGrabber pg = new PixelGrabber(im, 0, 0, w, h,pixels, 0, w);
try {
pg.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}

//Convert to gray
for(int i=0; i<pixels.length; i++)
grayPixels = convertPixelToGray(pixels);
ImageProducer ip = new MemoryImageSource(w,h,grayPixels,0,w);
cropped = createImage(ip);

for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
rgb = grayPixels[row*w + col];
alphaPixels[row*w + col] = (rgb >> 24) & 0xff;
redPixels[row*w + col] = (rgb >> 16) & 0xff;
greenPixels[row*w + col] = (rgb >> 8) & 0xff;
bluePixels[row*w + col] = rgb & 0xff;
}
}

fsDithering(redPixels, "r", true);
fsDithering(greenPixels, "g", true);
fsDithering(bluePixels, "b", true);



//1 bit
if(test==1){
for (int row = 0; row < h; row++){
for (int col = 0; col < w; col++)
bitPixels[row*w+col] = oneBits(grayPixels[row*w+col]);
}
}

//2 bits
else if(test==2){
for (int row = 0; row < h; row++){
for (int col = 0; col < w; col++)
bitPixels[row*w+col] = twoBits(grayPixels[row*w+col]);
}

}

//3 bits
else{
for (int row = 0; row < h; row++){
for (int col = 0; col < w; col++)
bitPixels[row*w+col] = threeBits(grayPixels[row*w+col]);
}

}
ImageProducer ip1 = new MemoryImageSource(w,h,bitPixels,0,w);
cropped1 = createImage(ip1);

// * process the final output image after dithering *
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {

transparency = alphaPixels[row*w + col];
red = redPixels[row*w + col] & 0xff;
green = greenPixels[row*w + col] & 0xff;
blue = bluePixels[row*w + col] & 0xff;

outputPixels[row*w + col] = (transparency << 24)|(red << 16)|(green << 8)|(blue);
}
}

ImageProducer ip2 = new MemoryImageSource (w,h,burkePixels,0,w);
cropped2 = createImage(ip2);
// Sierra
//for (int row = 0; row < h; row++){
// for (int col = 0; col < w; col++){
// sierraPixels[row*w+col] = sierraDithering(grayPixels[row*w+col],bitPixels[row*w+col]);
// }
//}
}//init()

public void paint(Graphics g) {
g.drawImage(im,0,0,this);
g.drawImage(cropped,im.getWidth(this)+20,0,this);
g.drawImage(cropped1,0,im.getHeight(this)+20,this);
g.drawImage(cropped2,0,im.getHeight(this)+40,this);
}

public int convertPixelToGray(int rgb)
{
int transparency = (rgb >> 24) & 0xff;
int red = (rgb >>16) & 0xff;
int green=(rgb >>8)& 0xff;
int blue =rgb & 0xff;
int gray=(red+green+blue)/3;
return ((transparency<<24)|(gray<<16)|(gray<<8)|gray);
}

public int oneBits(int rgb)
{
int blue=0,transparency;

blue = rgb & 0xff;
if(blue<=127)
blue=0;
else
blue=255;
transparency=(rgb >> 24) & 0xff;
return ((transparency<<24)|(blue<<16)|(blue<<8)|blue);
}

public int twoBits(int rgb)
{
int blue=0,transparency;
blue = rgb & 0xFF;
if(blue<44)
blue=0;
else if(blue<129)
blue=85;
else if(blue<214)
blue=170;
else
blue=255;

transparency=(rgb >> 24) & 0xff;
return ((transparency<<24)|(blue<<16)|(blue<<8)|blue);
}

public int threeBits(int rgb)
{
int blue=0,transparency;
blue = rgb & 0xFF;
if(blue<18)
blue=0;
else if(blue<54)
blue=36;
else if(blue<90)
blue=72;
else if(blue<126)
blue=108;
else if(blue<162)
blue=144;
else if(blue<198)
blue=180;
else if(blue<234)
blue=216;
else
blue=255;

transparency=(rgb >> 24) & 0xff;
return ((transparency<<24)|(blue<<16)|(blue<<8)|blue);

}

public void fsDithering(int[] aPixels, String color, boolean ditherErrProcess)
{
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
DitherValueK = aPixels[row*w + col];
DitherValueK = getApproxValue(DitherValueK);

if (color=="r")
redPixels[row*w + col] = DitherValueK;
else if (color=="g")
greenPixels[row*w + col] = DitherValueK;
else
bluePixels[row*w + col] = DitherValueK;

error = aPixels[row*w + col] - DitherValueK;


if ((row % 2)==1) {

// * odd rows - processRightToLeft *

if ((col-1) > -1)
aPixels[row*w + (col-1)] += error * 7>>4;
if (((col+1) < w) && ((row+1) < h) )
aPixels[(row+1)*w + (col+1)] += error *3>>4;
if ((row+1) < h)
aPixels[(row+1)*w + col] += error *5>>4;
if (((col-1) > -1) && ((row+1) < h))
aPixels[(row+1)*w + (col-1)] += error *1>>4;
//aPixels[(row+1)*w + (col-1)] += (error - aPixels[row*w + (col-1)] - aPixels[(row+1)*w + (col+1)] - aPixels[(row+1)*w + col]);
}
else {

// * even rows - processLeftToRight *

if ((col+1) < w)
aPixels[row*w + (col+1)] += error *7>>4;
if (((col-1) > -1) && ((row+1) < h) )
aPixels[(row+1)*w + (col-1)] += error * 3>>4;
if ((row+1) < h)
aPixels[(row+1)*w + col] += error * 5>>4;
if (((col+1) < w) && ((row+1) < h))
aPixels[(row+1)*w + (col+1)] += error>>4;
//aPixels[(row+1)*w + (col+1)] += (error - aPixels[row*w + (col+1)] - aPixels[(row+1)*w + (col-1)] - aPixels[(row+1)*w + col]);
}

}

}


}

public int getApproxValue(int DitherValueK)
{
/*
* calculate the approximate value of the pixels*
*/
if (test==1) {
if (DitherValueK<127)
DitherValueK = 0;
else
DitherValueK =255;
}
else if (test==2) {
if (DitherValueK<127) {
if (DitherValueK<42)
DitherValueK = 0;
else
DitherValueK = 85;
}
else {
if (DitherValueK<212)
DitherValueK = 170;
else
DitherValueK = 255;
}
}
else {
if (DitherValueK<54) {
if (DitherValueK<18)
DitherValueK = 0;
else
DitherValueK = 36;
}
else if (DitherValueK<126) {
if (DitherValueK<90)
DitherValueK = 72;
else
DitherValueK = 108;
}
else if (DitherValueK<198) {
if (DitherValueK<162)
DitherValueK = 144;
else
DitherValueK = 180;
}
else {
if (DitherValueK<234)
DitherValueK = 216;
else
DitherValueK = 252;
}

}
return DitherValueK;
}



}











CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: C# and EXCEL question saving a file saving the the first column as read only
•  Windows Authentication using LDAP get user first name
•  Javabeans incorporating sql query.
•  Re: Help: Trouble with z-Index and SELECT lists
•  Help with
•  Iexplore -
•  Iframe and Div Layer Scrollbar Problem
•  Left margin increases as page fills.....
•  Re: Parse error that is slowly driving me insane


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005