jdk/src/share/demo/applets/DitherTest/DitherTest.java
author nloodin
Wed, 31 Aug 2011 13:48:04 +0200
changeset 10292 ed7db6a12c2a
parent 8960 dbf27b0abc4c
permissions -rw-r--r--
7067811: Update demo/sample code to state it should not be used for production Summary: Added comment block after copyright block stating that code is unfit for production. Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
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
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8960
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.applet.Applet;
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    43
import java.awt.AWTEvent;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    44
import java.awt.BorderLayout;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    45
import java.awt.Button;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    46
import java.awt.Canvas;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    47
import java.awt.Choice;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    48
import java.awt.Color;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    49
import java.awt.Dimension;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
import java.awt.FlowLayout;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    51
import java.awt.FontMetrics;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
import java.awt.Frame;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    53
import java.awt.Graphics;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
import java.awt.Image;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
import java.awt.Label;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
import java.awt.LayoutManager;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
import java.awt.Panel;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
import java.awt.TextField;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
import java.awt.Toolkit;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
import java.awt.event.ActionEvent;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
import java.awt.event.ActionListener;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    62
import java.awt.event.KeyEvent;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    63
import java.awt.event.TextEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.awt.image.MemoryImageSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
enum DitherMethod {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    69
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    70
    NOOP, RED, GREEN, BLUE, ALPHA, SATURATION
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
};
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    73
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    74
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class DitherTest extends Applet implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private Thread runner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private DitherControls XControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private DitherControls YControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private DitherCanvas canvas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Frame f = new Frame("DitherTest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        DitherTest ditherTest = new DitherTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ditherTest.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        f.add("Center", ditherTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        f.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ditherTest.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    92
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String xspec = null, yspec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int xvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int yvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            xspec = getParameter("xaxis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            yspec = getParameter("yaxis");
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   101
        } catch (NullPointerException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            //only occurs if run as application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (xspec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            xspec = "red";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (yspec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            yspec = "blue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
        DitherMethod xmethod = colorMethod(xspec, xvals);
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   112
        DitherMethod ymethod = colorMethod(yspec, yvals);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        XControls = new DitherControls(this, xvals[0], xvals[1],
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   116
                xmethod, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        YControls = new DitherControls(this, yvals[0], yvals[1],
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   118
                ymethod, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        YControls.addRenderButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        add("North", XControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        add("South", YControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        add("Center", canvas = new DitherCanvas());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   125
    private DitherMethod colorMethod(String s, int vals[]) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   126
        DitherMethod method = DitherMethod.NOOP;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String lower = s.toLowerCase();
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   131
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   132
        for (DitherMethod m : DitherMethod.values()) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   133
            if (lower.startsWith(m.toString().toLowerCase())) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   134
                method = m;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   135
                lower = lower.substring(m.toString().length());
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   136
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   138
        if (method == DitherMethod.NOOP) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            vals[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            vals[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int begval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int endval = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int dash = lower.indexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (dash < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                endval = Integer.parseInt(lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                begval = Integer.parseInt(lower.substring(0, dash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                endval = Integer.parseInt(lower.substring(dash + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   153
        } catch (NumberFormatException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (begval < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            begval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else if (begval > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            begval = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (endval < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            endval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else if (endval > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            endval = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        vals[0] = begval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        vals[1] = endval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Calculates and returns the image.  Halts the calculation and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * null if the Applet is stopped during the calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private Image calculateImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int width = canvas.getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int height = canvas.getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int xvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int yvals[] = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int xmethod = XControls.getParams(xvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int ymethod = YControls.getParams(yvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int pixels[] = new int[width * height];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int c[] = new int[4];   //temporarily holds R,G,B,A information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        for (int j = 0; j < height; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            for (int i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                c[0] = c[1] = c[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                c[3] = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (xmethod < ymethod) {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   194
                    applyMethod(c, xmethod, i, width, xvals);
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   195
                    applyMethod(c, ymethod, j, height, yvals);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                } else {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   197
                    applyMethod(c, ymethod, j, height, yvals);
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   198
                    applyMethod(c, xmethod, i, width, xvals);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   200
                pixels[index++] = ((c[3] << 24) | (c[0] << 16) | (c[1] << 8)
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   201
                        | c[2]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // Poll once per row to see if we've been told to stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (runner != me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return createImage(new MemoryImageSource(width, height,
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   210
                ColorModel.getRGBdefault(), pixels, 0, width));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   213
    private void applyMethod(int c[], int methodIndex, int step,
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   214
            int total, int vals[]) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   215
        DitherMethod method = DitherMethod.values()[methodIndex];
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   216
        if (method == DitherMethod.NOOP) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int val = ((total < 2)
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   220
                ? vals[0]
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   221
                : vals[0] + ((vals[1] - vals[0]) * step / (total - 1)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        switch (method) {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   223
            case RED:
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   224
                c[0] = val;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   225
                break;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   226
            case GREEN:
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   227
                c[1] = val;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   228
                break;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   229
            case BLUE:
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   230
                c[2] = val;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   231
                break;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   232
            case ALPHA:
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   233
                c[3] = val;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   234
                break;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   235
            case SATURATION:
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   236
                int max = Math.max(Math.max(c[0], c[1]), c[2]);
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   237
                int min = max * (255 - val) / 255;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   238
                if (c[0] == 0) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   239
                    c[0] = min;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   240
                }
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   241
                if (c[1] == 0) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   242
                    c[1] = min;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   243
                }
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   244
                if (c[2] == 0) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   245
                    c[2] = min;
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   246
                }
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   247
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   251
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        runner = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        runner.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   257
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        canvas.setImage(null);  // Wipe previous image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        Image img = calculateImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (img != null && runner == Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            canvas.setImage(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   266
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        runner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   271
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        remove(XControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        remove(YControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        remove(canvas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   278
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public String getAppletInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return "An interactive demonstration of dithering.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   283
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public String[][] getParameterInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String[][] info = {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   286
            { "xaxis", "{RED, GREEN, BLUE, ALPHA, SATURATION}",
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   287
                "The color of the Y axis.  Default is RED." },
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   288
            { "yaxis", "{RED, GREEN, BLUE, ALPHA, SATURATION}",
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   289
                "The color of the X axis.  Default is BLUE." }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   295
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   296
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
class DitherCanvas extends Canvas {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   298
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private Image img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static String calcString = "Calculating...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   302
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int w = getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int h = getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (img == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            int x = (w - fm.stringWidth(calcString)) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            int y = h / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            g.drawString(calcString, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            g.drawImage(img, 0, 0, w, h, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   318
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void update(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   323
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return new Dimension(20, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   328
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return new Dimension(200, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void setImage(Image img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        this.img = img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   343
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   344
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
class DitherControls extends Panel implements ActionListener {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   346
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private CardinalTextField start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private CardinalTextField end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private Button button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private Choice choice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private DitherTest applet;
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   352
    private static LayoutManager dcLayout = new FlowLayout(FlowLayout.CENTER,
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   353
            10, 5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   355
    public DitherControls(DitherTest app, int s, int e, DitherMethod type,
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   356
            boolean vertical) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        applet = app;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        setLayout(dcLayout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        add(new Label(vertical ? "Vertical" : "Horizontal"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        add(choice = new Choice());
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   361
        for (DitherMethod m : DitherMethod.values()) {
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   362
            choice.addItem(m.toString().substring(0, 1)
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   363
                    + m.toString().substring(1).toLowerCase());
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   364
        }
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   365
        choice.select(type.ordinal());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        add(start = new CardinalTextField(Integer.toString(s), 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        add(end = new CardinalTextField(Integer.toString(e), 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /* puts on the button */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public void addRenderButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        add(button = new Button("New Image"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        button.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /* retrieves data from the user input fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public int getParams(int vals[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            vals[0] = scale(Integer.parseInt(start.getText()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            vals[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            vals[1] = scale(Integer.parseInt(end.getText()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            vals[1] = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return choice.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /* fits the number between 0 and 255 inclusive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private int scale(int number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (number < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } else if (number > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            number = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /* called when user clicks the button */
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   402
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (e.getSource() == button) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            applet.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   410
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   411
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
class CardinalTextField extends TextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    String oldText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public CardinalTextField(String text, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        super(text, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        oldText = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    // Consume non-digit KeyTyped events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    // Note that processTextEvent kind of eliminates the need for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    // function, but this is neater, since ideally, it would prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    // the text from appearing at all.  Sigh.  See bugid 4100317/4114565.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    //
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   427
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    protected void processEvent(AWTEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int id = evt.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (id != KeyEvent.KEY_TYPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            super.processEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        KeyEvent kevt = (KeyEvent) evt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        char c = kevt.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // Digits, backspace, and delete are okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        // Note that the minus sign is not allowed (neither is decimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (Character.isDigit(c) || (c == '\b') || (c == '\u007f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            super.processEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        kevt.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    // Should consume TextEvents for non-integer Strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    // Store away the text in the tf for every TextEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // so we can revert to it on a TextEvent (paste, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // legal key in the wrong location) with bad text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    // Note: it would be easy to extend this to an eight-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    // TextField (range 0-255), but I'll leave it as-is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    //
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   457
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    protected void processTextEvent(TextEvent te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // The empty string is okay, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        String newText = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (newText.equals("") || textIsCardinal(newText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            oldText = newText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            super.processTextEvent(te);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        setText(oldText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    // Returns true for Cardinal (non-negative) numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // Note that the empty string is not allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private boolean textIsCardinal(String textToCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        try {
8960
dbf27b0abc4c 7027678: /applets/DitherTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   475
            return Integer.parseInt(textToCheck, 10) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
}