Accueil > Forum > > > > Couleur de transparence
Couleur de transparence
mercredi 27 mars 2002 à 13:23:36 |
Couleur de transparence

Mikonyx
|
Salut! Est-il possible de définir une couleur ki serait transparente pour une image....??merci Miko
|
|
jeudi 28 mars 2002 à 12:37:44 |
Re : Couleur de transparence

Jo
|
Salut, Bon moi je viens de trouver ca sur mon disque dur, et ca devrait pouvoir t'aider:
Quelque note ki vont ave:
when Java's createImage method encounters a transparent colour in a gif. I solved this by manipulating the pixels in arrays (while cursing Netscape), and by passing the applet a value for a notional transparent pixel in the params.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * The Watcher Applet by Don Clark. May be distributed freely for non-commercial use * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
import java.applet.*; import java.awt.*; import java.awt.image.PixelGrabber; import java.awt.image.MemoryImageSource; import java.awt.image.ImageObserver;
public class Watcher extends Applet implements Runnable { String version = "Watcher version 3";
String filename; // params... int more_movement, more_x, more_y, more_cross, bounces, mask_topleft_x, mask_topleft_y, mask_botright_x, mask_botright_y, left_topleft_x, left_topleft_y, left_botright_x, left_botright_y, right_topleft_x, right_topleft_y, right_botright_x, right_botright_y, lefteye_middle_x, lefteye_middle_y, righteye_middle_x, righteye_middle_y, applet_background, transparent, eye_background, outer_border, inner_border; Color outer_color, inner_color; // border colours
Thread wThread = null;
MemoryImageSource buildMIS;
Image loadedimage, mainpic;
int loadedimagewidth, loadedimageheight, // sizes of images... mainpicwidth, mainpicheight, maskwidth, maskheight, leftwidth, leftheight, rightwidth, rightheight, pix[], // pixel array from loadedimage m_pix[], b_pix[], //pixel arrays for mask, background, l_pix[], r_pix[], //pixel arrays for left eye, right eye m_pix_size; // size of m_pix (and b_pix) array int lsocktlx=1000, lsocktly=1000, // offsets of eye sockets on mask lsockbrx=-1, lsockbry=-1, // - left socket top left x etc. rsocktlx=1000, rsocktly=1000, // - these values are temporary, the actual rsockbrx=-1, rsockbry=-1; // - values are calculated in sockets()
int dpleft_topleft_x, dpleft_topleft_y, // offsets to topleft & botright x & y dpleft_botright_x, dpleft_botright_y, // of left eye (l_pix) on drawpic. dpright_topleft_x, dpright_topleft_y, // offsets to topleft & botright x & y dpright_botright_x, dpright_botright_y; // of right eye (r_pix) on drawpic.
double DtoRfactor, wideH, highH, // for calculating eye movements movewidth, moveheight, incx, incy; // in mouseDrag() and prepMath()
boolean got_params = true, prepedMath = false, // startup flags allLoaded = false, allSetUp = false, // startup flags animated = false, dobounce = false, // ongoing status flags imageDone; // ongoing status flag
int leftx=0, lefty=0, rightx=0, righty=0, // for moving relative to eye-centres lx = -9, ly = -9, rx = -9, ry = -9, // copies above in buildImage() dragx = -1, dragy = -1; // old posns to test if changed by drag
Panel pnl; boolean pnlsetup = false; private int pnlx = -1, pnly = -1; private Image pnlimg;
// 'local' variables globally declared to save memory // from buildImage(): int halfmask, larraysize, rarraysize, currleft_tlx, currleft_tly, currleft_brx, currleft_bry, currright_tlx, currright_tly, currright_brx, currright_bry; // from mouseDrag(): double tracklx, trackly, trackrx, trackry, centy, centx, factorx, tklx, tkrx, tkly, tkry; // from bounce(): int bno=bounces+2, bmo = bno-1; double dleftx, dlefty, drightx, drighty; int lposns[][], rposns[][];
public Watcher() { }
public String getAppletInfo() { return "The Watcher Applet\r\n" + "(c) 1998, Don Clark"; }
public void init() { String param = ""; Point p; got_params = true;
try { param = getParameter("picture"); if (param.equals("")) filename = "picture.gif"; else filename = param;
param = getParameter("applet_background"); applet_background = findColor(param); if (applet_background != -1) setBackground(new Color(applet_background));
param = getParameter("transparent"); transparent = findColor(param); if (transparent == -1) transparent = 0x01FF01;// default
param = getParameter("eye_background"); eye_background = findColor(param); if (eye_background == -1) eye_background = 0xFFDEDEDE;// default else eye_background += 0xFF000000;// add FF000000 for max. opacity
param = getParameter("outer_border").toLowerCase(); outer_border = findColor(param); if (outer_border != -1) outer_color = new Color(outer_border);
param = getParameter("inner_border").toLowerCase(); inner_border = findColor(param); if (inner_border != -1) inner_color = new Color(inner_border);
param = getParameter("mask_topleft"); p = findPoint(param); mask_topleft_x = p.x; mask_topleft_y = p.y;
param = getParameter("mask_botright"); p = findPoint(param); mask_botright_x = p.x; mask_botright_y = p.y;
param = getParameter("lefteye_topleft"); p = findPoint(param); left_topleft_x = p.x; left_topleft_y = p.y;
param = getParameter("lefteye_botright"); p = findPoint(param); left_botright_x = p.x; left_botright_y = p.y;
param = getParameter("righteye_topleft"); p = findPoint(param); right_topleft_x = p.x; right_topleft_y = p.y;
param = getParameter("righteye_botright"); p = findPoint(param); right_botright_x = p.x; right_botright_y = p.y;
param = getParameter("lefteye_middle"); p = findPoint(param); lefteye_middle_x = p.x; lefteye_middle_y = p.y;
param = getParameter("righteye_middle"); p = findPoint(param); righteye_middle_x = p.x; righteye_middle_y = p.y;
param = getParameter("more_movement"); more_movement = Integer.parseInt(param); if ((more_movement < -500) | (more_movement > 500)) more_movement = 0;// default
param = getParameter("more_x_movement"); more_x = Integer.parseInt(param); if ((more_x < 0) | (more_x > 100)) more_x = 0;// default
param = getParameter("more_y_movement"); more_y = Integer.parseInt(param); if ((more_y < 0) | (more_y > 100)) more_y = 0;// default
param = getParameter("more_cross_eyed"); more_cross = Integer.parseInt(param); if ((more_cross < 0) | (more_cross > 50)) more_cross = 0;// default
param = getParameter("bounces"); bounces = Integer.parseInt(param); if ((bounces < 0) | (bounces > 20)) bounces = 5;// default } catch (Exception e) { got_params = false; } }
public void destroy() { }
private void displayImage(Graphics g) { if (!allLoaded) return;
if (!animated) g.drawImage(mainpic, 0, 0, null); else { if (buildImage()) drawb_pix(g); } }
private boolean buildImage() { if (!allSetUp) return false;
if ((lx == leftx) & (ly == lefty) & (rx == rightx) & (ry == righty)) return false;
int i, j, k = 0, lr, lrx, lry;
currleft_tlx = dpleft_topleft_x + leftx; // this and variables currleft_tly = dpleft_topleft_y + lefty; // below are to reduce currleft_brx = dpleft_botright_x + leftx; // calculation during currleft_bry = dpleft_botright_y + lefty; // the loops currright_tlx = dpright_topleft_x + rightx; currright_tly = dpright_topleft_y + righty; currright_brx = dpright_botright_x + rightx; currright_bry = dpright_botright_y + righty; larraysize = leftwidth * leftheight; rarraysize = rightwidth * rightheight; for (i = 0; i < maskheight; i++) // blend engine: puts bkgnd colour on b_pix, { // then adds the eyes, then the mask for (j = 0; j < maskwidth; j++) { b_pix[k] = eye_background;// bkgnd colour onto b_pix if (j <= halfmask) { if ((j >= currleft_tlx) && (j < currleft_brx) && (i >= currleft_tly) && (i < currleft_bry)) { // here when left eye array overlaps left half of b_pix array lrx = j - currleft_tlx; lry = (i - currleft_tly) * leftwidth; lr = lrx + lry; if ((lr >= 0) && (lr < larraysize) && (l_pix[lr] != 0)) b_pix[k] = l_pix[lr];// left eye onto left half of b_pix } } else { if ((j >= currright_tlx) && (j < currright_brx) && (i >= currright_tly) && (i < currright_bry)) { // here when right eye array overlaps right half of b_pix array lrx = j - currright_tlx; lry = (i - currright_tly) * rightwidth; lr = lrx + lry; if ((lr >= 0) && (lr < rarraysize) && (r_pix[lr] != 0)) b_pix[k] = r_pix[lr];// right eye onto right half of b_pix } }
if (m_pix[k] != 0) b_pix[k] = m_pix[k];// mask onto b_pix ++k; } }
lx = leftx; ly = lefty; rx = rightx; ry = righty; return true; }
private synchronized void drawb_pix(Graphics g) { imageDone = false;
try { g.drawImage(createImage(buildMIS), mask_topleft_x, mask_topleft_y, this); } catch(Exception e) { imageDone = true; } while (!imageDone) showStatus("..."); }
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.ALLBITS) != 0) { imageDone = true; return false; } else return true; }
private void pnlDraw() { if (pnlsetup && pnl.isVisible()) { Graphics g = pnl.getGraphics(); g.drawImage(pnlimg, 1, 1, null); } }
public void paint(Graphics g) { if (allLoaded) { displayImage(g); pnlDraw(); } else if (!got_params) { g.drawString("Watcher applet", 5, 20); g.drawString("parameter error", 5, 30); } else { g.drawString("Loading", 5, 20); g.drawString("picture...", 5, 30); } }
public void start() { if (wThread == null) { wThread = new Thread(this); wThread.start(); } } public void stop() { if (wThread != null) { wThread.stop(); wThread = null; } }
private void loadImage() { MediaTracker tracker = new MediaTracker(this); loadedimage = getImage(getCodeBase(), filename); tracker.addImage(loadedimage, 0);
try { tracker.waitForAll(); allLoaded = !tracker.isErrorAny(); } catch (InterruptedException e) { showStatus("Loading interrupted"); allLoaded = false; }
while (loadedimage.getWidth(null) == -1) ; loadedimagewidth = loadedimage.getWidth(null); loadedimageheight = loadedimage.getHeight(null); }
private boolean getPix() { int arraysize = loadedimagewidth * loadedimageheight; pix = new int[arraysize]; PixelGrabber pixGrab = new PixelGrabber(loadedimage, 0, 0, loadedimagewidth, loadedimageheight, pix, 0, loadedimagewidth); try { pixGrab.grabPixels(); } catch (InterruptedException e) { return false; }
for (int rgb, i = 0; i < arraysize; i++) { rgb = pix[i] & 0xFFFFFF; // get the colours if (rgb == transparent) pix[i] = 0; // transparent black else pix[i] = 0xFF000000 + rgb;// fully opaque alpha transparency + colours }
return true; }
public void run() { if (!got_params) { stop(); return; } if (!allLoaded) loadImage();
if (!allLoaded) { stop(); showStatus("Error loading picture"); return; }
if (!getPix()) { stop(); showStatus("Error processing picture"); return; }
createPicture(); createMask(); createLeft(); createRight(); if (!prepedMath) prepMath(); if (!pnlsetup) pnlsetup(); allSetUp = true;
while (true) { if (dobounce) bounce(); paint(getGraphics());
try { Thread.sleep(50); } catch (InterruptedException e) { showStatus("Watcher was interrupted"); } } }
private void createPicture() { mainpicwidth = loadedimagewidth / 2; mainpicheight = loadedimageheight; mainpic = createImage(mainpicwidth, mainpicheight);
reshape(0, 0, mainpicwidth, mainpicheight);
int i, j, k = 0, mp_pix[] = new int[mainpicwidth * mainpicheight]; for (i = 0; i < mainpicheight; i++) for (j = mainpicwidth; j < (mainpicwidth+mainpicwidth); j++) mp_pix[k++] = pix[i * loadedimagewidth + j];
Image temp = createImage(new MemoryImageSource(mainpicwidth, mainpicheight, mp_pix, 0, mainpicwidth)); Graphics g = mainpic.getGraphics(); g.drawImage(temp, 0, 0, null);
if ((outer_border != -1) && (inner_border != -1)) { g.setColor(inner_color); g.drawRect(0, 0, mainpicwidth-1, mainpicheight-1); g.drawRect(1, 1, mainpicwidth-2, mainpicheight-2); g.setColor(outer_color); g.drawRect(0, 0, mainpicwidth-2, mainpicheight-2); } else if (outer_border != -1) { g.setColor(outer_color); g.drawRect(0, 0, mainpicwidth-1, mainpicheight-1); } else if (inner_border != -1) { g.setColor(inner_color); g.drawRect(1, 1, mainpicwidth-3, mainpicheight-3); } }
private void createMask() { maskwidth = 1 + mask_botright_x - mask_topleft_x; halfmask = maskwidth / 2; maskheight = 1 + mask_botright_y - mask_topleft_y;
int i, j, k = 0, start;
m_pix_size = maskwidth * maskheight; m_pix = new int[m_pix_size]; start = mask_topleft_y * loadedimagewidth + mask_topleft_x;
for (i = 0; i < maskheight; i++) for (j = 0; j < maskwidth; j++) m_pix[k++] = pix[j + start + (i * loadedimagewidth)];
b_pix = new int[m_pix_size];// array for buildImage to lay bkgnd colour // & eyes & mask on buildMIS = new MemoryImageSource(maskwidth, maskheight, b_pix, 0, maskwidth); sockets(); }
private void sockets() { // finds offsets of eye sockets on mask by looking for pixel values of 0
int i, j, k = 0, l = maskwidth/2;
for (i = 0; i < maskheight; i++) { for (j = 0; j < maskwidth; j++) { if (m_pix[k++] == 0) { if (j <= l)// left half { if (j < lsocktlx) lsocktlx = j; if (i < lsocktly) lsocktly = i; if (j > lsockbrx) lsockbrx = j; if (i > lsockbry) lsockbry = i; } else // right half { if (j < rsocktlx) rsocktlx = j; if (i < rsocktly) rsocktly = i; if (j > rsockbrx) rsockbrx = j; if (i > rsockbry) rsockbry = i; } } } } }
private void createLeft() { leftwidth = 1 + left_botright_x - left_topleft_x; leftheight = 1 + left_botright_y - left_topleft_y; larraysize = leftwidth * leftheight;
int i, j, k = 0, start;
l_pix = new int[larraysize]; start = left_topleft_y * loadedimagewidth + left_topleft_x;
for (i = 0; i < leftheight; i++)// copy eye pixels into l_pix array for (j = 0; j < leftwidth; j++) l_pix[k++] = pix[j + start + (i * loadedimagewidth)];
// offsets to topleft & botright x & y of eye (l_pix) on drawpic dpleft_topleft_x = lefteye_middle_x - mask_topleft_x - (leftwidth/2); dpleft_topleft_y = lefteye_middle_y - mask_topleft_y - (leftheight/2); dpleft_botright_x = dpleft_topleft_x + leftwidth; dpleft_botright_y = dpleft_topleft_y + leftheight; }
private void createRight() { rightwidth = 1 + right_botright_x - right_topleft_x; rightheight = 1 + right_botright_y - right_topleft_y; rarraysize = rightwidth * rightheight;
int i, j, k = 0, start;
r_pix = new int[rarraysize]; start = right_topleft_y * loadedimagewidth + right_topleft_x;
for (i = 0; i < rightheight; i++)// copy eye pixels into r_pix array for (j = 0; j < rightwidth; j++) r_pix[k++] = pix[j + start + (i * loadedimagewidth)];
// offsets to topleft & botright x & y of eye (r_pix) on drawpic dpright_topleft_x = righteye_middle_x - mask_topleft_x - (rightwidth/2); dpright_topleft_y = righteye_middle_y - mask_topleft_y - (rightheight/2); dpright_botright_x = dpright_topleft_x + rightwidth; dpright_botright_y = dpright_topleft_y + rightheight; }
private int findColor(String s) { int n, i; Point p; String t; s = s.trim();
if (s.indexOf("none") != -1) return -1; if (s.indexOf("black") != -1) return 0x000000; // (0, 0, 0); if (s.indexOf("white") != -1) return 0xFFFFFF; // (255, 255, 255); if (s.indexOf("red") != -1) return 0xFF0000; // (255, 0, 0); if (s.indexOf("green") != -1) return 0x00FF00; // (0, 255, 0); if (s.indexOf("blue") != -1) return 0x0000FF; // (0, 0, 255); if (s.indexOf("dark") != -1) return 0x404040; // (64, 64, 64); if (s.indexOf("light") != -1) return 0xC0C0C0; // (192, 192, 192); if (s.indexOf("gray") != -1) return 0x808080; // (128, 128, 128); if (s.indexOf("grey") != -1) return 0x808080; // (128, 128, 128); if (s.indexOf("yellow") != -1) return 0xFFFF00; // (255, 255, 0); if (s.indexOf("cyan") != -1) return 0x00FFFF; // (0, 255, 255); if (s.indexOf("magenta") != -1) return 0xFF00FF; // (255, 0, 255); if (s.indexOf("orange") != -1) return 0xFFC800; // (255, 200, 0); if (s.indexOf("pink") != -1) return 0xFFAFAF; // (255, 175, 175);
if (s.indexOf("#") != -1)// if hex format #number { s = s.replace('#', ' '); s = s.trim(); try {n = Integer.parseInt(s, 16);}; catch (NumberFormatException nfe) {n = -1;} return n % 0x1000000; }
i = s.indexOf(","); if (i == -1) return -1;
try {t = s.substring(0, i+1) + " 0";} catch (StringIndexOutOfBoundsException e) {return -1;} p = findPoint(t); n = p.x << 16; // got red (p.y ignored)
try {t = s.substring(i+1);} catch (StringIndexOutOfBoundsException e) {return -1;} p = findPoint(t); n += p.x << 8; // got green n += p.y; // got blue return n % 0x1000000; }
private Point findPoint(String s) { int i, j, len = s.length(); String one = "", two = ""; boolean building = false, building_one = false, building_two = false; char c;
for (i = 0; i < len; i++) { c = s.charAt(i); if (Character.isDigit(c)) { if (!building) { building = true; if (building_one) { building_two = true; two += String.valueOf(c); } else { building_one = true; one += String.valueOf(c); } } else { if (building_two) two += String.valueOf(c); else one += String.valueOf(c); } } else { building = false; } }
try {i = Integer.parseInt(one);} catch (NumberFormatException nfe) {i = 0;}
try {j = Integer.parseInt(two);} catch (NumberFormatException nfe) {j = 0;}
return new Point(i, j); }
private void pnlsetup() { setLayout(null); pnl = new Panel(); add(pnl); pnl.setBackground(Color.black); pnl.hide();
pnlx = size().width-4;
String s = "The Watcher Applet from"; String t = "donclark@powernet.co.uk"; Font gf = pnl.getFont(); boolean toobig = true; Font f = null; FontMetrics fm = null; int w, sw = 0, tw = 0, ps = 19; while (toobig) { ps -= 1; if (ps == 8) toobig = false; f = new Font(gf.getName(), Font.PLAIN, ps); pnl.setFont(f); fm = pnl.getFontMetrics(f); sw = fm.stringWidth(s); tw = fm.stringWidth(t); if (sw > tw) w = sw; else w = tw; if (w < (pnlx - 4)) toobig = false; }
int ma = fm.getMaxAscent(), md = fm.getMaxDescent(); int imgwidth = pnlx - 2, imgheight = (ma + md + 2) * 2;
pnly = imgheight + 2; pnl.resize(pnlx, pnly); pnl.move(2, mainpicheight-pnly-2);// allows for a 2 pixel border
pnlimg = pnl.createImage(imgwidth, imgheight); Graphics g = pnlimg.getGraphics(); g.setFont(f); g.setColor(Color.black); g.fillRect(0, 0, imgwidth-1, imgheight-1); g.setColor(Color.white); g.drawRect(0, 0, imgwidth-1, imgheight-1); g.drawString(s, (imgwidth-sw-2)/2, imgheight/2 - md - 1); g.drawString(t, (imgwidth-tw-2)/2, imgheight - md - 3);
pnlsetup = true; }
private void bounce() { dobounce = false; if (bounces == 0) { animated = false; return; }
// lposns[][] & rposns[][] store the left & right x & y posns for moving the // eyes to centre [x][0] & [y][0], from start posns [x][bmo] & [y][bmo]. // bno = bounce number = total no. of posns, from 0,0 to start positions. // bmo = bounce number minus one
dleftx = leftx; dlefty = lefty; drightx = rightx; drighty = righty;
int count; for (count = 0; count < bno; count++) { lposns[0][count] = (int)(dleftx / bmo * count); lposns[1][count] = (int)(dlefty / bmo * count); rposns[0][count] = (int)(drightx / bmo * count); rposns[1][count] = (int)(drighty / bmo * count); }
Graphics g = getGraphics(); animated = true; int steps, rep_count, rep_total;
for (steps = bmo; steps > 0; steps--) // do steps (number of bounce sequences) { if (!(lposns[0][steps]==0 && lposns[1][steps]==0 && // if(!...) block to rposns[0][steps]==0 && rposns[1][steps]==0)) // skip non-movements { rep_total = 1; // as bounce gets smaller it is repeated if (bounces < 3) { if ((bounces == 2) && (steps == 1)) rep_total = 2; } else if ((steps == 2) || (steps == 3)) rep_total = 2; else if (steps == 1) rep_total = 3;
for (rep_count = 0; rep_count < rep_total; rep_count++) // do repeats { for (count = steps; count >= 0; count--) // move to centre { leftx=lposns[0][count]; lefty=lposns[1][count]; rightx=rposns[0][count]; righty=rposns[1][count]; paint(g); } for (count = 1; count < steps; count++)// move to other side { leftx=-lposns[0][count]; lefty=-lposns[1][count]; rightx=-rposns[0][count]; righty=-rposns[1][count]; paint(g); } for (count = steps; count >= 0 ; count--)// move to centre again { leftx=-lposns[0][count]; lefty=-lposns[1][count]; rightx=-rposns[0][count]; righty=-rposns[1][count]; paint(g); } for (count = 1; count < steps; count++)// move to original posn. { leftx=lposns[0][count]; lefty=lposns[1][count]; rightx=rposns[0][count]; righty=rposns[1][count]; paint(g); } }// end of repeats loop }// end of if (posns not 0) block }//end of steps loop
System.gc(); animated = false; return; }
public boolean mouseDown(Event evt, int x, int y) { dragx = x; dragy = y; leftx=0; lefty=0; rightx=0; righty=0; lx = 1; // so that (lx != leftx) to make buildImage create new pic animated = true; paint(getGraphics()); requestFocus();
return true; }
public boolean mouseUp(Event evt, int x, int y) { if ((dragx == x) && (dragy == y)) animated = false; else dobounce = true;
return true; }
private void prepMath() { DtoRfactor = Math.PI / 180;
int i, j; Dimension d = getToolkit().getScreenSize(); int eyewidth, eyeheight, socketwidth, socketheight;
if (leftwidth > rightwidth) eyewidth = leftwidth; else eyewidth = rightwidth; i = lsockbrx - lsocktlx; j = rsockbrx - rsocktlx; if (i > j) socketwidth = i + 1; else socketwidth = j + 1; movewidth = socketwidth + eyewidth + more_x;
if (leftheight > rightheight) eyeheight=leftheight; else eyeheight=rightheight; i = lsockbry - lsocktly; j = rsockbry - rsocktly; if (i > j) socketheight = i + 1; else socketheight = j + 1; moveheight = socketheight + eyeheight + more_y;
centx = (righteye_middle_x - lefteye_middle_x) / 2; centy = moveheight / 2; factorx = centx - more_cross; if (factorx < 1) factorx = 1;// prevents /0 (unlikely)
incx = 180 / (double)d.width; incy = 180 / (double)d.height; wideH = movewidth / 2 + more_movement; // width hypotenuse (radius) highH = moveheight / 2 + more_movement; // height hypotenuse (radius) // opposite = sine(angle) * hypotenuse bno = bounces + 2; bmo = bno - 1; lposns = new int[2][bno]; rposns = new int[2][bno];
prepedMath = true; }
public boolean mouseDrag(Event evt, int x, int y) { if (!prepedMath) return true; if ((dragx == x) && (dragy == y)) return true;
double temp;
tracklx = x - lefteye_middle_x; trackly = y - lefteye_middle_y; trackrx = x - righteye_middle_x; trackry = y - righteye_middle_y;
tklx = Math.abs(tracklx); tkrx = Math.abs(trackrx); tkly = Math.abs(trackly); tkry = Math.abs(trackry);
if (centy == tkly) tkly--; // prevents /0 if (centy == tkry) tkry--; // prevents /0
// left cross-eyed bit if ((tracklx > 0) & (tracklx <= (centx + centx)) & (tkly <= centy)) { if (tracklx <= centx) temp = tklx; else temp = centx+centx-tklx; tracklx += temp * (180/factorx) / (centy/(centy-tkly)); } // right cross-eyed bit if ((trackrx < 0) & (trackrx >= -(centx + centx)) & (tkry <= centy)) { if (trackrx >= -centx) temp = tkrx; else temp = centx+centx-tkrx; trackrx -= temp * (180/factorx) / (centy/(centy-tkry)); }
leftx = (int)(Math.sin(DtoR(tracklx * incx)) * wideH); lefty = (int)(Math.sin(DtoR(trackly * incy)) * highH); rightx = (int)(Math.sin(DtoR(trackrx * incx)) * wideH); righty = (int)(Math.sin(DtoR(trackry * incy)) * highH);
animated = true; paint(getGraphics()); System.gc();
return true; }
private double DtoR(double d) { //To convert degrees to radians, multiply degrees by pi/180. return d * DtoRfactor; //(To convert radians to degrees, multiply radians by 180/pi) }
public boolean keyDown(Event evt, int nKey) { if (pnlsetup) pnl.show(); if ((nKey == 118) || (nKey == 86) || (nKey == 22)) //v V CTRL+v showStatus(version); return true; }
public boolean keyUp(Event evt, int nKey) { if (pnlsetup) pnl.hide(); showStatus(""); return true; } } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
jeudi 28 mars 2002 à 18:57:27 |
Re : Couleur de transparence

Mikonyx
|
oh put1 chuis vrément dans la merde moi...mdr rien de plus simple? du style SetInvisibleColor()...bouuuuuuuuuuuuhhh snirf :'( merci +++++ Miko
------------------------------- Réponse au message : -------------------------------
Salut, Bon moi je viens de trouver ca sur mon disque dur, et ca devrait pouvoir t'aider:
Quelque note ki vont ave:
when Java's createImage method encounters a transparent colour in a gif. I solved this by manipulating the pixels in arrays (while cursing Netscape), and by passing the applet a value for a notional transparent pixel in the params.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * The Watcher Applet by Don Clark. May be distributed freely for non-commercial use * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
import java.applet.*; import java.awt.*; import java.awt.image.PixelGrabber; import java.awt.image.MemoryImageSource; import java.awt.image.ImageObserver;
public class Watcher extends Applet implements Runnable { String version = "Watcher version 3";
String filename; // params... int more_movement, more_x, more_y, more_cross, bounces, mask_topleft_x, mask_topleft_y, mask_botright_x, mask_botright_y, left_topleft_x, left_topleft_y, left_botright_x, left_botright_y, right_topleft_x, right_topleft_y, right_botright_x, right_botright_y, lefteye_middle_x, lefteye_middle_y, righteye_middle_x, righteye_middle_y, applet_background, transparent, eye_background, outer_border, inner_border; Color outer_color, inner_color; // border colours
Thread wThread = null;
MemoryImageSource buildMIS;
Image loadedimage, mainpic;
int loadedimagewidth, loadedimageheight, // sizes of images... mainpicwidth, mainpicheight, maskwidth, maskheight, leftwidth, leftheight, rightwidth, rightheight, pix[], // pixel array from loadedimage m_pix[], b_pix[], //pixel arrays for mask, background, l_pix[], r_pix[], //pixel arrays for left eye, right eye m_pix_size; // size of m_pix (and b_pix) array int lsocktlx=1000, lsocktly=1000, // offsets of eye sockets on mask lsockbrx=-1, lsockbry=-1, // - left socket top left x etc. rsocktlx=1000, rsocktly=1000, // - these values are temporary, the actual rsockbrx=-1, rsockbry=-1; // - values are calculated in sockets()
int dpleft_topleft_x, dpleft_topleft_y, // offsets to topleft & botright x & y dpleft_botright_x, dpleft_botright_y, // of left eye (l_pix) on drawpic. dpright_topleft_x, dpright_topleft_y, // offsets to topleft & botright x & y dpright_botright_x, dpright_botright_y; // of right eye (r_pix) on drawpic.
double DtoRfactor, wideH, highH, // for calculating eye movements movewidth, moveheight, incx, incy; // in mouseDrag() and prepMath()
boolean got_params = true, prepedMath = false, // startup flags allLoaded = false, allSetUp = false, // startup flags animated = false, dobounce = false, // ongoing status flags imageDone; // ongoing status flag
int leftx=0, lefty=0, rightx=0, righty=0, // for moving relative to eye-centres lx = -9, ly = -9, rx = -9, ry = -9, // copies above in buildImage() dragx = -1, dragy = -1; // old posns to test if changed by drag
Panel pnl; boolean pnlsetup = false; private int pnlx = -1, pnly = -1; private Image pnlimg;
// 'local' variables globally declared to save memory // from buildImage(): int halfmask, larraysize, rarraysize, currleft_tlx, currleft_tly, currleft_brx, currleft_bry, currright_tlx, currright_tly, currright_brx, currright_bry; // from mouseDrag(): double tracklx, trackly, trackrx, trackry, centy, centx, factorx, tklx, tkrx, tkly, tkry; // from bounce(): int bno=bounces+2, bmo = bno-1; double dleftx, dlefty, drightx, drighty; int lposns[][], rposns[][];
public Watcher() { }
public String getAppletInfo() { return "The Watcher Applet\r\n" + "(c) 1998, Don Clark"; }
public void init() { String param = ""; Point p; got_params = true;
try { param = getParameter("picture"); if (param.equals("")) filename = "picture.gif"; else filename = param;
param = getParameter("applet_background"); applet_background = findColor(param); if (applet_background != -1) setBackground(new Color(applet_background));
param = getParameter("transparent"); transparent = findColor(param); if (transparent == -1) transparent = 0x01FF01;// default
param = getParameter("eye_background"); eye_background = findColor(param); if (eye_background == -1) eye_background = 0xFFDEDEDE;// default else eye_background += 0xFF000000;// add FF000000 for max. opacity
param = getParameter("outer_border").toLowerCase(); outer_border = findColor(param); if (outer_border != -1) outer_color = new Color(outer_border);
param = getParameter("inner_border").toLowerCase(); inner_border = findColor(param); if (inner_border != -1) inner_color = new Color(inner_border);
param = getParameter("mask_topleft"); p = findPoint(param); mask_topleft_x = p.x; mask_topleft_y = p.y;
param = getParameter("mask_botright"); p = findPoint(param); mask_botright_x = p.x; mask_botright_y = p.y;
param = getParameter("lefteye_topleft"); p = findPoint(param); left_topleft_x = p.x; left_topleft_y = p.y;
param = getParameter("lefteye_botright"); p = findPoint(param); left_botright_x = p.x; left_botright_y = p.y;
param = getParameter("righteye_topleft"); p = findPoint(param); right_topleft_x = p.x; right_topleft_y = p.y;
param = getParameter("righteye_botright"); p = findPoint(param); right_botright_x = p.x; right_botright_y = p.y;
param = getParameter("lefteye_middle"); p = findPoint(param); lefteye_middle_x = p.x; lefteye_middle_y = p.y;
param = getParameter("righteye_middle"); p = findPoint(param); righteye_middle_x = p.x; righteye_middle_y = p.y;
param = getParameter("more_movement"); more_movement = Integer.parseInt(param); if ((more_movement < -500) | (more_movement > 500)) more_movement = 0;// default
param = getParameter("more_x_movement"); more_x = Integer.parseInt(param); if ((more_x < 0) | (more_x > 100)) more_x = 0;// default
param = getParameter("more_y_movement"); more_y = Integer.parseInt(param); if ((more_y < 0) | (more_y > 100)) more_y = 0;// default
param = getParameter("more_cross_eyed"); more_cross = Integer.parseInt(param); if ((more_cross < 0) | (more_cross > 50)) more_cross = 0;// default
param = getParameter("bounces"); bounces = Integer.parseInt(param); if ((bounces < 0) | (bounces > 20)) bounces = 5;// default } catch (Exception e) { got_params = false; } }
public void destroy() { }
private void displayImage(Graphics g) { if (!allLoaded) return;
if (!animated) g.drawImage(mainpic, 0, 0, null); else { if (buildImage()) drawb_pix(g); } }
private boolean buildImage() { if (!allSetUp) return false;
if ((lx == leftx) & (ly == lefty) & (rx == rightx) & (ry == righty)) return false;
int i, j, k = 0, lr, lrx, lry;
currleft_tlx = dpleft_topleft_x + leftx; // this and variables currleft_tly = dpleft_topleft_y + lefty; // below are to reduce currleft_brx = dpleft_botright_x + leftx; // calculation during currleft_bry = dpleft_botright_y + lefty; // the loops currright_tlx = dpright_topleft_x + rightx; currright_tly = dpright_topleft_y + righty; currright_brx = dpright_botright_x + rightx; currright_bry = dpright_botright_y + righty; larraysize = leftwidth * leftheight; rarraysize = rightwidth * rightheight; for (i = 0; i < maskheight; i++) // blend engine: puts bkgnd colour on b_pix, { // then adds the eyes, then the mask for (j = 0; j < maskwidth; j++) { b_pix[k] = eye_background;// bkgnd colour onto b_pix if (j <= halfmask) { if ((j >= currleft_tlx) && (j < currleft_brx) && (i >= currleft_tly) && (i < currleft_bry)) { // here when left eye array overlaps left half of b_pix array lrx = j - currleft_tlx; lry = (i - currleft_tly) * leftwidth; lr = lrx + lry; if ((lr >= 0) && (lr < larraysize) && (l_pix[lr] != 0)) b_pix[k] = l_pix[lr];// left eye onto left half of b_pix } } else { if ((j >= currright_tlx) && (j < currright_brx) && (i >= currright_tly) && (i < currright_bry)) { // here when right eye array overlaps right half of b_pix array lrx = j - currright_tlx; lry = (i - currright_tly) * rightwidth; lr = lrx + lry; if ((lr >= 0) && (lr < rarraysize) && (r_pix[lr] != 0)) b_pix[k] = r_pix[lr];// right eye onto right half of b_pix } }
if (m_pix[k] != 0) b_pix[k] = m_pix[k];// mask onto b_pix ++k; } }
lx = leftx; ly = lefty; rx = rightx; ry = righty; return true; }
private synchronized void drawb_pix(Graphics g) { imageDone = false;
try { g.drawImage(createImage(buildMIS), mask_topleft_x, mask_topleft_y, this); } catch(Exception e) { imageDone = true; } while (!imageDone) showStatus("..."); }
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.ALLBITS) != 0) { imageDone = true; return false; } else return true; }
private void pnlDraw() { if (pnlsetup && pnl.isVisible()) { Graphics g = pnl.getGraphics(); g.drawImage(pnlimg, 1, 1, null); } }
public void paint(Graphics g) { if (allLoaded) { displayImage(g); pnlDraw(); } else if (!got_params) { g.drawString("Watcher applet", 5, 20); g.drawString("parameter error", 5, 30); } else { g.drawString("Loading", 5, 20); g.drawString("picture...", 5, 30); } }
public void start() { if (wThread == null) { wThread = new Thread(this); wThread.start(); } } public void stop() { if (wThread != null) { wThread.stop(); wThread = null; } }
private void loadImage() { MediaTracker tracker = new MediaTracker(this); loadedimage = getImage(getCodeBase(), filename); tracker.addImage(loadedimage, 0);
try { tracker.waitForAll(); allLoaded = !tracker.isErrorAny(); } catch (InterruptedException e) { showStatus("Loading interrupted"); allLoaded = false; }
while (loadedimage.getWidth(null) == -1) ; loadedimagewidth = loadedimage.getWidth(null); loadedimageheight = loadedimage.getHeight(null); }
private boolean getPix() { int arraysize = loadedimagewidth * loadedimageheight; pix = new int[arraysize]; PixelGrabber pixGrab = new PixelGrabber(loadedimage, 0, 0, loadedimagewidth, loadedimageheight, pix, 0, loadedimagewidth); try { pixGrab.grabPixels(); } catch (InterruptedException e) { return false; }
for (int rgb, i = 0; i < arraysize; i++) { rgb = pix[i] & 0xFFFFFF; // get the colours if (rgb == transparent) pix[i] = 0; // transparent black else pix[i] = 0xFF000000 + rgb;// fully opaque alpha transparency + colours }
return true; }
public void run() { if (!got_params) { stop(); return; } if (!allLoaded) loadImage();
if (!allLoaded) { stop(); showStatus("Error loading picture"); return; }
if (!getPix()) { stop(); showStatus("Error processing picture"); return; }
createPicture(); createMask(); createLeft(); createRight(); if (!prepedMath) prepMath(); if (!pnlsetup) pnlsetup(); allSetUp = true;
while (true) { if (dobounce) bounce(); paint(getGraphics());
try { Thread.sleep(50); } catch (InterruptedException e) { showStatus("Watcher was interrupted"); } } }
private void createPicture() { mainpicwidth = loadedimagewidth / 2; mainpicheight = loadedimageheight; mainpic = createImage(mainpicwidth, mainpicheight);
reshape(0, 0, mainpicwidth, mainpicheight);
int i, j, k = 0, mp_pix[] = new int[mainpicwidth * mainpicheight]; for (i = 0; i < mainpicheight; i++) for (j = mainpicwidth; j < (mainpicwidth+mainpicwidth); j++) mp_pix[k++] = pix[i * loadedimagewidth + j];
Image temp = createImage(new MemoryImageSource(mainpicwidth, mainpicheight, mp_pix, 0, mainpicwidth)); Graphics g = mainpic.getGraphics(); g.drawImage(temp, 0, 0, null);
if ((outer_border != -1) && (inner_border != -1)) { g.setColor(inner_color); g.drawRect(0, 0, mainpicwidth-1, mainpicheight-1); g.drawRect(1, 1, mainpicwidth-2, mainpicheight-2); g.setColor(outer_color); g.drawRect(0, 0, mainpicwidth-2, mainpicheight-2); } else if (outer_border != -1) { g.setColor(outer_color); g.drawRect(0, 0, mainpicwidth-1, mainpicheight-1); } else if (inner_border != -1) { g.setColor(inner_color); g.drawRect(1, 1, mainpicwidth-3, mainpicheight-3); } }
private void createMask() { maskwidth = 1 + mask_botright_x - mask_topleft_x; halfmask = maskwidth / 2; maskheight = 1 + mask_botright_y - mask_topleft_y;
int i, j, k = 0, start;
m_pix_size = maskwidth * maskheight; m_pix = new int[m_pix_size]; start = mask_topleft_y * loadedimagewidth + mask_topleft_x;
for (i = 0; i < maskheight; i++) for (j = 0; j < maskwidth; j++) m_pix[k++] = pix[j + start + (i * loadedimagewidth)];
b_pix = new int[m_pix_size];// array for buildImage to lay bkgnd colour // & eyes & mask on buildMIS = new MemoryImageSource(maskwidth, maskheight, b_pix, 0, maskwidth); sockets(); }
private void sockets() { // finds offsets of eye sockets on mask by looking for pixel values of 0
int i, j, k = 0, l = maskwidth/2;
for (i = 0; i < maskheight; i++) { for (j = 0; j < maskwidth; j++) { if (m_pix[k++] == 0) { if (j <= l)// left half { if (j < lsocktlx) lsocktlx = j; if (i < lsocktly) lsocktly = i; if (j > lsockbrx) lsockbrx = j; if (i > lsockbry) lsockbry = i; } else // right half { if (j < rsocktlx) rsocktlx = j; if (i < rsocktly) rsocktly = i; if (j > rsockbrx) rsockbrx = j; if (i > rsockbry) rsockbry = i; } } } } }
private void createLeft() { leftwidth = 1 + left_botright_x - left_topleft_x; leftheight = 1 + left_botright_y - left_topleft_y; larraysize = leftwidth * leftheight;
int i, j, k = 0, start;
l_pix = new int[larraysize]; start = left_topleft_y * loadedimagewidth + left_topleft_x;
for (i = 0; i < leftheight; i++)// copy eye pixels into l_pix array for (j = 0; j < leftwidth; j++) l_pix[k++] = pix[j + start + (i * loadedimagewidth)];
// offsets to topleft & botright x & y of eye (l_pix) on drawpic dpleft_topleft_x = lefteye_middle_x - mask_topleft_x - (leftwidth/2); dpleft_topleft_y = lefteye_middle_y - mask_topleft_y - (leftheight/2); dpleft_botright_x = dpleft_topleft_x + leftwidth; dpleft_botright_y = dpleft_topleft_y + leftheight; }
private void createRight() { rightwidth = 1 + right_botright_x - right_topleft_x; rightheight = 1 + right_botright_y - right_topleft_y; rarraysize = rightwidth * rightheight;
int i, j, k = 0, start;
r_pix = new int[rarraysize]; start = right_topleft_y * loadedimagewidth + right_topleft_x;
for (i = 0; i < rightheight; i++)// copy eye pixels into r_pix array for (j = 0; j < rightwidth; j++) r_pix[k++] = pix[j + start + (i * loadedimagewidth)];
// offsets to topleft & botright x & y of eye (r_pix) on drawpic dpright_topleft_x = righteye_middle_x - mask_topleft_x - (rightwidth/2); dpright_topleft_y = righteye_middle_y - mask_topleft_y - (rightheight/2); dpright_botright_x = dpright_topleft_x + rightwidth; dpright_botright_y = dpright_topleft_y + rightheight; }
private int findColor(String s) { int n, i; Point p; String t; s = s.trim();
if (s.indexOf("none") != -1) return -1; if (s.indexOf("black") != -1) return 0x000000; // (0, 0, 0); if (s.indexOf("white") != -1) return 0xFFFFFF; // (255, 255, 255); if (s.indexOf("red") != -1) return 0xFF0000; // (255, 0, 0); if (s.indexOf("green") != -1) return 0x00FF00; // (0, 255, 0); if (s.indexOf("blue") != -1) return 0x0000FF; // (0, 0, 255); if (s.indexOf("dark") != -1) return 0x404040; // (64, 64, 64); if (s.indexOf("light") != -1) return 0xC0C0C0; // (192, 192, 192); if (s.indexOf("gray") != -1) return 0x808080; // (128, 128, 128); if (s.indexOf("grey") != -1) return 0x808080; // (128, 128, 128); if (s.indexOf("yellow") != -1) return 0xFFFF00; // (255, 255, 0); if (s.indexOf("cyan") != -1) return 0x00FFFF; // (0, 255, 255); if (s.indexOf("magenta") != -1) return 0xFF00FF; // (255, 0, 255); if (s.indexOf("orange") != -1) return 0xFFC800; // (255, 200, 0); if (s.indexOf("pink") != -1) return 0xFFAFAF; // (255, 175, 175);
if (s.indexOf("#") != -1)// if hex format #number { s = s.replace('#', ' '); s = s.trim(); try {n = Integer.parseInt(s, 16);}; catch (NumberFormatException nfe) {n = -1;} return n % 0x1000000; }
i = s.indexOf(","); if (i == -1) return -1;
try {t = s.substring(0, i+1) + " 0";} catch (StringIndexOutOfBoundsException e) {return -1;} p = findPoint(t); n = p.x << 16; // got red (p.y ignored)
try {t = s.substring(i+1);} catch (StringIndexOutOfBoundsException e) {return -1;} p = findPoint(t); n += p.x << 8; // got green n += p.y; // got blue return n % 0x1000000; }
private Point findPoint(String s) { int i, j, len = s.length(); String one = "", two = ""; boolean building = false, building_one = false, building_two = false; char c;
for (i = 0; i < len; i++) { c = s.charAt(i); if (Character.isDigit(c)) { if (!building) { building = true; if (building_one) { building_two = true; two += String.valueOf(c); } else { building_one = true; one += String.valueOf(c); } } else { if (building_two) two += String.valueOf(c); else one += String.valueOf(c); } } else { building = false; } }
try {i = Integer.parseInt(one);} catch (NumberFormatException nfe) {i = 0;}
try {j = Integer.parseInt(two);} catch (NumberFormatException nfe) {j = 0;}
return new Point(i, j); }
private void pnlsetup() { setLayout(null); pnl = new Panel(); add(pnl); pnl.setBackground(Color.black); pnl.hide();
pnlx = size().width-4;
String s = "The Watcher Applet from"; String t = "donclark@powernet.co.uk"; Font gf = pnl.getFont(); boolean toobig = true; Font f = null; FontMetrics fm = null; int w, sw = 0, tw = 0, ps = 19; while (toobig) { ps -= 1; if (ps == 8) toobig = false; f = new Font(gf.getName(), Font.PLAIN, ps); pnl.setFont(f); fm = pnl.getFontMetrics(f); sw = fm.stringWidth(s); tw = fm.stringWidth(t); if (sw > tw) w = sw; else w = tw; if (w < (pnlx - 4)) toobig = false; }
int ma = fm.getMaxAscent(), md = fm.getMaxDescent(); int imgwidth = pnlx - 2, imgheight = (ma + md + 2) * 2;
pnly = imgheight + 2; pnl.resize(pnlx, pnly); pnl.move(2, mainpicheight-pnly-2);// allows for a 2 pixel border
pnlimg = pnl.createImage(imgwidth, imgheight); Graphics g = pnlimg.getGraphics(); g.setFont(f); g.setColor(Color.black); g.fillRect(0, 0, imgwidth-1, imgheight-1); g.setColor(Color.white); g.drawRect(0, 0, imgwidth-1, imgheight-1); g.drawString(s, (imgwidth-sw-2)/2, imgheight/2 - md - 1); g.drawString(t, (imgwidth-tw-2)/2, imgheight - md - 3);
pnlsetup = true; }
private void bounce() { dobounce = false; if (bounces == 0) { animated = false; return; }
// lposns[][] & rposns[][] store the left & right x & y posns for moving the // eyes to centre [x][0] & [y][0], from start posns [x][bmo] & [y][bmo]. // bno = bounce number = total no. of posns, from 0,0 to start positions. // bmo = bounce number minus one
dleftx = leftx; dlefty = lefty; drightx = rightx; drighty = righty;
int count; for (count = 0; count < bno; count++) { lposns[0][count] = (int)(dleftx / bmo * count); lposns[1][count] = (int)(dlefty / bmo * count); rposns[0][count] = (int)(drightx / bmo * count); rposns[1][count] = (int)(drighty / bmo * count); }
Graphics g = getGraphics(); animated = true; int steps, rep_count, rep_total;
for (steps = bmo; steps > 0; steps--) // do steps (number of bounce sequences) { if (!(lposns[0][steps]==0 && lposns[1][steps]==0 && // if(!...) block to rposns[0][steps]==0 && rposns[1][steps]==0)) // skip non-movements { rep_total = 1; // as bounce gets smaller it is repeated if (bounces < 3) { if ((bounces == 2) && (steps == 1)) rep_total = 2; } else if ((steps == 2) || (steps == 3)) rep_total = 2; else if (steps == 1) rep_total = 3;
for (rep_count = 0; rep_count < rep_total; rep_count++) // do repeats { for (count = steps; count >= 0; count--) // move to centre { leftx=lposns[0][count]; lefty=lposns[1][count]; rightx=rposns[0][count]; righty=rposns[1][count]; paint(g); } for (count = 1; count < steps; count++)// move to other side { leftx=-lposns[0][count]; lefty=-lposns[1][count]; rightx=-rposns[0][count]; righty=-rposns[1][count]; paint(g); } for (count = steps; count >= 0 ; count--)// move to centre again { leftx=-lposns[0][count]; lefty=-lposns[1][count]; rightx=-rposns[0][count]; righty=-rposns[1][count]; paint(g); } for (count = 1; count < steps; count++)// move to original posn. { leftx=lposns[0][count]; lefty=lposns[1][count]; rightx=rposns[0][count]; righty=rposns[1][count]; paint(g); } }// end of repeats loop }// end of if (posns not 0) block }//end of steps loop
System.gc(); animated = false; return; }
public boolean mouseDown(Event evt, int x, int y) { dragx = x; dragy = y; leftx=0; lefty=0; rightx=0; righty=0; lx = 1; // so that (lx != leftx) to make buildImage create new pic animated = true; paint(getGraphics()); requestFocus();
return true; }
public boolean mouseUp(Event evt, int x, int y) { if ((dragx == x) && (dragy == y)) animated = false; else dobounce = true;
return true; }
private void prepMath() { DtoRfactor = Math.PI / 180;
int i, j; Dimension d = getToolkit().getScreenSize(); int eyewidth, eyeheight, socketwidth, socketheight;
if (leftwidth > rightwidth) eyewidth = leftwidth; else eyewidth = rightwidth; i = lsockbrx - lsocktlx; j = rsockbrx - rsocktlx; if (i > j) socketwidth = i + 1; else socketwidth = j + 1; movewidth = socketwidth + eyewidth + more_x;
if (leftheight > rightheight) eyeheight=leftheight; else eyeheight=rightheight; i = lsockbry - lsocktly; j = rsockbry - rsocktly; if (i > j) socketheight = i + 1; else socketheight = j + 1; moveheight = socketheight + eyeheight + more_y;
centx = (righteye_middle_x - lefteye_middle_x) / 2; centy = moveheight / 2; factorx = centx - more_cross; if (factorx < 1) factorx = 1;// prevents /0 (unlikely)
incx = 180 / (double)d.width; incy = 180 / (double)d.height; wideH = movewidth / 2 + more_movement; // width hypotenuse (radius) highH = moveheight / 2 + more_movement; // height hypotenuse (radius) // opposite = sine(angle) * hypotenuse bno = bounces + 2; bmo = bno - 1; lposns = new int[2][bno]; rposns = new int[2][bno];
prepedMath = true; }
public boolean mouseDrag(Event evt, int x, int y) { if (!prepedMath) return true; if ((dragx == x) && (dragy == y)) return true;
double temp;
tracklx = x - lefteye_middle_x; trackly = y - lefteye_middle_y; trackrx = x - righteye_middle_x; trackry = y - righteye_middle_y;
tklx = Math.abs(tracklx); tkrx = Math.abs(trackrx); tkly = Math.abs(trackly); tkry = Math.abs(trackry);
if (centy == tkly) tkly--; // prevents /0 if (centy == tkry) tkry--; // prevents /0
// left cross-eyed bit if ((tracklx > 0) & (tracklx <= (centx + centx)) & (tkly <= centy)) { if (tracklx <= centx) temp = tklx; else temp = centx+centx-tklx; tracklx += temp * (180/factorx) / (centy/(centy-tkly)); } // right cross-eyed bit if ((trackrx < 0) & (trackrx >= -(centx + centx)) & (tkry <= centy)) { if (trackrx >= -centx) temp = tkrx; else temp = centx+centx-tkrx; trackrx -= temp * (180/factorx) / (centy/(centy-tkry)); }
leftx = (int)(Math.sin(DtoR(tracklx * incx)) * wideH); lefty = (int)(Math.sin(DtoR(trackly * incy)) * highH); rightx = (int)(Math.sin(DtoR(trackrx * incx)) * wideH); righty = (int)(Math.sin(DtoR(trackry * incy)) * highH);
animated = true; paint(getGraphics()); System.gc();
return true; }
private double DtoR(double d) { //To convert degrees to radians, multiply degrees by pi/180. return d * DtoRfactor; //(To convert radians to degrees, multiply radians by 180/pi) }
public boolean keyDown(Event evt, int nKey) { if (pnlsetup) pnl.show(); if ((nKey == 118) || (nKey == 86) || (nKey == 22)) //v V CTRL+v showStatus(version); return true; }
public boolean keyUp(Event evt, int nKey) { if (pnlsetup) pnl.hide(); showStatus(""); return true; } } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
jeudi 28 mars 2002 à 22:08:56 |
Re : Couleur de transparence

Jo
|
LOL sacre Mikonyx non g rien trouver de plsu simple, je n'est que ca :-p
------------------------------- Réponse au message : -------------------------------
oh put1 chuis vrément dans la merde moi...mdr rien de plus simple? du style SetInvisibleColor()...bouuuuuuuuuuuuhhh snirf :'( merci +++++ Miko
------------------------------- Réponse au message : -------------------------------
Salut, Bon moi je viens de trouver ca sur mon disque dur, et ca devrait pouvoir t'aider:
Quelque note ki vont ave:
when Java's createImage method encounters a transparent colour in a gif. I solved this by manipulating the pixels in arrays (while cursing Netscape), and by passing the applet a value for a notional transparent pixel in the params.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * The Watcher Applet by Don Clark. May be distributed freely for non-commercial use * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
import java.applet.*; import java.awt.*; import java.awt.image.PixelGrabber; import java.awt.image.MemoryImageSource; import java.awt.image.ImageObserver;
public class Watcher extends Applet implements Runnable { String version = "Watcher version 3";
String filename; // params... int more_movement, more_x, more_y, more_cross, bounces, mask_topleft_x, mask_topleft_y, mask_botright_x, mask_botright_y, left_topleft_x, left_topleft_y, left_botright_x, left_botright_y, right_topleft_x, right_topleft_y, right_botright_x, right_botright_y, lefteye_middle_x, lefteye_middle_y, righteye_middle_x, righteye_middle_y, applet_background, transparent, eye_background, outer_border, inner_border; Color outer_color, inner_color; // border colours
Thread wThread = null;
MemoryImageSource buildMIS;
Image loadedimage, mainpic;
int loadedimagewidth, loadedimageheight, // sizes of images... mainpicwidth, mainpicheight, maskwidth, maskheight, leftwidth, leftheight, rightwidth, rightheight, pix[], // pixel array from loadedimage m_pix[], b_pix[], //pixel arrays for mask, background, l_pix[], r_pix[], //pixel arrays for left eye, right eye m_pix_size; // size of m_pix (and b_pix) array int lsocktlx=1000, lsocktly=1000, // offsets of eye sockets on mask lsockbrx=-1, lsockbry=-1, // - left socket top left x etc. rsocktlx=1000, rsocktly=1000, // - these values are temporary, the actual rsockbrx=-1, rsockbry=-1; // - values are calculated in sockets()
int dpleft_topleft_x, dpleft_topleft_y, // offsets to topleft & botright x & y dpleft_botright_x, dpleft_botright_y, // of left eye (l_pix) on drawpic. dpright_topleft_x, dpright_topleft_y, // offsets to topleft & botright x & y dpright_botright_x, dpright_botright_y; // of right eye (r_pix) on drawpic.
double DtoRfactor, wideH, highH, // for calculating eye movements movewidth, moveheight, incx, incy; // in mouseDrag() and prepMath()
boolean got_params = true, prepedMath = false, // startup flags allLoaded = false, allSetUp = false, // startup flags animated = false, dobounce = false, // ongoing status flags imageDone; // ongoing status flag
int leftx=0, lefty=0, rightx=0, righty=0, // for moving relative to eye-centres lx = -9, ly = -9, rx = -9, ry = -9, // copies above in buildImage() dragx = -1, dragy = -1; // old posns to test if changed by drag
Panel pnl; boolean pnlsetup = false; private int pnlx = -1, pnly = -1; private Image pnlimg;
// 'local' variables globally declared to save memory // from buildImage(): int halfmask, larraysize, rarraysize, currleft_tlx, currleft_tly, currleft_brx, currleft_bry, currright_tlx, currright_tly, currright_brx, currright_bry; // from mouseDrag(): double tracklx, trackly, trackrx, trackry, centy, centx, factorx, tklx, tkrx, tkly, tkry; // from bounce(): int bno=bounces+2, bmo = bno-1; double dleftx, dlefty, drightx, drighty; int lposns[][], rposns[][];
public Watcher() { }
public String getAppletInfo() { return "The Watcher Applet\r\n" + "(c) 1998, Don Clark"; }
public void init() { String param = ""; Point p; got_params = true;
try { param = getParameter("picture"); if (param.equals("")) filename = "picture.gif"; else filename = param;
param = getParameter("applet_background"); applet_background = findColor(param); if (applet_background != -1) setBackground(new Color(applet_background));
param = getParameter("transparent"); transparent = findColor(param); if (transparent == -1) transparent = 0x01FF01;// default
param = getParameter("eye_background"); eye_background = findColor(param); if (eye_background == -1) eye_background = 0xFFDEDEDE;// default else eye_background += 0xFF000000;// add FF000000 for max. opacity
param = getParameter("outer_border").toLowerCase(); outer_border = findColor(param); if (outer_border != -1) outer_color = new Color(outer_border);
param = getParameter("inner_border").toLowerCase(); inner_border = findColor(param); if (inner_border != -1) inner_color = new Color(inner_border);
param = getParameter("mask_topleft"); p = findPoint(param); mask_topleft_x = p.x; mask_topleft_y = p.y;
param = getParameter("mask_botright"); p = findPoint(param); mask_botright_x = p.x; mask_botright_y = p.y;
param = getParameter("lefteye_topleft"); p = findPoint(param); left_topleft_x = p.x; left_topleft_y = p.y;
param = getParameter("lefteye_botright"); p = findPoint(param); left_botright_x = p.x; left_botright_y = p.y;
param = getParameter("righteye_topleft"); p = findPoint(param); right_topleft_x = p.x; right_topleft_y = p.y;
param = getParameter("righteye_botright"); p = findPoint(param); right_botright_x = p.x; right_botright_y = p.y;
param = getParameter("lefteye_middle"); p = findPoint(param); lefteye_middle_x = p.x; lefteye_middle_y = p.y;
param = getParameter("righteye_middle"); p = findPoint(param); righteye_middle_x = p.x; righteye_middle_y = p.y;
param = getParameter("more_movement"); more_movement = Integer.parseInt(param); if ((more_movement < -500) | (more_movement > 500)) more_movement = 0;// default
param = getParameter("more_x_movement"); more_x = Integer.parseInt(param); if ((more_x < 0) | (more_x > 100)) more_x = 0;// default
param = getParameter("more_y_movement"); more_y = Integer.parseInt(param); if ((more_y < 0) | (more_y > 100)) more_y = 0;// default
param = getParameter("more_cross_eyed"); more_cross = Integer.parseInt(param); if ((more_cross < 0) | (more_cross > 50)) more_cross = 0;// default
param = getParameter("bounces"); bounces = Integer.parseInt(param); if ((bounces < 0) | (bounces > 20)) bounces = 5;// default } catch (Exception e) { got_params = false; } }
public void destroy() { }
private void displayImage(Graphics g) { if (!allLoaded) return;
if (!animated) g.drawImage(mainpic, 0, 0, null); else { if (buildImage()) drawb_pix(g); } }
private boolean buildImage() { if (!allSetUp) return false;
if ((lx == leftx) & (ly == lefty) & (rx == rightx) & (ry == righty)) return false;
int i, j, k = 0, lr, lrx, lry;
currleft_tlx = dpleft_topleft_x + leftx; // this and variables currleft_tly = dpleft_topleft_y + lefty; // below are to reduce currleft_brx = dpleft_botright_x + leftx; // calculation during currleft_bry = dpleft_botright_y + lefty; // the loops currright_tlx = dpright_topleft_x + rightx; currright_tly = dpright_topleft_y + righty; currright_brx = dpright_botright_x + rightx; currright_bry = dpright_botright_y + righty; larraysize = leftwidth * leftheight; rarraysize = rightwidth * rightheight; for (i = 0; i < maskheight; i++) // blend engine: puts bkgnd colour on b_pix, { // then adds the eyes, then the mask for (j = 0; j < maskwidth; j++) { b_pix[k] = eye_background;// bkgnd colour onto b_pix if (j <= halfmask) { if ((j >= currleft_tlx) && (j < currleft_brx) && (i >= currleft_tly) && (i < currleft_bry)) { // here when left eye array overlaps left half of b_pix array lrx = j - currleft_tlx; lry = (i - currleft_tly) * leftwidth; lr = lrx + lry; if ((lr >= 0) && (lr < larraysize) && (l_pix[lr] != 0)) b_pix[k] = l_pix[lr];// left eye onto left half of b_pix } } else { if ((j >= currright_tlx) && (j < currright_brx) && (i >= currright_tly) && (i < currright_bry)) { // here when right eye array overlaps right half of b_pix array lrx = j - currright_tlx; lry = (i - currright_tly) * rightwidth; lr = lrx + lry; if ((lr >= 0) && (lr < rarraysize) && (r_pix[lr] != 0)) b_pix[k] = r_pix[lr];// right eye onto right half of b_pix } }
if (m_pix[k] != 0) b_pix[k] = m_pix[k];// mask onto b_pix ++k; } }
lx = leftx; ly = lefty; rx = rightx; ry = righty; return true; }
private synchronized void drawb_pix(Graphics g) { imageDone = false;
try { g.drawImage(createImage(buildMIS), mask_topleft_x, mask_topleft_y, this); } catch(Exception e) { imageDone = true; } while (!imageDone) showStatus("..."); }
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.ALLBITS) != 0) { imageDone = true; return false; } else return true; }
private void pnlDraw() { if (pnlsetup && pnl.isVisible()) { Graphics g = pnl.getGraphics(); g.drawImage(pnlimg, 1, 1, null); } }
public void paint(Graphics g) { if (allLoaded) { displayImage(g); pnlDraw(); } else if (!got_params) { g.drawString("Watcher applet", 5, 20); g.drawString("parameter error", 5, 30); } else { g.drawString("Loading", 5, 20); g.drawString("picture...", 5, 30); } }
public void start() { if (wThread == null) { wThread = new Thread(this); wThread.start(); } } public void stop() { if (wThread != null) { wThread.stop(); wThread = null; } }
private void loadImage() { MediaTracker tracker = new MediaTracker(this); loadedimage = getImage(getCodeBase(), filename); tracker.addImage(loadedimage, 0);
try { tracker.waitForAll(); allLoaded = !tracker.isErrorAny(); } catch (InterruptedException e) { showStatus("Loading interrupted"); allLoaded = false; }
while (loadedimage.getWidth(null) == -1) ; loadedimagewidth = loadedimage.getWidth(null); loadedimageheight = loadedimage.getHeight(null); }
private boolean getPix() { int arraysize = loadedimagewidth * loadedimageheight; pix = new int[arraysize]; PixelGrabber pixGrab = new PixelGrabber(loadedimage, 0, 0, loadedimagewidth, loadedimageheight, pix, 0, loadedimagewidth); try { pixGrab.grabPixels(); } catch (InterruptedException e) { return false; }
for (int rgb, i = 0; i < arraysize; i++) { rgb = pix[i] & 0xFFFFFF; // get the colours if (rgb == transparent) pix[i] = 0; // transparent black else pix[i] = 0xFF000000 + rgb;// fully opaque alpha transparency + colours }
return true; }
public void run() { if (!got_params) { stop(); return; } if (!allLoaded) loadImage();
if (!allLoaded) { stop(); showStatus("Error loading picture"); return; }
if (!getPix()) { stop(); showStatus("Error processing picture"); return; }
createPicture(); createMask(); createLeft(); createRight(); if (!prepedMath) prepMath(); if (!pnlsetup) pnlsetup(); allSetUp = true;
while (true) { if (dobounce) bounce(); paint(getGraphics());
try { Thread.sleep(50); } catch (InterruptedException e) { showStatus("Watcher was interrupted"); } } }
private void createPicture() { mainpicwidth = loadedimagewidth / 2; mainpicheight = loadedimageheight; mainpic = createImage(mainpicwidth, mainpicheight);
reshape(0, 0, mainpicwidth, mainpicheight);
int i, j, k = 0, mp_pix[] = new int[mainpicwidth * mainpicheight]; for (i = 0; i < mainpicheight; i++) for (j = mainpicwidth; j < (mainpicwidth+mainpicwidth); j++) mp_pix[k++] = pix[i * loadedimagewidth + j];
Image temp = createImage(new MemoryImageSource(mainpicwidth, mainpicheight, mp_pix, 0, mainpicwidth)); Graphics g = mainpic.getGraphics(); g.drawImage(temp, 0, 0, null);
if ((outer_border != -1) && (inner_border != -1)) { g.setColor(inner_color); g.drawRect(0, 0, mainpicwidth-1, mainpicheight-1); g.drawRect(1, 1, mainpicwidth-2, mainpicheight-2); g.setColor(outer_color); g.drawRect(0, 0, mainpicwidth-2, mainpicheight-2); } else if (outer_border != -1) { g.setColor(outer_color); g.drawRect(0, 0, mainpicwidth-1, mainpicheight-1); } else if (inner_border != -1) { g.setColor(inner_color); g.drawRect(1, 1, mainpicwidth-3, mainpicheight-3); } }
private void createMask() { maskwidth = 1 + mask_botright_x - mask_topleft_x; halfmask = maskwidth / 2; maskheight = 1 + mask_botright_y - mask_topleft_y;
int i, j, k = 0, start;
m_pix_size = maskwidth * maskheight; m_pix = new int[m_pix_size]; start = mask_topleft_y * loadedimagewidth + mask_topleft_x;
for (i = 0; i < maskheight; i++) for (j = 0; j < maskwidth; j++) m_pix[k++] = pix[j + start + (i * loadedimagewidth)];
b_pix = new int[m_pix_size];// array for buildImage to lay bkgnd colour // & eyes & mask on buildMIS = new MemoryImageSource(maskwidth, maskheight, b_pix, 0, maskwidth); sockets(); }
private void sockets() { // finds offsets of eye sockets on mask by looking for pixel values of 0
int i, j, k = 0, l = maskwidth/2;
for (i = 0; i < maskheight; i++) { for (j = 0; j < maskwidth; j++) { if (m_pix[k++] == 0) { if (j <= l)// left half { if (j < lsocktlx) lsocktlx = j; if (i < lsocktly) lsocktly = i; if (j > lsockbrx) lsockbrx = j; if (i > lsockbry) lsockbry = i; } else // right half { if (j < rsocktlx) rsocktlx = j; if (i < rsocktly) rsocktly = i; if (j > rsockbrx) rsockbrx = j; if (i > rsockbry) rsockbry = i; } } } } }
private void createLeft() { leftwidth = 1 + left_botright_x - left_topleft_x; leftheight = 1 + left_botright_y - left_topleft_y; larraysize = leftwidth * leftheight;
int i, j, k = 0, start;
l_pix = new int[larraysize]; start = left_topleft_y * loadedimagewidth + left_topleft_x;
for (i = 0; i < leftheight; i++)// copy eye pixels into l_pix array for (j = 0; j < leftwidth; j++) l_pix[k++] = pix[j + start + (i * loadedimagewidth)];
// offsets to topleft & botright x & y of eye (l_pix) on drawpic dpleft_topleft_x = lefteye_middle_x - mask_topleft_x - (leftwidth/2); dpleft_topleft_y = lefteye_middle_y - mask_topleft_y - (leftheight/2); dpleft_botright_x = dpleft_topleft_x + leftwidth; dpleft_botright_y = dpleft_topleft_y + leftheight; }
private void createRight() { rightwidth = 1 + right_botright_x - right_topleft_x; rightheight = 1 + right_botright_y - right_topleft_y; rarraysize = rightwidth * rightheight;
int i, j, k = 0, start;
r_pix = new int[rarraysize]; start = right_topleft_y * loadedimagewidth + right_topleft_x;
for (i = 0; i < rightheight; i++)// copy eye pixels into r_pix array for (j = 0; j < rightwidth; j++) r_pix[k++] = pix[j + start + (i * loadedimagewidth)];
// offsets to topleft & botright x & y of eye (r_pix) on drawpic dpright_topleft_x = righteye_middle_x - mask_topleft_x - (rightwidth/2); dpright_topleft_y = righteye_middle_y - mask_topleft_y - (rightheight/2); dpright_botright_x = dpright_topleft_x + rightwidth; dpright_botright_y = dpright_topleft_y + rightheight; }
private int findColor(String s) { int n, i; Point p; String t; s = s.trim();
if (s.indexOf("none") != -1) return -1; if (s.indexOf("black") != -1) return 0x000000; // (0, 0, 0); if (s.indexOf("white") != -1) return 0xFFFFFF; // (255, 255, 255); if (s.indexOf("red") != -1) return 0xFF0000; // (255, 0, 0); if (s.indexOf("green") != -1) return 0x00FF00; // (0, 255, 0); if (s.indexOf("blue") != -1) return 0x0000FF; // (0, 0, 255); if (s.indexOf("dark") != -1) return 0x404040; // (64, 64, 64); if (s.indexOf("light") != -1) return 0xC0C0C0; // (192, 192, 192); if (s.indexOf("gray") != -1) return 0x808080; // (128, 128, 128); if (s.indexOf("grey") != -1) return 0x808080; // (128, 128, 128); if (s.indexOf("yellow") != -1) return 0xFFFF00; // (255, 255, 0); if (s.indexOf("cyan") != -1) return 0x00FFFF; // (0, 255, 255); if (s.indexOf("magenta") != -1) return 0xFF00FF; // (255, 0, 255); if (s.indexOf("orange") != -1) return 0xFFC800; // (255, 200, 0); if (s.indexOf("pink") != -1) return 0xFFAFAF; // (255, 175, 175);
if (s.indexOf("#") != -1)// if hex format #number { s = s.replace('#', ' '); s = s.trim(); try {n = Integer.parseInt(s, 16);}; catch (NumberFormatException nfe) {n = -1;} return n % 0x1000000; }
i = s.indexOf(","); if (i == -1) return -1;
try {t = s.substring(0, i+1) + " 0";} catch (StringIndexOutOfBoundsException e) {return -1;} p = findPoint(t); n = p.x << 16; // got red (p.y ignored)
try {t = s.substring(i+1);} catch (StringIndexOutOfBoundsException e) {return -1;} p = findPoint(t); n += p.x << 8; // got green n += p.y; // got blue return n % 0x1000000; }
private Point findPoint(String s) { int i, j, len = s.length(); String one = "", two = ""; boolean building = false, building_one = false, building_two = false; char c;
for (i = 0; i < len; i++) { c = s.charAt(i); if (Character.isDigit(c)) { if (!building) { building = true; if (building_one) { building_two = true; two += String.valueOf(c); } else { building_one = true; one += String.valueOf(c); } } else { if (building_two) two += String.valueOf(c); else one += String.valueOf(c); } } else { building = false; } }
try {i = Integer.parseInt(one);} catch (NumberFormatException nfe) {i = 0;}
try {j = Integer.parseInt(two);} catch (NumberFormatException nfe) {j = 0;}
return new Point(i, j); }
private void pnlsetup() { setLayout(null); pnl = new Panel(); add(pnl); pnl.setBackground(Color.black); pnl.hide();
pnlx = size().width-4;
String s = "The Watcher Applet from"; String t = "donclark@powernet.co.uk"; Font gf = pnl.getFont(); boolean toobig = true; Font f = null; FontMetrics fm = null; int w, sw = 0, tw = 0, ps = 19; while (toobig) { ps -= 1; if (ps == 8) toobig = false; f = new Font(gf.getName(), Font.PLAIN, ps); pnl.setFont(f); fm = pnl.getFontMetrics(f); sw = fm.stringWidth(s); tw = fm.stringWidth(t); if (sw > tw) w = sw; else w = tw; if (w < (pnlx - 4)) toobig = false; }
int ma = fm.getMaxAscent(), md = fm.getMaxDescent(); int imgwidth = pnlx - 2, imgheight = (ma + md + 2) * 2;
pnly = imgheight + 2; pnl.resize(pnlx, pnly); pnl.move(2, mainpicheight-pnly-2);// allows for a 2 pixel border
pnlimg = pnl.createImage(imgwidth, imgheight); Graphics g = pnlimg.getGraphics(); g.setFont(f); g.setColor(Color.black); g.fillRect(0, 0, imgwidth-1, imgheight-1); g.setColor(Color.white); g.drawRect(0, 0, imgwidth-1, imgheight-1); g.drawString(s, (imgwidth-sw-2)/2, imgheight/2 - md - 1); g.drawString(t, (imgwidth-tw-2)/2, imgheight - md - 3);
pnlsetup = true; }
private void bounce() { dobounce = false; if (bounces == 0) { animated = false; return; }
// lposns[][] & rposns[][] store the left & right x & y posns for moving the // eyes to centre [x][0] & [y][0], from start posns [x][bmo] & [y][bmo]. // bno = bounce number = total no. of posns, from 0,0 to start positions. // bmo = bounce number minus one
dleftx = leftx; dlefty = lefty; drightx = rightx; drighty = righty;
int count; for (count = 0; count < bno; count++) { lposns[0][count] = (int)(dleftx / bmo * count); lposns[1][count] = (int)(dlefty / bmo * count); rposns[0][count] = (int)(drightx / bmo * count); rposns[1][count] = (int)(drighty / bmo * count); }
Graphics g = getGraphics(); animated = true; int steps, rep_count, rep_total;
for (steps = bmo; steps > 0; steps--) // do steps (number of bounce sequences) { if (!(lposns[0][steps]==0 && lposns[1][steps]==0 && // if(!...) block to rposns[0][steps]==0 && rposns[1][steps]==0)) // skip non-movements { rep_total = 1; // as bounce gets smaller it is repeated if (bounces < 3) { if ((bounces == 2) && (steps == 1)) rep_total = 2; } else if ((steps == 2) || (steps == 3)) rep_total = 2; else if (steps == 1) rep_total = 3;
for (rep_count = 0; rep_count < rep_total; rep_count++) // do repeats { for (count = steps; count >= 0; count--) // move to centre { leftx=lposns[0][count]; lefty=lposns[1][count]; rightx=rposns[0][count]; righty=rposns[1][count]; paint(g); } for (count = 1; count < steps; count++)// move to other side { leftx=-lposns[0][count]; lefty=-lposns[1][count]; rightx=-rposns[0][count]; righty=-rposns[1][count]; paint(g); } for (count = steps; count >= 0 ; count--)// move to centre again { leftx=-lposns[0][count]; lefty=-lposns[1][count]; rightx=-rposns[0][count]; righty=-rposns[1][count]; paint(g); } for (count = 1; count < steps; count++)// move to original posn. { leftx=lposns[0][count]; lefty=lposns[1][count]; rightx=rposns[0][count]; righty=rposns[1][count]; paint(g); } }// end of repeats loop }// end of if (posns not 0) block }//end of steps loop
System.gc(); animated = false; return; }
public boolean mouseDown(Event evt, int x, int y) { dragx = x; dragy = y; leftx=0; lefty=0; rightx=0; righty=0; lx = 1; // so that (lx != leftx) to make buildImage create new pic animated = true; paint(getGraphics()); requestFocus();
return true; }
public boolean mouseUp(Event evt, int x, int y) { if ((dragx == x) && (dragy == y)) animated = false; else dobounce = true;
return true; }
private void prepMath() { DtoRfactor = Math.PI / 180;
int i, j; Dimension d = getToolkit().getScreenSize(); int eyewidth, eyeheight, socketwidth, socketheight;
if (leftwidth > rightwidth) eyewidth = leftwidth; else eyewidth = rightwidth; i = lsockbrx - lsocktlx; j = rsockbrx - rsocktlx; if (i > j) socketwidth = i + 1; else socketwidth = j + 1; movewidth = socketwidth + eyewidth + more_x;
if (leftheight > rightheight) eyeheight=leftheight; else eyeheight=rightheight; i = lsockbry - lsocktly; j = rsockbry - rsocktly; if (i > j) socketheight = i + 1; else socketheight = j + 1; moveheight = socketheight + eyeheight + more_y;
centx = (righteye_middle_x - lefteye_middle_x) / 2; centy = moveheight / 2; factorx = centx - more_cross; if (factorx < 1) factorx = 1;// prevents /0 (unlikely)
incx = 180 / (double)d.width; incy = 180 / (double)d.height; wideH = movewidth / 2 + more_movement; // width hypotenuse (radius) highH = moveheight / 2 + more_movement; // height hypotenuse (radius) // opposite = sine(angle) * hypotenuse bno = bounces + 2; bmo = bno - 1; lposns = new int[2][bno]; rposns = new int[2][bno];
prepedMath = true; }
public boolean mouseDrag(Event evt, int x, int y) { if (!prepedMath) return true; if ((dragx == x) && (dragy == y)) return true;
double temp;
tracklx = x - lefteye_middle_x; trackly = y - lefteye_middle_y; trackrx = x - righteye_middle_x; trackry = y - righteye_middle_y;
tklx = Math.abs(tracklx); tkrx = Math.abs(trackrx); tkly = Math.abs(trackly); tkry = Math.abs(trackry);
if (centy == tkly) tkly--; // prevents /0 if (centy == tkry) tkry--; // prevents /0
// left cross-eyed bit if ((tracklx > 0) & (tracklx <= (centx + centx)) & (tkly <= centy)) { if (tracklx <= centx) temp = tklx; else temp = centx+centx-tklx; tracklx += temp * (180/factorx) / (centy/(centy-tkly)); } // right cross-eyed bit if ((trackrx < 0) & (trackrx >= -(centx + centx)) & (tkry <= centy)) { if (trackrx >= -centx) temp = tkrx; else temp = centx+centx-tkrx; trackrx -= temp * (180/factorx) / (centy/(centy-tkry)); }
leftx = (int)(Math.sin(DtoR(tracklx * incx)) * wideH); lefty = (int)(Math.sin(DtoR(trackly * incy)) * highH); rightx = (int)(Math.sin(DtoR(trackrx * incx)) * wideH); righty = (int)(Math.sin(DtoR(trackry * incy)) * highH);
animated = true; paint(getGraphics()); System.gc();
return true; }
private double DtoR(double d) { //To convert degrees to radians, multiply degrees by pi/180. return d * DtoRfactor; //(To convert radians to degrees, multiply radians by 180/pi) }
public boolean keyDown(Event evt, int nKey) { if (pnlsetup) pnl.show(); if ((nKey == 118) || (nKey == 86) || (nKey == 22)) //v V CTRL+v showStatus(version); return true; }
public boolean keyUp(Event evt, int nKey) { if (pnlsetup) pnl.hide(); showStatus(""); return true; } } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
Cette discussion est classée dans : couleur, transparence
Répondre à ce message
Sujets en rapport avec ce message
couleur de fond [ par sneark ]
Slt tlm,Je suis débutant et j'aimerai savoir un petit truc:Bon j'ai trouver ça pour changer la couleur de fond sur un applet: setBackground(new Color(
surface d'un dessin sur un graphics [ par papis ]
Bonjour,je suis en train de developper une appliquation de dessin, j'ai un petit probleme pour calculer la surface d'un dessin. Un dessin est formé de
De la couleur dans mon JLabel [ par mcdohl ]
Salut a tous, Je n'arrive pas a changer la couleur du texte dans mon JLabel, est-ce que qqn sait comment je peux faire afin d'y arriver ?Merci d'avanc
Changer la couleur du texte en mode console [ par Sw1tch ]
Salut,J'aimerai changer le couleur du texte que j'affiche dans la console avec System.out.print("Salut") ; et par la même occasion savoir s'il est pos
Texte en couleur [ par debianc14 ]
Comment afficher un texte en couleur ?J'ai trouvée cette exemple, mais il ne fonctionne pas.Quel importation manque-t-il ?Merci d'avanceclass Test {
changer la couleur d'un boutton, ahhhh!! [ par babou6 ]
Aplease, j'ai besoin de votre aide, je vais bientot peter un plomb!!!!!J'ai fait une fenetre avec un gridlayout de dimension variable(l'utilisateur le
Plz Help !!!!!!!!!!!!!! getColor ????? [ par lipao17 ]
Salut j'ai besoin d'aide, je suis entrain de faire un applet HTML en Java et je cherche à faire un truc pour recupérer la couleur !!!Genre j'ai plusie
font jtextarea [ par fma88 ]
bonjours à tousj'ai une application qui affiche des messages dans un jtextarea.Je voudrais pouvoir affecter une font ou une couleur différente suivan
couleur specifique a trouver [ par Rockit72 ]
Voila ce que je voudrais faire en application java.Je voudrait savoir comment trouver une couleur specifique(par exemple vert) sur le bureau entier de
couleur des pixels [ par leo2 ]
Bonjour à tous,Je n'arrive pas à récupérer le RVB d'une image !J'ai crée un constructeur int R, int V, int B !Puis un tableau, mais impossible de récu
Livres en rapport
|
Derniers Blogs
GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc SLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPSSLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPS par Vko
Retrouvez les slides et les démo de ma session Fast & Furious XAML Apps. A ceux qui se posent la question : "est-ce que le code de la DataGrid est disponible?", je vous répondrais "pas encore". Je vais mettre en place un projet codeplex pour part...
Cliquez pour lire la suite de l'article par Vko XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : COURRE : COUR par Julien39
Cliquez pour lire la suite par Julien39
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|