import java.applet.*; import java.awt.*; import java.awt.image.*; public class SimpleDithering extends Applet { Image backImage; Button button; TextField textField; Label labelField; final int WIDTH = 256; final int HEIGHT = 256; public void init() { this.draw (0, 255, 0); button = new Button("Red"); add(button); button = new Button("Green"); add(button); button = new Button("Blue"); add(button); labelField = new Label("Intensity"); add(labelField); textField = new TextField(3); add(textField); textField.setText("255"); repaint(); } public boolean action(Event event, Object arg) { try { String str = textField.getText(); int intensity = Integer.parseInt(str); if (intensity < 0) { intensity = 0; textField.setText("0"); } if (intensity > 255) { intensity = 255; textField.setText("255"); } repaint(); if (arg == "Red") this.draw (intensity, 0, 0); else if (arg == "Green") this.draw (0, intensity, 0); else if (arg == "Blue") this.draw (0, 0, intensity); } catch (Exception e) { } repaint(); return true; } public void draw(int r, int g, int b) { int imageBits[] = new int[WIDTH * HEIGHT]; for (int row = 0; row < HEIGHT; ++row) { int red = (row * r) / (HEIGHT - 1); int green = (row * g) / (HEIGHT - 1); int blue = (row * b) / (HEIGHT - 1); int alpha = 255; for (int col = 0; col < WIDTH; ++col) { int index = row * WIDTH + col; imageBits[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue; } } MemoryImageSource producer = new MemoryImageSource (WIDTH, HEIGHT, imageBits, 0, WIDTH); backImage = createImage(producer); } public void paint(Graphics g) { g.drawImage(backImage, 0, 0, this); } }