home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Frame;
- import java.awt.Image;
- import java.awt.Window;
- import java.awt.image.ColorModel;
- import java.awt.image.MemoryImageSource;
-
- public class DitherTest extends Applet implements Runnable {
- static final int NOOP = 0;
- static final int RED = 1;
- static final int GREEN = 2;
- static final int BLUE = 3;
- static final int ALPHA = 4;
- static final int SATURATION = 5;
- Thread kicker;
- DitherControls XControls;
- DitherControls YControls;
- DitherCanvas canvas;
-
- public synchronized void start() {
- if (this.canvas.getImage() == null) {
- this.kicker = new Thread(this);
- this.kicker.start();
- }
-
- }
-
- public void restart() {
- this.stop();
- this.canvas.setImage((Image)null);
- this.start();
- }
-
- public synchronized void stop() {
- try {
- if (this.kicker != null) {
- this.kicker.stop();
- }
- } catch (Exception var3) {
- }
-
- this.kicker = null;
- }
-
- void applymethod(int[] c, int method, int step, int total, int[] vals) {
- if (method != 0) {
- int val = total < 2 ? vals[0] : vals[0] + (vals[1] - vals[0]) * step / (total - 1);
- switch (method) {
- case 1:
- c[0] = val;
- break;
- case 2:
- c[1] = val;
- break;
- case 3:
- c[2] = val;
- break;
- case 4:
- c[3] = val;
- break;
- case 5:
- int max = Math.max(Math.max(c[0], c[1]), c[2]);
- int min = max * (255 - val) / 255;
- if (c[0] == 0) {
- c[0] = min;
- }
-
- if (c[1] == 0) {
- c[1] = min;
- }
-
- if (c[2] == 0) {
- c[2] = min;
- }
- }
-
- }
- }
-
- int colormethod(String s, int[] vals) {
- int method = 0;
- if (s == null) {
- s = "";
- }
-
- String lower = s.toLowerCase();
- int len = 0;
- if (lower.startsWith("red")) {
- method = 1;
- lower = lower.substring(3);
- } else if (lower.startsWith("green")) {
- method = 2;
- lower = lower.substring(5);
- } else if (lower.startsWith("blue")) {
- method = 3;
- lower = lower.substring(4);
- } else if (lower.startsWith("alpha")) {
- method = 4;
- lower = lower.substring(4);
- } else if (lower.startsWith("saturation")) {
- method = 5;
- lower = lower.substring(10);
- }
-
- if (method == 0) {
- vals[0] = 0;
- vals[1] = 0;
- return method;
- } else {
- int begval = 0;
- int endval = 255;
-
- try {
- int dash = lower.indexOf(45);
- if (dash < 0) {
- begval = endval = Integer.parseInt(lower);
- } else {
- begval = Integer.parseInt(lower.substring(0, dash));
- endval = Integer.parseInt(lower.substring(dash + 1));
- }
- } catch (Exception var10) {
- }
-
- if (begval < 0) {
- begval = 0;
- }
-
- if (endval < 0) {
- endval = 0;
- }
-
- if (begval > 255) {
- begval = 255;
- }
-
- if (endval > 255) {
- endval = 255;
- }
-
- vals[0] = begval;
- vals[1] = endval;
- return method;
- }
- }
-
- synchronized void newImage(Thread me, int width, int height, int[] pixels) {
- if (this.kicker == me) {
- Image img = ((Component)this).createImage(new MemoryImageSource(width, height, ColorModel.getRGBdefault(), pixels, 0, width));
- this.canvas.setImage(img);
- this.kicker = null;
- }
- }
-
- public void run() {
- Thread me = Thread.currentThread();
- me.setPriority(4);
- int width = this.canvas.size().width;
- int height = this.canvas.size().height;
- int[] xvals = new int[2];
- int[] yvals = new int[2];
- int xmethod = this.XControls.getParams(xvals);
- int ymethod = this.YControls.getParams(yvals);
- int[] pixels = new int[width * height];
- int[] c = new int[4];
- int index = 0;
-
- for(int j = 0; j < height; ++j) {
- for(int i = 0; i < width; ++i) {
- c[0] = c[1] = c[2] = 0;
- c[3] = 255;
- if (xmethod < ymethod) {
- this.applymethod(c, xmethod, i, width, xvals);
- this.applymethod(c, ymethod, j, height, yvals);
- } else {
- this.applymethod(c, ymethod, j, height, yvals);
- this.applymethod(c, xmethod, i, width, xvals);
- }
-
- pixels[index++] = c[3] << 24 | c[0] << 16 | c[1] << 8 | c[2];
- if (this.kicker != me) {
- return;
- }
- }
- }
-
- this.newImage(me, width, height, pixels);
- }
-
- public void init() {
- int[] xvals = new int[2];
- int[] yvals = new int[2];
-
- String xspec;
- try {
- xspec = ((Applet)this).getParameter("xaxis");
- } catch (Exception var9) {
- xspec = null;
- }
-
- String yspec;
- try {
- yspec = ((Applet)this).getParameter("yaxis");
- } catch (Exception var8) {
- yspec = null;
- }
-
- if (xspec == null) {
- xspec = "red";
- }
-
- if (yspec == null) {
- yspec = "blue";
- }
-
- int xmethod = this.colormethod(xspec, xvals);
- int ymethod = this.colormethod(yspec, yvals);
- ((Container)this).setLayout(new BorderLayout());
- this.XControls = new DitherControls(this, xvals[0], xvals[1], xmethod, false);
- this.YControls = new DitherControls(this, yvals[0], yvals[1], ymethod, true);
- this.YControls.addRenderButton();
- ((Container)this).add("North", this.XControls);
- ((Container)this).add("South", this.YControls);
- ((Container)this).add("Center", this.canvas = new DitherCanvas());
- }
-
- public static void main(String[] args) {
- Frame f = new Frame("ArcTest");
- DitherTest ditherTest = new DitherTest();
- ditherTest.init();
- ((Container)f).add("Center", ditherTest);
- ((Window)f).pack();
- ((Window)f).show();
- ditherTest.start();
- }
- }
-