Java Graphics Invert Color Composite -
basically want make cursor jcomponent that's pixels appear inverse of color over. example, take mouse , hover on letters in url page. if closely, pixels of cursor on black pixels of letter turn white. know can invert rgb color subtracting current red, green, , blue colors 255 each corresponding field, don't know how implement way want.
this part of basic paint program making. jcomponent mentioned before "canvas" can draw on various tools. not using java.awt.cursor change cursor because want cursor change dynamically according under it. "cursor" using defined .png image, , creating bufferedimage file can draw on top of existing bufferedimage of whole component. redraw image using point defined mouselistener.
i looked alphacomposite , looks close want, there nothing inverting colors underneath cursor want. please help.
edit:
so had hard way algorithm because there's nothing built in purpose. here's code little out of context:
/** * changes color of each pixel under cursor shape in image * complimentary color of pixel. * * @param points array of points relative cursor image * represents each black pixel of cursor image * @param cp point relative cursor image used * hotspot of cursor */ public void drawcursorbypixel(arraylist<point> points, point cp) { point ml = handler.getmouselocation(); if (ml != null) { (point p : points) { int x = ml.x + p.x - cp.x; int y = ml.y + p.y - cp.y; if (x >= 0 && x < image.getwidth() && y >= 0 && y < image.getheight()) { image.setrgb(x, y, getcompliment(image.getrgb(x, y))); } } } } public int getcompliment(int c) { color col = new color(c); int newr = 255 - col.getred(); int newg = 255 - col.getgreen(); int newb = 255 - col.getblue(); return new color(newr, newg, newb).getrgb(); }
i believe looking image filter. sounds have pieces built already. filter image of cursor, drawn on top of else. trick is, say, draw each pixel of cursor such said pixel's color calculated "opposite" of pixel color in drawn space behind cursor.
i not know best way go this, know 1 way might able improve on. paint whatever background buffered image, go color of pixels cursor hover on using bufferedimage
's color model. example 1 found here question.
bufferedimage image = new bufferedimage(width, height, bufferedimage.type_4byte_abgr); graphics2d g2 = image.creategraphics(); _mainpanel.paint(g2); image.getcolormodel().getrgb(pixel); g2.dispose();
ultimately you'll use buffered image of background pixels (and colors) cursor overlaps, , can run algorithm on colors invert them in cursor, redraw cursor new colors.
this question has couple of solutions algorithm, though have not tried them see effects.
Comments
Post a Comment