| 1 |
/* ControlCurve.java |
| 2 |
Copyright (C) 2012 Tim Lambert, Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
IcedTea is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with IcedTea; see the file COPYING. If not, write to the |
| 18 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
02110-1301 USA. |
| 20 |
|
| 21 |
Linking this library statically or dynamically with other modules is |
| 22 |
making a combined work based on this library. Thus, the terms and |
| 23 |
conditions of the GNU General Public License cover the whole |
| 24 |
combination. |
| 25 |
|
| 26 |
As a special exception, the copyright holders of this library give you |
| 27 |
permission to link this library with independent modules to produce an |
| 28 |
executable, regardless of the license terms of these independent |
| 29 |
modules, and to copy and distribute the resulting executable under |
| 30 |
terms of your choice, provided that you also meet, for each linked |
| 31 |
independent module, the terms and conditions of the license of that |
| 32 |
module. An independent module is a module which is not derived from |
| 33 |
or based on this library. If you modify this library, you may extend |
| 34 |
this exception to your version of the library, but you are not |
| 35 |
obligated to do so. If you do not wish to do so, delete this |
| 36 |
exception statement from your version. */ |
| 37 |
package net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012; |
| 38 |
|
| 39 |
/** This class represents a curve defined by a sequence of control points */ |
| 40 |
|
| 41 |
/* |
| 42 |
* This class is part of the NatCubic implementation (http://www.cse.unsw.edu.au/~lambert/) |
| 43 |
* which does not have a license. The author (Tim Lambert) has agreed to |
| 44 |
* license this under GPL+Classpath by email |
| 45 |
* |
| 46 |
*/ |
| 47 |
import java.awt.*; |
| 48 |
|
| 49 |
public class ControlCurve { |
| 50 |
|
| 51 |
protected Polygon pts; |
| 52 |
protected Polygon result; |
| 53 |
protected boolean withPoints = true; |
| 54 |
protected int selection = -1; |
| 55 |
|
| 56 |
public ControlCurve() { |
| 57 |
pts = new Polygon(); |
| 58 |
} |
| 59 |
|
| 60 |
public ControlCurve(Polygon p) { |
| 61 |
pts = p; |
| 62 |
} |
| 63 |
|
| 64 |
public Polygon getSourcePolygon() { |
| 65 |
return pts; |
| 66 |
} |
| 67 |
|
| 68 |
public void setSourcePolygon(Polygon pts) { |
| 69 |
this.pts = pts; |
| 70 |
} |
| 71 |
static Font f = new Font("Courier", Font.PLAIN, 12); |
| 72 |
|
| 73 |
/** |
| 74 |
* to be overwriten |
| 75 |
*/ |
| 76 |
public Polygon calcualteResult() { |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
public void calcualteAndSaveResult() { |
| 81 |
result = calcualteResult(); |
| 82 |
} |
| 83 |
|
| 84 |
/** paint this curve into g.*/ |
| 85 |
public void paint(Graphics g) { |
| 86 |
if (isWithPoints()) { |
| 87 |
FontMetrics fm = g.getFontMetrics(f); |
| 88 |
g.setFont(f); |
| 89 |
int h = fm.getAscent() / 2; |
| 90 |
|
| 91 |
for (int i = 0; i < pts.npoints; i++) { |
| 92 |
String s = Integer.toString(i); |
| 93 |
int w = fm.stringWidth(s) / 2; |
| 94 |
g.drawString(Integer.toString(i), pts.xpoints[i] - w, pts.ypoints[i] + h); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
static final int EPSILON = 36; /* square of distance for picking */ |
| 99 |
|
| 100 |
|
| 101 |
/** return index of control point near to (x,y) or -1 if nothing near */ |
| 102 |
public int selectPoint(int x, int y) { |
| 103 |
int mind = Integer.MAX_VALUE; |
| 104 |
selection = -1; |
| 105 |
for (int i = 0; i < pts.npoints; i++) { |
| 106 |
int d = sqr(pts.xpoints[i] - x) + sqr(pts.ypoints[i] - y); |
| 107 |
if (d < mind && d < EPSILON) { |
| 108 |
mind = d; |
| 109 |
selection = i; |
| 110 |
} |
| 111 |
} |
| 112 |
return selection; |
| 113 |
} |
| 114 |
|
| 115 |
// square of an int |
| 116 |
static int sqr(int x) { |
| 117 |
return x * x; |
| 118 |
} |
| 119 |
|
| 120 |
public Polygon getResult() { |
| 121 |
return result; |
| 122 |
} |
| 123 |
|
| 124 |
public void resetResult() { |
| 125 |
this.result = null; |
| 126 |
} |
| 127 |
|
| 128 |
/** add a control point, return index of new control point */ |
| 129 |
public int addPoint(int x, int y) { |
| 130 |
pts.addPoint(x, y); |
| 131 |
resetResult(); |
| 132 |
return selection = pts.npoints - 1; |
| 133 |
} |
| 134 |
|
| 135 |
/** set selected control point */ |
| 136 |
public void setPoint(int x, int y) { |
| 137 |
setPoint(selection, x, y); |
| 138 |
} |
| 139 |
|
| 140 |
/** set selected control point */ |
| 141 |
public void setPoint(int index, int x, int y) { |
| 142 |
if (index >= 0 && index < pts.npoints) { |
| 143 |
pts.xpoints[index] = x; |
| 144 |
pts.ypoints[index] = y; |
| 145 |
resetResult(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** remove selected control point */ |
| 150 |
public void removePoint(int index) { |
| 151 |
if (index >= 0 && index < pts.npoints) { |
| 152 |
pts.npoints--; |
| 153 |
for (int i = index; i < pts.npoints; i++) { |
| 154 |
pts.xpoints[i] = pts.xpoints[i + 1]; |
| 155 |
pts.ypoints[i] = pts.ypoints[i + 1]; |
| 156 |
} |
| 157 |
resetResult(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** remove selected control point */ |
| 162 |
public void removePoint() { |
| 163 |
removePoint(selection); |
| 164 |
} |
| 165 |
|
| 166 |
public boolean isWithPoints() { |
| 167 |
return withPoints; |
| 168 |
} |
| 169 |
|
| 170 |
public void setWithPoints(boolean withPoints) { |
| 171 |
this.withPoints = withPoints; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public String toString() { |
| 176 |
StringBuilder r = new StringBuilder(); |
| 177 |
for (int i = 0; i < pts.npoints; i++) { |
| 178 |
r.append(" ").append(pts.xpoints[i]).append(" ").append(pts.ypoints[i]); |
| 179 |
} |
| 180 |
return r.toString(); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* for testing purposes |
| 185 |
* @param selection |
| 186 |
*/ |
| 187 |
void setSelection(int selection) { |
| 188 |
this.selection = selection; |
| 189 |
} |
| 190 |
} |