Work with images

Hi! There is such problem: on mouse click on the certain picture, it is necessary to distinguish in what area of a picture have got ( transparent or color ). Whether there is a ready decision of this problem? Clearly, what it is necessary to receive bitmap a picture and to address to parameter an alpha but how it to make?

[331 byte] By [Jackie_rua] at [2007-9-29 11:11:21]
# 1

From the java documentation:

public class PixelGrabber

extends Object

implements ImageConsumer

The PixelGrabber class implements an ImageConsumer which can be attached to an Image or ImageProducer object to retrieve a subset of the pixels in that image. Here is an example:

public void handlesinglepixel(int x, int y, int pixel) {

int alpha = (pixel >> 24) & 0xff;

int red= (pixel >> 16) & 0xff;

int green = (pixel >> 8) & 0xff;

int blue = (pixel) & 0xff;

// Deal with the pixel as necessary...

}

public void handlepixels(Image img, int x, int y, int w, int h) {

int[] pixels = new int[w * h];

PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);

try {

pg.grabPixels();

} catch (InterruptedException e) {

System.err.println("interrupted waiting for pixels!");

return;

}

if ((pg.getStatus() & ImageObserver.ABORT) != 0) {

System.err.println("image fetch aborted or errored");

return;

}

for (int j = 0; j < h; j++) {

for (int i = 0; i < w; i++) {

handlesinglepixel(x+i, y+j, pixels[j * w + i]);

}

}

}

joop_eggena at 2007-7-15 0:34:52 > top of Java-index,Other Topics,Algorithms...
# 2

import java.awt.*;

import java.awt.event.*;

import java.math.*;

public class CalculatorApplet extends Frame

{

Frame CalFrame;

private LCD_Screen out;

private Keypad in;

private Math operations;

private Color b;

public CalculatorApplet()

{

b=Color.black;

setLayout(new BorderLayout());

setBackground(b);

out = new LCD_Screen();

operations = new Math(out);

in = new Keypad(out);

super( "Calculator");

add("North",out);

add("Center",in);

add("East",operations);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public static void main(String[] args)

{

Frame f=new CalculatorApplet();

f.setSize(180,180);

f.show();

}

}

class LCD_Screen extends Panel

{

private TextField show;

public LCD_Screen ()

{

show = new TextField(20);

Panel p=new Panel();

p.add(show);

add("Center",p);

}

public void write(String s)

{

show.setText(s);

}

public String read()

{

return show.getText();

}

}

trenha at 2007-7-15 0:34:52 > top of Java-index,Other Topics,Algorithms...