📄 drawutil.java
字号:
} else if (y1 > centerY) {
y1--;
isPositionMoved = true;
}
if (y2 < centerY) {
y2++;
isPositionMoved = true;
} else if (y2 > centerY) {
y2--;
isPositionMoved = true;
}
if (y3 < centerY) {
y3++;
isPositionMoved = true;
} else if (y3 > centerY) {
y3--;
isPositionMoved = true;
}
} while (isPositionMoved);
//#endif
}
/**
* Retrieves the center position of all numbers
* @param n1 first number
* @param n2 second number
* @param n3 third number
* @return the center of all numbers: min( n1, n2, n3 ) + (max( n1, n2, n3 ) - min( n1, n2, n3 )) / 2
*/
public static int getCenter(int n1, int n2, int n3) {
int max = Math.max( n1, Math.max( n2, n3) );
int min = Math.min( n1, Math.min( n2, n3 ) );
return min + ((max - min) / 2);
}
/**
* Draws the specified triangle.
*
* @param x1 the x coordinate of the first vertex of the triangle
* @param y1 the y coordinate of the first vertex of the triangle
* @param x2 the x coordinate of the second vertex of the triangle
* @param y2 the y coordinate of the second vertex of the triangle
* @param x3 the x coordinate of the third vertex of the triangle
* @param y3 the y coordinate of the third vertex of the triangle
* @param g the graphics context
*/
public static void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, Graphics g) {
g.drawLine( x1, y1, x2, y2 );
g.drawLine( x2, y2, x3, y3 );
g.drawLine( x3, y3, x1, y1 );
}
/**
* Finds the index of the smallest element
*
* @param elements the elements
* @return the index of the smallest element
*/
static int indexOfLeast(int[] elements) {
int index = 0;
int least = elements[0];
for (int i=1; i<elements.length; i++) {
if (elements[i] < least) {
index = i;
least = elements[i];
}
}
return index;
}
/**
* Checks whether the specified point px, py is within the triangle defined by ax, ay, bx, by and cx, cy.
*
* @param px The x of the point to test
* @param py The y of the point to test
* @param ax The x of the 1st point of the triangle
* @param ay The y of the 1st point of the triangle
* @param bx The x of the 2nd point of the triangle
* @param by The y of the 2nd point of the triangle
* @param cx The x of the 3rd point of the triangle
* @param cy The y of the 3rd point of the triangle
* @return true when the point is within the given triangle
*/
private static boolean withinBounds(int px, int py,
int ax, int ay,
int bx, int by,
int cx, int cy)
{
if ( px < Math.min(ax, Math.min( bx, cx ) )
|| px > Math.max(ax, Math.max( bx, cx ) )
|| py < Math.min(ay, Math.min( by, cy ) )
|| py > Math.max(ay, Math.max( by, cy ) ) )
{
return false;
}
boolean sameabc = sameSide(px, py, ax, ay, bx, by, cx, cy);
boolean samebac = sameSide(px, py, bx, by, ax, ay, cx, cy);
boolean samecab = sameSide(px, py, cx, cy, ax, ay, bx, by);
return sameabc && samebac && samecab;
}
private static boolean sameSide (int p1x, int p1y, int p2x, int p2y,
int l1x, int l1y, int l2x, int l2y)
{
long lhs = ((p1x - l1x) * (l2y - l1y) - (l2x - l1x) * (p1y - l1y));
long rhs = ((p2x - l1x) * (l2y - l1y) - (l2x - l1x) * (p2y - l1y));
long product = lhs * rhs;
boolean result = product >= 0;
return result;
}
private static int[][] trimEar(int[] xPoints, int[] yPoints, int earIndex) {
int[] newXPoints = new int[xPoints.length - 1];
int[] newYPoints = new int[yPoints.length - 1];
int[][] newPoly = new int[2][];
newPoly[0] = newXPoints;
newPoly[1] = newYPoints;
int p = 0;
for (int i=0; i<xPoints.length; i++) {
if (i != earIndex) {
newXPoints[p] = xPoints[i];
newYPoints[p] = yPoints[i];
p++;
}
}
return newPoly;
}
private static int[][][] split(int[] xPoints, int[] yPoints, int aIndex, int bIndex) {
int firstLen, secondLen;
if (bIndex < aIndex) {
firstLen = (xPoints.length - aIndex) + bIndex + 1;
} else {
firstLen = (bIndex - aIndex) + 1;
}
secondLen = (xPoints.length - firstLen) + 2;
int[][] first = new int[2][firstLen];
int[][] second = new int[2][secondLen];
for (int i=0; i<firstLen; i++) {
int index = (aIndex + i) % xPoints.length;
first[0][i] = xPoints[index];
first[1][i] = yPoints[index];
}
for (int i=0; i<secondLen; i++) {
int index = (bIndex + i) % xPoints.length;
second[0][i] = xPoints[index];
second[1][i] = yPoints[index];
}
int[][][] result = new int[2][][];
result[0] = first;
result[1] = second;
return result;
}
/**
* Creates a gradient of colors.
* This method is highly optimized and only uses bit-shifting and additions (no multitplication nor devision), but
* it will create a new integer array in each call.
*
* @param startColor the first color
* @param endColor the last color
* @param steps the number of colors in the gradient,
* when 2 is given, the first one will be the startColor and the second one will the endColor.
* @return an int array with the gradient.
* @see #getGradient(int, int, int[])
* @see #getGradientColor(int, int, int)
*/
public static final int[] getGradient( int startColor, int endColor, int steps ) {
int[] gradient = new int[ steps ];
getGradient(startColor, endColor, gradient);
return gradient;
}
/**
* Creates a gradient of colors.
* This method is highly optimized and only uses bit-shifting and additions (no multitplication nor devision).
*
* @param startColor the first color
* @param endColor the last color
* @param gradient the array in which the gradient colors are stored.
* @see #getGradientColor(int, int, int, int)
*/
public static final void getGradient(int startColor, int endColor, int[] gradient) {
int steps = gradient.length;
if (steps == 0) {
return;
} else if (steps == 1) {
gradient[0] = startColor;
return;
}
int startAlpha = startColor >>> 24;
int startRed = (startColor >>> 16) & 0x00FF;
int startGreen = (startColor >>> 8) & 0x0000FF;
int startBlue = startColor & 0x00000FF;
int endAlpha = endColor >>> 24;
int endRed = (endColor >>> 16) & 0x00FF;
int endGreen = (endColor >>> 8) & 0x0000FF;
int endBlue = endColor & 0x00000FF;
int stepAlpha = ((endAlpha - startAlpha) << 8) / (steps-1);
int stepRed = ((endRed -startRed) << 8) / (steps-1);
int stepGreen = ((endGreen - startGreen) << 8) / (steps-1);
int stepBlue = ((endBlue - startBlue) << 8) / (steps-1);
// System.out.println("step red=" + Integer.toHexString(stepRed));
// System.out.println("step green=" + Integer.toHexString(stepGreen));
// System.out.println("step blue=" + Integer.toHexString(stepBlue));
startAlpha <<= 8;
startRed <<= 8;
startGreen <<= 8;
startBlue <<= 8;
gradient[0] = startColor;
for (int i = 1; i < steps; i++) {
startAlpha += stepAlpha;
startRed += stepRed;
startGreen += stepGreen;
startBlue += stepBlue;
gradient[i] = (( startAlpha << 16) & 0xFF000000)
| (( startRed << 8) & 0x00FF0000)
| ( startGreen & 0x0000FF00)
| ( startBlue >>> 8);
//| (( startBlue >>> 8) & 0x000000FF);
}
}
/**
* Retrieves the gradient color between the given start and end colors.
*
* @param startColor the start color
* @param endColor the end color
* @param permille the permille between 0 and 1000 - 0 will return the startColor, 1000 the endColor,
* 500 a gradient color directly in the middlet between start and endcolor.
* @return the gradient color
*/
public static final int getGradientColor( int startColor, int endColor, int permille ) {
int alpha = startColor >>> 24;
int red = (startColor >>> 16) & 0x00FF;
int green = (startColor >>> 8) & 0x0000FF;
int blue = startColor & 0x000000FF;
int diffAlpha = (endColor >>> 24) - alpha;
int diffRed = ( (endColor >>> 16) & 0x00FF ) - red;
int diffGreen = ( (endColor >>> 8) & 0x0000FF ) - green;
int diffBlue = ( endColor & 0x000000FF ) - blue;
alpha += (diffAlpha * permille) / 1000;
red += (diffRed * permille) / 1000;
green += (diffGreen * permille) / 1000;
blue += (diffBlue * permille) / 1000;
return ( alpha << 24 )
| ( red << 16 )
| ( green << 8 )
| ( blue );
// return (( alpha << 24) & 0xFF000000)
// | (( red << 16) & 0x00FF0000)
// | ( (green << 8) & 0x0000FF00)
// | ( blue );
}
/**
* Retrieves the gradient color between the given start and end colors.
* This method returns getGradientColor(startColor, endColor, (step * 1000)/numberOfSteps);
*
* @param startColor the start color
* @param endColor the end color
* @param step the step/position within the gradient
* @param numberOfSteps the maxium step (=100%)
* @return the gradient color
* @see #getGradientColor(int, int, int)
*/
public static final int getGradientColor( int startColor, int endColor, int step, int numberOfSteps ) {
int permille = (step * 1000) / numberOfSteps;
return getGradientColor(startColor, endColor, permille);
}
/**
* Retrieves the complementary color to the specified one.
*
* @param color the original argb color
* @return the complementary color with the same alpha value
*/
public static int getComplementaryColor( int color ) {
return ( 0xFF000000 & color )
| ((255 - (( 0x00FF0000 & color ) >> 16)) << 16)
| ((255 - (( 0x0000FF00 & color ) >> 8)) << 8)
| (255 - ( 0x000000FF & color ) );
}
/**
* <p>Paints a dropshadow behind a given ARGB-Array, whereas you are able to specify
* the shadows inner and outer color.</p>
* <p>Note that the dropshadow just works for fully opaque pixels and that it needs
* a transparent margin to draw the shadow.
* </p>
* <p>Choosing the same inner and outer color and varying the transparency is recommended.
* Dropshadow just works for fully opaque pixels.</p>
*
* @param argbData the images ARGB-Array
* @param width the width of the ARGB-Array
* @param height the width of the ARGB-Array
* @param xOffset use this for finetuning the shadow's horizontal position. Negative values move the shadow to the left.
* @param yOffset use this for finetuning the shadow's vertical position. Negative values move the shadow to the top.
* @param size use this for finetuning the shadows radius.
* @param innerColor the inner color of the shadow, which should be less opaque than the text.
* @param outerColor the outer color of the shadow, which should be less than opaque the inner color.
*
*/
public final static void dropShadow(int[] argbData, int width, int height,int xOffset, int yOffset, int size, int innerColor, int outerColor){
// additional Margin for the image because of the shadow
int iLeft = size-xOffset<0 ? 0 : size-xOffset;
int iRight = size+xOffset<0 ? 0 : size+xOffset;
int iTop = size-yOffset<0 ? 0 : size-yOffset;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -