TAG:
Demonstration of Java and javascript communication import java.awt.Graphics; import java.awt.Color; import java.awt.Component; import java.awt.Event; import java.awt.Image; import java.awt.*; import java.net.*; public class ColorPicker extends java.applet.Applet { Image offscreenPalette, offscreenSwatch; Graphics offscreenGPalette, offscreenGSwatch, paletteG; int rval, gval, bval, rectedge = 15, x, y; Button ButBgColor, ButTxtColor; int bgrval, bggval, bgbval, txtrval, txtgval, txtbval; String str, bgstr, txtstr; String rstr, gstr, bstr; String bgColor, txtColor; Point rect, oldrect; boolean backclr, txtclr; public void init() { backclr = true; txtclr = false; bgrval = 255; bggval = 255; bgbval = 255; bgstr = "Backgroud RGB = (FF,FF,FF)"; bgColor = "#FFFFFF"; txtrval = 0; txtgval = 0; txtbval = 0; txtstr = "Text RGB = (00,00,00)"; txtColor = "#000000"; ButBgColor = new Button("Background Color"); ButTxtColor = new Button("Text Color"); add(ButBgColor); add(ButTxtColor); ButBgColor.disable(); str = bgstr + " " + txtstr; rect = new Point((rectedge * 35) + rectedge, (rectedge * 5) + 30 ); oldrect = new Point(-30, -30); setBackground(new Color(255, 255, 255)); offscreenPalette = this.createImage((rectedge * 36) + 1, (rectedge * 6) + 1); offscreenSwatch = this.createImage(this.size().width, 80); offscreenGPalette = offscreenPalette.getGraphics(); offscreenGSwatch = offscreenSwatch.getGraphics(); y = 0; for ( rval = 0; rval <= 255; rval += 51 ) { x = 0; for ( gval = 0; gval <= 255; gval += 51 ) { for ( bval = 0; bval <= 255; bval += 51 ) { offscreenGPalette.setColor(new Color(rval, gval, bval)); offscreenGPalette.fillRect(x, y, rectedge, rectedge); offscreenGPalette.setColor(Color.black); offscreenGPalette.drawRect(x, y, rectedge, rectedge); x += rectedge; } } y += rectedge; } } public void update(Graphics g) { paint(g); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { String label = (String)arg; txtclr = false; backclr = false; if (label.equals("Background Color")) { if (! ButTxtColor.isEnabled()) ButTxtColor.enable(); ButBgColor.disable(); backclr = true; } else if (label.equals("Text Color")) { if (! ButBgColor.isEnabled()) ButBgColor.enable(); txtclr = true; ButTxtColor.disable(); } } return true; } public void paint(Graphics g) { g.drawImage(offscreenPalette, rectedge, 30, offscreenPalette.getWidth(this), offscreenPalette.getHeight(this), this); offscreenGSwatch.setFont(new Font("Times", Font.BOLD, 16)); offscreenGSwatch.setColor(new Color(bgrval, bggval, bgbval)); offscreenGSwatch.fillRect(0, 0, offscreenSwatch.getWidth(this), offscreenSwatch.getHeight(this)); offscreenGSwatch.setColor(new Color(txtrval, txtgval, txtbval)); offscreenGSwatch.drawString(str, 100, 40); g.drawImage(offscreenSwatch, 0, 130, this); g.setColor(Color.black); g.drawRect(oldrect.x, oldrect.y, rectedge, rectedge); g.setColor(Color.white); g.drawRect(rect.x, rect.y, rectedge, rectedge); oldrect.x = rect.x; oldrect.y = rect.y; } public boolean mouseDown(Event evt, int x, int y) { if ( y < 30 || y > (30 +(6 * rectedge)) || x < rectedge || x > (rectedge + (36 * rectedge)) ) { str = "Please click the color in the RGB palette."; } else { y = (y - 30) / rectedge; rval = 51 * y; rect.y = rectedge * y + 30; x = (x - rectedge) / rectedge; rect.x = rectedge * x + rectedge; bval = 51 * (x % 6); gval = 51 * (x / 6); rstr = int2hexstr(rval); gstr = int2hexstr(gval); bstr = int2hexstr(bval); if ( backclr ) { bgrval = rval; bggval = gval; bgbval = bval; bgstr = "Background Color = (" + rstr + "," + gstr + "," + bstr + ")"; bgColor = "#" + rstr + gstr + bstr; } else if ( txtclr ) { txtrval = rval; txtgval = gval; txtbval = bval; txtstr = "Text Color = (" + rstr + "," + gstr + "," + bstr + ")"; txtColor = "#" + rstr + gstr + bstr; } str = bgstr + " " + txtstr; } repaint(); return true; } /* The getBgColor() and getTxtColor() methods should be declared as public methods, else you will have problems running this applet in some browsers doing java and javascript communications. Thanks to Wm. Davis <billydavis@hotmail.com> who tells me this. */ public String getBgColor() { return bgColor; } public String getTxtColor() { return txtColor; } String int2hexstr(int rgbval) { String str1 = "", str2 = "", cstr = ""; int c1, c2; c1 = rgbval / 16; c2 = rgbval % 16; str1 = hex2str(c1); str2 = hex2str(c2); cstr = str1 + str2; return cstr; } String hex2str(int hexint) { String hexchar = ""; switch(hexint) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: hexchar = Integer.toString(hexint); break; case 10: hexchar = "A"; break; case 11: hexchar = "B"; break; case 12: hexchar = "C"; break; case 13: hexchar = "D"; break; case 14: hexchar = "E"; break; case 15: hexchar = "F"; break; } return hexchar; } } (iwgh) |