Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.astrect.graphics;
- import java.awt.geom.Ellipse2D;
- import org.astrect.notrefactored.Node_Sub;
- /**
- * Handles 2D graphic components drawing.
- */
- public class Rasterizer2D extends Node_Sub {
- public static Ellipse2D clipEllipse;
- public static int areaPixels[];
- public static int areaWidth;
- public static int areaHeight;
- public static int clipStartY;
- public static int clipEndY;
- public static int clipStartX;
- public static int clipEndX;
- public static int clipCenterX;
- public static int clipCenterY;
- /**
- * Clears the whole area that has been initialized to black.
- */
- public static void clearArea() {
- final int i = areaWidth * areaHeight;
- for (int j = 0; j < i; j++) {
- areaPixels[j] = 0;
- }
- }
- /**
- * Draws a horizontal line.
- */
- public static void drawHorizontalLine(int x, int y, int length, int color) {
- if (y < clipStartY || y >= clipEndY) {
- return;
- }
- if (x < clipStartX) {
- length -= clipStartX - x;
- x = clipStartX;
- }
- if (x + length > clipEndX) {
- length = clipEndX - x;
- }
- final int i1 = x + y * areaWidth;
- for (int j1 = 0; j1 < length; j1++) {
- areaPixels[i1 + j1] = getColorWithEllipseClipping(i1 + j1, color);
- }
- }
- /**
- * Draws a horizontal line with an alpha value support.
- */
- public static void drawHorizontalLine(int x, int y, int length, int color, int alpha) {
- if (y < clipStartY || y >= clipEndY) {
- return;
- }
- if (x < clipStartX) {
- length -= clipStartX - x;
- x = clipStartX;
- }
- if (x + length > clipEndX) {
- length = clipEndX - x;
- }
- final int j1 = 256 - alpha;
- final int k1 = (color >> 16 & 0xff) * alpha;
- final int l1 = (color >> 8 & 0xff) * alpha;
- final int i2 = (color & 0xff) * alpha;
- int i3 = x + y * areaWidth;
- for (int j3 = 0; j3 < length; j3++) {
- final int j2 = (areaPixels[i3] >> 16 & 0xff) * j1;
- final int k2 = (areaPixels[i3] >> 8 & 0xff) * j1;
- final int l2 = (areaPixels[i3] & 0xff) * j1;
- final int k3 = (k1 + j2 >> 8 << 16) + (l1 + k2 >> 8 << 8) + (i2 + l2 >> 8);
- areaPixels[i3] = getColorWithEllipseClipping(i3++, k3);
- }
- }
- /**
- * Draws a line from (x1, y1) to (x2, y2) with the specified color.
- */
- public static void drawLine(int x1, int y1, int x2, int y2, int color) {
- double xDifference = x1 - x2;
- if (xDifference < 0) {
- xDifference = -xDifference;
- }
- double yDifference = y1 - y2;
- if (yDifference < 0) {
- yDifference = -yDifference;
- }
- final double slope = yDifference / xDifference;
- if (xDifference >= yDifference) {
- for (int x = (int) xDifference; x >= 0; x--) {
- final int y = (int) (slope * x);
- if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
- || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
- continue;
- }
- try {
- final int xPos = x1 < x2 ? x : -x;
- final int yPos = y1 < y2 ? y : -y;
- areaPixels[x1 + xPos + (y1 + yPos) * areaWidth] = color;
- } catch (final Exception e) {
- }
- }
- } else {
- for (int y = (int) yDifference; y >= 0; y--) {
- final int x = (int) (y / slope);
- if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
- || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
- continue;
- }
- try {
- final int xPos = x1 < x2 ? x : -x;
- final int yPos = y1 < y2 ? y : -y;
- areaPixels[x1 + xPos + (y1 + yPos) * areaWidth] = color;
- } catch (final Exception e) {
- }
- }
- }
- }
- /**
- * Draws a line from (x1, y1) to (x2, y2) with the specified color and alpha
- * value.
- */
- public static void drawLine(int x1, int y1, int x2, int y2, int color, int alpha) {
- double xDifference = x1 - x2;
- if (xDifference < 0) {
- xDifference = -xDifference;
- }
- double yDifference = y1 - y2;
- if (yDifference < 0) {
- yDifference = -yDifference;
- }
- final double slope = yDifference / xDifference;
- final int alpha2 = 256 - alpha;
- final int red = (color >> 16 & 0xff) * alpha;
- final int green = (color >> 8 & 0xff) * alpha;
- final int blue = (color & 0xff) * alpha;
- if (xDifference >= yDifference) {
- for (int x = (int) xDifference; x >= 0; x--) {
- final int y = (int) (slope * x);
- if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
- || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
- continue;
- }
- try {
- final int xPos = x1 < x2 ? x : -x;
- final int yPos = y1 < y2 ? y : -y;
- final int pixel = x1 + xPos + (y1 + yPos) * areaWidth;
- final int j2 = (areaPixels[pixel] >> 16 & 0xff) * alpha2;
- final int k2 = (areaPixels[pixel] >> 8 & 0xff) * alpha2;
- final int l2 = (areaPixels[pixel] & 0xff) * alpha2;
- final int k3 = (red + j2 >> 8 << 16) + (green + k2 >> 8 << 8) + (blue + l2 >> 8);
- areaPixels[pixel] = k3;
- } catch (final Exception e) {
- }
- }
- } else {
- for (int y = (int) yDifference; y >= 0; y--) {
- final int x = (int) (y / slope);
- if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
- || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
- continue;
- }
- try {
- final int xPos = x1 < x2 ? x : -x;
- final int yPos = y1 < y2 ? y : -y;
- final int pixel = x1 + xPos + (y1 + yPos) * areaWidth;
- final int j2 = (areaPixels[pixel] >> 16 & 0xff) * alpha2;
- final int k2 = (areaPixels[pixel] >> 8 & 0xff) * alpha2;
- final int l2 = (areaPixels[pixel] & 0xff) * alpha2;
- final int k3 = (red + j2 >> 8 << 16) + (green + k2 >> 8 << 8) + (blue + l2 >> 8);
- areaPixels[pixel] = k3;
- } catch (final Exception e) {
- }
- }
- }
- }
- /**
- * Draws a non-filled rectangle.
- */
- public static void drawRectangle(int x, int y, int width, int height, int color) {
- drawHorizontalLine(x, y, width, color);
- drawHorizontalLine(x, y + height - 1, width, color);
- drawVerticalLine(x, y, height, color);
- drawVerticalLine(x + width - 1, y, height, color);
- }
- /**
- * Draws a non-filled rectangle with an alpha value support.
- */
- public static void drawRectangle(int x, int y, int width, int height, int color, int alpha) {
- drawHorizontalLine(x, y, width, color, alpha);
- drawHorizontalLine(x, y + height - 1, width, color, alpha);
- if (height > 2) {
- drawVerticalLine(x, y + 1, height - 2, color, alpha);
- drawVerticalLine(x + width - 1, y + 1, height - 2, color, alpha);
- }
- }
- /**
- * Draws a vertical line.
- */
- public static void drawVerticalLine(int x, int y, int length, int color) {
- if (x < clipStartX || x >= clipEndX) {
- return;
- }
- if (y < clipStartY) {
- length -= clipStartY - y;
- y = clipStartY;
- }
- if (y + length > clipEndY) {
- length = clipEndY - y;
- }
- final int j1 = x + y * areaWidth;
- for (int k1 = 0; k1 < length; k1++) {
- areaPixels[j1 + k1 * areaWidth] = getColorWithEllipseClipping(j1 + k1 * areaWidth, color);
- }
- }
- /**
- * Draws a vertical line with an alpha value support.
- */
- public static void drawVerticalLine(int x, int y, int length, int color, int alpha) {
- if (x < clipStartX || x >= clipEndX) {
- return;
- }
- if (y < clipStartY) {
- length -= clipStartY - y;
- y = clipStartY;
- }
- if (y + length > clipEndY) {
- length = clipEndY - y;
- }
- final int j1 = 256 - alpha;
- final int k1 = (color >> 16 & 0xff) * alpha;
- final int l1 = (color >> 8 & 0xff) * alpha;
- final int i2 = (color & 0xff) * alpha;
- int i3 = x + y * areaWidth;
- for (int j3 = 0; j3 < length; j3++) {
- final int j2 = (areaPixels[i3] >> 16 & 0xff) * j1;
- final int k2 = (areaPixels[i3] >> 8 & 0xff) * j1;
- final int l2 = (areaPixels[i3] & 0xff) * j1;
- final int k3 = (k1 + j2 >> 8 << 16) + (l1 + k2 >> 8 << 8) + (i2 + l2 >> 8);
- areaPixels[i3] = getColorWithEllipseClipping(i3, k3);
- i3 += areaWidth;
- }
- }
- /**
- * Draws a filled rectangle.
- */
- public static void fillRectangle(int x, int y, int width, int height, int color) {
- if (x < clipStartX) {
- width -= clipStartX - x;
- x = clipStartX;
- }
- if (y < clipStartY) {
- height -= clipStartY - y;
- y = clipStartY;
- }
- if (x + width > clipEndX) {
- width = clipEndX - x;
- }
- if (y + height > clipEndY) {
- height = clipEndY - y;
- }
- final int k1 = areaWidth - width;
- int l1 = x + y * areaWidth;
- for (int i2 = -height; i2 < 0; i2++) {
- for (int j2 = -width; j2 < 0; j2++) {
- areaPixels[l1] = getColorWithEllipseClipping(l1++, color);
- }
- l1 += k1;
- }
- }
- /**
- * Draws a filled rectangle with an alpha value support.
- */
- public static void fillRectangle(int x, int y, int width, int height, int color, int alpha) {
- if (x < clipStartX) {
- width -= clipStartX - x;
- x = clipStartX;
- }
- if (y < clipStartY) {
- height -= clipStartY - y;
- y = clipStartY;
- }
- if (x + width > clipEndX) {
- width = clipEndX - x;
- }
- if (y + height > clipEndY) {
- height = clipEndY - y;
- }
- final int l1 = 256 - alpha;
- final int i2 = (color >> 16 & 0xff) * alpha;
- final int j2 = (color >> 8 & 0xff) * alpha;
- final int k2 = (color & 0xff) * alpha;
- final int k3 = areaWidth - width;
- int l3 = x + y * areaWidth;
- for (int i4 = 0; i4 < height; i4++) {
- for (int j4 = -width; j4 < 0; j4++) {
- final int l2 = (areaPixels[l3] >> 16 & 0xff) * l1;
- final int i3 = (areaPixels[l3] >> 8 & 0xff) * l1;
- final int j3 = (areaPixels[l3] & 0xff) * l1;
- final int k4 = (i2 + l2 >> 8 << 16) + (j2 + i3 >> 8 << 8) + (k2 + j3 >> 8);
- areaPixels[l3] = getColorWithEllipseClipping(l3++, k4);
- }
- l3 += k3;
- }
- }
- /**
- * Checks if the pixel is inside clipping. Returns parameter color if true,
- * otherwise original color.
- */
- public static int getColorWithEllipseClipping(int px, int color) {
- if (clipEllipse == null) {
- return color;
- }
- final int x = px % areaWidth;
- final int y = (px - x) / areaWidth;
- if (outOfClip(x, y)) {
- return areaPixels[px];
- }
- return color;
- }
- /**
- * Initializes the maximum size to the drawing area.
- */
- public static void initializeDrawingArea(int height, int width, int pixels[]) {
- areaPixels = pixels;
- areaWidth = width;
- areaHeight = height;
- setClipRectangle(0, 0, width, height);
- }
- /**
- * Check if pixel is outside of clipped area. Returns true if pixel is
- * outside, false otherwise.
- */
- private static boolean outOfClip(int x, int y) {
- if (x < clipStartX || x > clipEndX || y < clipStartY || y > clipEndY) {
- return true;
- }
- if (clipEllipse != null && !clipEllipse.contains(x, y)) {
- return true;
- }
- return false;
- }
- /**
- * Sets the clipping to its defaults.
- */
- public static void removeClip() {
- setClipRectangle(0, 0, areaWidth, areaHeight);
- }
- /**
- * Sets clipping by a circle.
- */
- public static void setClipCircle(int x, int y, int diameter) {
- setClipRectangle(x, y, x + diameter, y + diameter);
- clipEllipse = new Ellipse2D.Double(x, y, diameter, diameter);
- }
- /**
- * Sets clipping by an ellipse.
- */
- public static void setClipEllipse(int x, int y, int diameterX, int diameterY) {
- setClipRectangle(x, y, x + diameterX, y + diameterY);
- clipEllipse = new Ellipse2D.Double(x, y, diameterX, diameterY);
- }
- /**
- * Sets clipping by a rectangle.
- */
- public static void setClipRectangle(int startX, int startY, int endX, int endY) {
- clipEllipse = null;
- if (startX < 0) {
- startX = 0;
- }
- if (startY < 0) {
- startY = 0;
- }
- if (endX > areaWidth) {
- endX = areaWidth;
- }
- if (endY > areaHeight) {
- endY = areaHeight;
- }
- clipStartX = startX;
- clipStartY = startY;
- clipEndX = endX;
- clipEndY = endY;
- clipCenterX = clipEndX / 2;
- clipCenterY = clipEndY / 2;
- }
- Rasterizer2D() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement