import java.awt.*; import java.awt.image.*; import java.applet.*; import java.lang.*; import java.util.*; public class Image_and_cursor extends Applet { Graphics bG, cG, sG; Image bI, cI; Image backdrop; String backdrop_filename; Color cursor_color, back_color, controls_color; Color pixel_color; int backdrop_array[] = new int[1000000]; PixelGrabber get_backdrop; int cursor_x; int cursor_y; int image_height; int image_width; int height; int width; int cursor_red, cursor_green, cursor_blue; int back_red, back_green, back_blue; int controls_red, controls_green, controls_blue; int margin = 40; int arrow_size = 10; int font_size; Font textfont; int mid_x; int mid_y; int left_point; int right_point; int top_point; int bottom_point; int color_sw; // 0 = do not display pixel color // 1 = display pixel color as three integers // 2 = display pixel color as three reals public void init() { font_size = Integer.parseInt(getParameter("font_size")); image_height = Integer.parseInt(getParameter("image_height")); image_width = Integer.parseInt(getParameter("image_width")); mid_x = margin + image_width/2; mid_y = margin + image_height/2; left_point = 20; right_point = image_width + 2 * margin - left_point; top_point = left_point; bottom_point = image_height + 2 * margin - left_point; height = image_height + 3 * margin; width = image_width + 2 * margin; bI = createImage(width, height); bG = bI.getGraphics(); textfont = new Font("Courier", Font.BOLD, font_size); bG.setFont(textfont); cI = createImage(width, height); cG = cI.getGraphics(); backdrop_filename = getParameter("backdrop_file"); backdrop = getImage(getCodeBase(), backdrop_filename); color_sw = Integer.parseInt(getParameter("color_sw")); cursor_red = Integer.parseInt(getParameter("cursor_red")); cursor_green = Integer.parseInt(getParameter("cursor_green")); cursor_blue = Integer.parseInt(getParameter("cursor_blue")); cursor_x = Integer.parseInt(getParameter("cursor_x")); cursor_y = Integer.parseInt(getParameter("cursor_y")); back_red = Integer.parseInt(getParameter("back_red")); back_green = Integer.parseInt(getParameter("back_green")); back_blue = Integer.parseInt(getParameter("back_blue")); controls_red = Integer.parseInt(getParameter("controls_red")); controls_green = Integer.parseInt(getParameter("controls_green")); controls_blue = Integer.parseInt(getParameter("controls_blue")); cursor_color = new Color(cursor_red, cursor_green, cursor_blue); back_color = new Color(back_red, back_green, back_blue); controls_color = new Color(controls_red, controls_green, controls_blue); get_backdrop = new PixelGrabber( backdrop, 0, 0, image_width, image_height, backdrop_array, 0, image_width); try {get_backdrop.grabPixels();} catch(Exception e) {} } public void paint(Graphics g) { cG.setColor(back_color); cG.fillRect(0, 0, width, height); cG.drawImage(backdrop, margin, margin, this); show_colors(); g.drawImage(bI, 0, 0, this); sG = getGraphics(); } public void show_colors() { int pixel, red, green, blue; pixel = cursor_y * image_width + cursor_x; red = (0xff & (backdrop_array[pixel] >> 16)); green = (0xff & (backdrop_array[pixel] >> 8)); blue = (0xff & backdrop_array[pixel]); pixel_color = new Color(red, green, blue); bG.drawImage(cI, 0, 0, this); drawcursors(cursor_x, cursor_y); bG.setColor(back_color); bG.fillRect(0, image_height + 2 * margin, width, margin); bG.setColor(controls_color); bG.drawString("x: " + String.valueOf(cursor_x), margin, image_height + 3 * margin - 40); bG.drawString("y: " + String.valueOf(image_height - cursor_y), margin, image_height + 3 * margin - 10); if (color_sw > 0) { bG.setColor(pixel_color); bG.fillRect(width/2 - 20, image_height + 5 * margin/2 - 10, 40, 20); if (color_sw == 1) { bG.setColor(controls_color); bG.drawString("red: " + String.valueOf(red), width/2 + 60, image_height + 3 * margin - 51); bG.drawString("green: " + String.valueOf(green), width/2 + 60, image_height + 3 * margin - 26); bG.drawString("blue: " + String.valueOf(blue), width/2 + 60, image_height + 3 * margin - 1); } else { bG.setColor(controls_color); bG.drawString("red: " + three_places(red/255.0), width/2 + 60, image_height + 3 * margin - 51); bG.drawString("green: " + three_places(green/255.0), width/2 + 60, image_height + 3 * margin - 26); bG.drawString("blue: " + three_places(blue/255.0), width/2 + 60, image_height + 3 * margin - 1); } } } public void drawcursors(int x, int y) { bG.setColor(cursor_color); bG.drawLine(cursor_x + margin, margin, cursor_x + margin, margin + image_height - 1); bG.drawLine(margin, cursor_y + margin, margin + image_width - 1, cursor_y + margin); for (int i = 0; i <= arrow_size; i = i + 1) { bG.drawLine(left_point + i, mid_y - i, left_point + i, mid_y + i); bG.drawLine(right_point - i, mid_y - i, right_point - i, mid_y + i); bG.drawLine(mid_x - i, top_point + i, mid_x + i, top_point + i); bG.drawLine(mid_x - i, bottom_point - i, mid_x + i, bottom_point - i); } } public boolean mouseDown(Event evt, int mx, int my) { if ((mx >= margin) & (mx <= margin + image_width) & (my >= margin) & (my <= margin + image_height)) { cursor_x = mx - margin; cursor_y = my - margin; } if ((left_point <= mx) & (mx <= left_point + arrow_size) & (mid_y - arrow_size <= my) & (my <= mid_y + arrow_size)) { cursor_x = java.lang.Math.max(0, cursor_x - 1); } if ((right_point - arrow_size <= mx) & (mx <= right_point) & (mid_y - arrow_size <= my) & (my <= mid_y + arrow_size)) { cursor_x = java.lang.Math.min(image_width, cursor_x + 1); } if ((top_point <= my) & (my <= top_point + arrow_size) & (mid_x - arrow_size <= mx) & (mx <= mid_x + arrow_size)) { cursor_y = java.lang.Math.max(0, cursor_y - 1); } if ((bottom_point - arrow_size <= my) & (my <= bottom_point) & (mid_x - arrow_size <= mx) & (mx <= mid_x + arrow_size)) { cursor_y = java.lang.Math.min(image_height, cursor_y + 1); } show_colors(); sG.drawImage(bI, 0, 0, this); return true; } public String three_places(double x) { int ival; double dval; String sval; String test = "."; int n; ival = (int) java.lang.Math.round(1000 * x); dval = ival/1000.0; sval = java.lang.Double.toString(dval); n = sval.length(); if (n >= 1) { if (test.equals(sval.substring(n - 1, n))) { sval = sval + "000"; } } if (n >= 2) { if (test.equals(sval.substring(n - 2, n - 1))) { sval = sval + "00"; } } if (n >= 3) { if (test.equals(sval.substring(n - 3, n - 2))) { sval = sval + "0"; } } return sval; } }