jdk/src/share/demo/applets/DitherTest/DitherTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.MemoryImageSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class DitherTest extends Applet implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    final private static int NOOP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    final private static int RED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    final private static int GREEN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    final private static int BLUE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    final private static int ALPHA = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final private static int SATURATION = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Thread runner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private DitherControls XControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private DitherControls YControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private DitherCanvas canvas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Frame f = new Frame("DitherTest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        DitherTest ditherTest = new DitherTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ditherTest.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        f.add("Center", ditherTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        f.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ditherTest.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        String xspec = null, yspec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int xvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int yvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            xspec = getParameter("xaxis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            yspec = getParameter("yaxis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            //only occurs if run as application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (xspec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            xspec = "red";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (yspec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            yspec = "blue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int xmethod = colormethod(xspec, xvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int ymethod = colormethod(yspec, yvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        XControls = new DitherControls(this, xvals[0], xvals[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                       xmethod, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        YControls = new DitherControls(this, yvals[0], yvals[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                       ymethod, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        YControls.addRenderButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        add("North", XControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        add("South", YControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        add("Center", canvas = new DitherCanvas());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private int colormethod(String s, int vals[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int method = NOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String lower = s.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (lower.startsWith("red")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            method = RED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            lower = lower.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else if (lower.startsWith("green")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            method = GREEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            lower = lower.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } else if (lower.startsWith("blue")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            method = BLUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            lower = lower.substring(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else if (lower.startsWith("alpha")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            method = ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            lower = lower.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else if (lower.startsWith("saturation")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            method = SATURATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            lower = lower.substring(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (method == NOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            vals[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            vals[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int begval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int endval = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            int dash = lower.indexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (dash < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                endval = Integer.parseInt(lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                begval = Integer.parseInt(lower.substring(0, dash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                endval = Integer.parseInt(lower.substring(dash + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (begval < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            begval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else if (begval > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            begval = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (endval < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            endval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if (endval > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            endval = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        vals[0] = begval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        vals[1] = endval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Calculates and returns the image.  Halts the calculation and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * null if the Applet is stopped during the calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private Image calculateImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int width = canvas.getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int height = canvas.getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int xvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int yvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int xmethod = XControls.getParams(xvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int ymethod = YControls.getParams(yvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int pixels[] = new int[width * height];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int c[] = new int[4];   //temporarily holds R,G,B,A information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        for (int j = 0; j < height; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            for (int i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                c[0] = c[1] = c[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                c[3] = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (xmethod < ymethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    applymethod(c, xmethod, i, width, xvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    applymethod(c, ymethod, j, height, yvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    applymethod(c, ymethod, j, height, yvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    applymethod(c, xmethod, i, width, xvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                pixels[index++] = ((c[3] << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                   (c[0] << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                   (c[1] << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                   c[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // Poll once per row to see if we've been told to stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (runner != me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return createImage(new MemoryImageSource(width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            ColorModel.getRGBdefault(), pixels, 0, width));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private void applymethod(int c[], int method, int step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                             int total, int vals[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (method == NOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int val = ((total < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                   ? vals[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                   : vals[0] + ((vals[1] - vals[0]) * step / (total - 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        switch (method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        case RED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            c[0] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        case GREEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            c[1] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        case BLUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            c[2] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        case ALPHA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            c[3] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        case SATURATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            int max = Math.max(Math.max(c[0], c[1]), c[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            int min = max * (255 - val) / 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (c[0] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                c[0] = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (c[1] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                c[1] = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (c[2] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                c[2] = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        runner = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        runner.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        canvas.setImage(null);  // Wipe previous image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Image img = calculateImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (img != null && runner == Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            canvas.setImage(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        runner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        remove(XControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        remove(YControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        remove(canvas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public String getAppletInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return "An interactive demonstration of dithering.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public String[][] getParameterInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        String[][] info = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            {"xaxis", "{RED, GREEN, BLUE, ALPHA, SATURATION}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
             "The color of the Y axis.  Default is RED."},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            {"yaxis", "{RED, GREEN, BLUE, ALPHA, SATURATION}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
             "The color of the X axis.  Default is BLUE."}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
class DitherCanvas extends Canvas {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private Image img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static String calcString = "Calculating...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int w = getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int h = getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (img == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            int x = (w - fm.stringWidth(calcString)) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            int y = h / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            g.drawString(calcString, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            g.drawImage(img, 0, 0, w, h, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void update(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return new Dimension(20, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return new Dimension(200, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void setImage(Image img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        this.img = img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
class DitherControls extends Panel implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private CardinalTextField start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private CardinalTextField end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private Button button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private Choice choice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private DitherTest applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private static LayoutManager dcLayout = new FlowLayout(FlowLayout.CENTER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                                           10, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public DitherControls(DitherTest app, int s, int e, int type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                          boolean vertical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        applet = app;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        setLayout(dcLayout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        add(new Label(vertical ? "Vertical" : "Horizontal"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        add(choice = new Choice());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        choice.addItem("Noop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        choice.addItem("Red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        choice.addItem("Green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        choice.addItem("Blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        choice.addItem("Alpha");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        choice.addItem("Saturation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        choice.select(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        add(start = new CardinalTextField(Integer.toString(s), 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        add(end = new CardinalTextField(Integer.toString(e), 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /* puts on the button */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void addRenderButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        add(button = new Button("New Image"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        button.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /* retrieves data from the user input fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public int getParams(int vals[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            vals[0] = scale(Integer.parseInt(start.getText()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            vals[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            vals[1] = scale(Integer.parseInt(end.getText()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            vals[1] = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return choice.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /* fits the number between 0 and 255 inclusive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private int scale(int number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (number < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else if (number > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            number = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /* called when user clicks the button */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (e.getSource() == button) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            applet.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
class CardinalTextField extends TextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    String oldText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public CardinalTextField(String text, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        super(text, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        oldText = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // Consume non-digit KeyTyped events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    // Note that processTextEvent kind of eliminates the need for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // function, but this is neater, since ideally, it would prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    // the text from appearing at all.  Sigh.  See bugid 4100317/4114565.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    protected void processEvent(AWTEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int id = evt.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (id != KeyEvent.KEY_TYPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            super.processEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        KeyEvent kevt = (KeyEvent) evt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        char c = kevt.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // Digits, backspace, and delete are okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // Note that the minus sign is not allowed (neither is decimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (Character.isDigit(c) || (c == '\b') || (c == '\u007f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            super.processEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        kevt.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    // Should consume TextEvents for non-integer Strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    // Store away the text in the tf for every TextEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    // so we can revert to it on a TextEvent (paste, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    // legal key in the wrong location) with bad text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    // Note: it would be easy to extend this to an eight-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    // TextField (range 0-255), but I'll leave it as-is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    protected void processTextEvent(TextEvent te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // The empty string is okay, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        String newText = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (newText.equals("") || textIsCardinal(newText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            oldText = newText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            super.processTextEvent(te);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        setText(oldText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    // Returns true for Cardinal (non-negative) numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    // Note that the empty string is not allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private boolean textIsCardinal(String textToCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        int value = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            value = Integer.parseInt(textToCheck, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return (value >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}