MemoryImageSource and using it...

J

JariTapio

Hei!

Im tryin to make a random screen with MemoryImageSource but faling to do
it..

I do not know what is wrong please help...

The Source follows...

Please be a friend and reply...

JariTapio/Helsinki/08may2005
www.EuroJari.com


//<applet code ="Testi.class" width=640 height=480></applet>
//

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;

public class Testi extends java.applet.Applet implements Runnable{

Thread thread;
Image image;
int [] pix = new int [999999];

public void init()
{
}
public void start()
{
thread = new Thread(this);
thread.start();
}
public void stop()
{
thread.stop();
}
public void run()
{
while ( true )
{
for (int cc=0; cc< 640*480; ++cc )
pix [cc]=(int)(Math.random ()*255);

ImageProducer ip = new MemoryImageSource ( 640,480,pix,0,640 );
image = createImage (ip);
}
}

public void paint(Graphics g)
{
g.drawImage(image,0,0,640,480,this);
}

public void update(Graphics g)
{
paint(g);
}
}
 
T

Thomas Fritsch

JariTapio said:
Hei! Hei!


Im tryin to make a random screen with MemoryImageSource but faling to do
it..

I do not know what is wrong please help...

The Source follows...

Please be a friend and reply...

JariTapio/Helsinki/08may2005
www.EuroJari.com


//<applet code ="Testi.class" width=640 height=480></applet>
//

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;

public class Testi extends java.applet.Applet implements Runnable{

Thread thread;
Image image;
int [] pix = new int [999999];

public void init()
{
}
public void start()
{
thread = new Thread(this);
thread.start();
}
public void stop()
{
thread.stop();
}
public void run()
{
while ( true )
{
for (int cc=0; cc< 640*480; ++cc )
pix [cc]=(int)(Math.random ()*255);

There is a problem with your pixel values. They are always in the range
0x00000000 ... 0x000000FF.
Because you are implicitly using the default RGBColorModel this means:
The blue component varies. The red, green, and -most importantly- the alpha
components are 0.
Hence your image is totally transparent!
Use for example instead:
pix [cc]=0xFF000000 + (int)(Math.random ()*255);
This will give a blue/black sparkling image.
ImageProducer ip = new MemoryImageSource ( 640,480,pix,0,640 );
image = createImage (ip);
Here you should call:
repaint();
so that your paint method gets called after your image changed.
Also: Consider making a pause here in order to lower the CPU-load:
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top