src/demo/share/jfc/J2Ddemo/java2d/demos/Clipping/Intersection.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 52252 de9486d74a74
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50146
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     1
/*
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     2
 *
52252
de9486d74a74 8211693: Convert C-style array declarations in client demos and jdk.accessibility
tvaleev
parents: 50146
diff changeset
     3
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
50146
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     4
 *
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     5
 * Redistribution and use in source and binary forms, with or without
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     6
 * modification, are permitted provided that the following conditions
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     7
 * are met:
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     8
 *
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
     9
 *   - Redistributions of source code must retain the above copyright
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    10
 *     notice, this list of conditions and the following disclaimer.
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    11
 *
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    12
 *   - Redistributions in binary form must reproduce the above copyright
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    13
 *     notice, this list of conditions and the following disclaimer in the
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    14
 *     documentation and/or other materials provided with the distribution.
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    15
 *
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    16
 *   - Neither the name of Oracle nor the names of its
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    17
 *     contributors may be used to endorse or promote products derived
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    18
 *     from this software without specific prior written permission.
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    19
 *
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    21
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    22
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    23
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    24
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    25
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    26
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    27
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    28
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    29
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    31
 */
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    32
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    33
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    34
package java2d.demos.Clipping;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    35
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    36
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    37
import java.awt.*;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    38
import java.awt.event.*;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    39
import java.awt.geom.*;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    40
import java.awt.font.FontRenderContext;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    41
import java.awt.font.TextLayout;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    42
import javax.swing.*;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    43
import java2d.AnimatingControlsSurface;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    44
import java2d.CustomControls;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    45
import static java.awt.Color.*;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    46
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    47
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    48
/**
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    49
 * Animated intersection clipping of lines, an image and a textured rectangle.
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    50
 */
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    51
@SuppressWarnings("serial")
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    52
public class Intersection extends AnimatingControlsSurface {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    53
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    54
    private static final int HEIGHT_DECREASE = 0;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    55
    private static final int HEIGHT_INCREASE = 1;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    56
    private static final int  WIDTH_DECREASE = 2;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    57
    private static final int  WIDTH_INCREASE = 3;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    58
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    59
    private int xx, yy, ww, hh;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    60
    private int direction = HEIGHT_DECREASE;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    61
    private int angdeg;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    62
    private Shape textshape;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    63
    private double sw, sh;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    64
    private GeneralPath ovals;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    65
    private Rectangle2D rectshape;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    66
    protected boolean doIntersection = true;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    67
    protected boolean doOvals = true;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    68
    protected boolean doText;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    69
    protected boolean threeSixty;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    70
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    71
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    72
    public Intersection() {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    73
        setBackground(WHITE);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    74
        setControls(new Component[] { new DemoControls(this) });
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    75
    }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    76
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    77
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    78
    @Override
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    79
    public void reset(int w, int h) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    80
        xx = yy = 0;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    81
        ww = w-1; hh = h;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    82
        direction = HEIGHT_DECREASE;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    83
        angdeg = 0;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    84
        FontRenderContext frc = new FontRenderContext(null, true, false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    85
        Font f = new Font(Font.SERIF, Font.BOLD,32);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    86
        TextLayout tl = new TextLayout("J2D", f, frc);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    87
        sw = tl.getBounds().getWidth();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    88
        sh = tl.getBounds().getHeight();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    89
        int size = Math.min(w, h);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    90
        double sx = (size-40)/sw;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    91
        double sy = (size-100)/sh;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    92
        AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    93
        textshape = tl.getOutline(Tx);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    94
        rectshape = textshape.getBounds();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    95
        sw = rectshape.getWidth();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    96
        sh = rectshape.getHeight();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    97
        ovals = new GeneralPath();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    98
        ovals.append(new Ellipse2D.Double(  10,   10, 20, 20), false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
    99
        ovals.append(new Ellipse2D.Double(w-30,   10, 20, 20), false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   100
        ovals.append(new Ellipse2D.Double(  10, h-30, 20, 20), false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   101
        ovals.append(new Ellipse2D.Double(w-30, h-30, 20, 20), false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   102
    }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   103
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   104
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   105
    @Override
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   106
    public void step(int w, int h) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   107
        if (direction == HEIGHT_DECREASE) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   108
            yy+=2; hh-=4;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   109
            if (yy >= h/2) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   110
                direction = HEIGHT_INCREASE;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   111
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   112
        } else if (direction == HEIGHT_INCREASE) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   113
            yy-=2; hh+=4;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   114
            if (yy <= 0) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   115
                direction = WIDTH_DECREASE;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   116
                hh = h-1; yy = 0;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   117
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   118
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   119
        if (direction == WIDTH_DECREASE) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   120
            xx+=2; ww-=4;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   121
            if (xx >= w/2) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   122
                direction = WIDTH_INCREASE;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   123
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   124
        } else if (direction == WIDTH_INCREASE) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   125
            xx-=2; ww+=4;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   126
            if (xx <= 0) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   127
                direction = HEIGHT_DECREASE;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   128
                ww = w-1; xx = 0;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   129
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   130
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   131
        if ((angdeg += 5) == 360) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   132
            angdeg = 0;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   133
            threeSixty = true;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   134
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   135
    }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   136
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   137
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   138
    @Override
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   139
    public void render(int w, int h, Graphics2D g2) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   140
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   141
        Rectangle rect = new Rectangle(xx, yy, ww, hh);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   142
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   143
        AffineTransform Tx = new AffineTransform();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   144
        Tx.rotate(Math.toRadians(angdeg),w/2,h/2);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   145
        Tx.translate(w/2-sw/2, sh+(h-sh)/2);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   146
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   147
        GeneralPath path = new GeneralPath();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   148
        if (doOvals) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   149
            path.append(ovals, false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   150
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   151
        if (doText) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   152
            path.append(Tx.createTransformedShape(textshape), false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   153
        } else {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   154
            path.append(Tx.createTransformedShape(rectshape), false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   155
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   156
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   157
        if (doIntersection) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   158
            g2.clip(rect);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   159
            g2.clip(path);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   160
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   161
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   162
        g2.setColor(GREEN);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   163
        g2.fill(rect);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   164
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   165
        g2.setClip(new Rectangle(0, 0, w, h));
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   166
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   167
        g2.setColor(LIGHT_GRAY);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   168
        g2.draw(rect);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   169
        g2.setColor(BLACK);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   170
        g2.draw(path);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   171
    }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   172
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   173
52252
de9486d74a74 8211693: Convert C-style array declarations in client demos and jdk.accessibility
tvaleev
parents: 50146
diff changeset
   174
    public static void main(String[] argv) {
50146
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   175
        createDemoFrame(new Intersection());
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   176
    }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   177
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   178
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   179
    static final class DemoControls extends CustomControls implements ActionListener {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   180
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   181
        Intersection demo;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   182
        JToolBar toolbar;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   183
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   184
        public DemoControls(Intersection demo) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   185
            super(demo.name);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   186
            this.demo = demo;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   187
            add(toolbar = new JToolBar());
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   188
            toolbar.setFloatable(false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   189
            addTool("Intersect", true );
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   190
            addTool("Text",      false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   191
            addTool("Ovals",     true );
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   192
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   193
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   194
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   195
        public void addTool(String str, boolean state) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   196
            JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str));
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   197
            b.setFocusPainted(false);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   198
            b.setSelected(state);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   199
            b.addActionListener(this);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   200
            int width = b.getPreferredSize().width;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   201
            Dimension prefSize = new Dimension(width, 21);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   202
            b.setPreferredSize(prefSize);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   203
            b.setMaximumSize(  prefSize);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   204
            b.setMinimumSize(  prefSize);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   205
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   206
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   207
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   208
        @Override
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   209
        public void actionPerformed(ActionEvent e) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   210
            JToggleButton b = (JToggleButton) e.getSource();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   211
            if (b.getText().equals("Intersect")) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   212
                demo.doIntersection = b.isSelected();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   213
            } else if (b.getText().equals("Ovals")) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   214
                demo.doOvals = b.isSelected();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   215
            } else if (b.getText().equals("Text")) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   216
                demo.doText = b.isSelected();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   217
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   218
            if (!demo.animating.running()) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   219
                demo.repaint();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   220
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   221
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   222
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   223
        @Override
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   224
        public Dimension getPreferredSize() {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   225
            return new Dimension(200,40);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   226
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   227
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   228
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   229
        @Override
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   230
        @SuppressWarnings("SleepWhileHoldingLock")
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   231
        public void run() {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   232
            Thread me = Thread.currentThread();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   233
            while (thread == me) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   234
                if (demo.threeSixty) {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   235
                    ((AbstractButton) toolbar.getComponentAtIndex(1)).doClick();
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   236
                    demo.threeSixty = false;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   237
                }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   238
                try {
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   239
                    Thread.sleep(500);
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   240
                } catch (InterruptedException e) { return; }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   241
            }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   242
            thread = null;
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   243
        }
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   244
    } // End DemoControls
0bb0e464ee76 8198991: Move Java2D demo to the open repository
prr
parents:
diff changeset
   245
} // End Intersection