From c9cca2ea38fd301c4383d14178589c09520bea22 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Mon, 9 Jul 2018 15:14:18 +0300 Subject: [PATCH] Code refactoring. --- .../sixth/e3d/gui/RenderingContext.java | 21 ++++++----- .../eu/svjatoslav/sixth/e3d/gui/View.java | 11 +++--- .../shapes/basic/ForwardOrientedTexture.java | 4 +-- .../raster/shapes/basic/line/Line.java | 36 +++++++++---------- .../basic/solidpolygon/SolidPolygon.java | 8 ++--- .../texturedpolygon/TexturedPolygon.java | 2 +- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java index 83dcae8..5e74a94 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java @@ -21,11 +21,11 @@ public class RenderingContext { public static final int bufferedImageType = BufferedImage.TYPE_4BYTE_ABGR; - public final BufferedImage bufferedImage; + final BufferedImage bufferedImage; public final Graphics2D graphics; - public final byte[] bytes; + public final byte[] pixels; public final int width; public final int height; @@ -44,13 +44,17 @@ public class RenderingContext { */ public boolean doRender = true; // TODO: make use of the variable + /** + * Mouse click. During rendering we can detect which item user clicked on. + */ public MouseClick mouseClick; + /** + * Item that user clicked on. + */ public MouseInteractionController clickedItem; - public RenderingContext(final int width, final int height, - final RenderingContext oldContext) { - + public RenderingContext(final int width, final int height) { this.width = width; this.height = height; @@ -63,7 +67,7 @@ public class RenderingContext { final WritableRaster raster = bufferedImage.getRaster(); final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer(); - bytes = dbi.getData(); + pixels = dbi.getData(); graphics = (Graphics2D) bufferedImage.getGraphics(); @@ -72,11 +76,6 @@ public class RenderingContext { graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - - if (oldContext != null) { - mouseClick = oldContext.mouseClick; - clickedItem = oldContext.clickedItem; - } } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/View.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/View.java index 43d19c4..bd60473 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/View.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/View.java @@ -31,7 +31,7 @@ public class View extends JPanel implements ComponentListener { long lastUpdateMillis = 0; private Timer canvasUpdateTimer; private ViewUpdateTimerTask canvasUpdateTimerTask; - private RenderingContext renderingContext = new RenderingContext(1, 1, null); + private RenderingContext renderingContext = null; private MouseInteractionController currentMouseOverComponent; /** * Currently target FPS for this view. It might change at runtime. @@ -153,8 +153,7 @@ public class View extends JPanel implements ComponentListener { if ((renderingContext == null) || (renderingContext.width != getWidth()) || (renderingContext.height != getHeight())) - renderingContext = new RenderingContext(getWidth(), getHeight(), - renderingContext); + renderingContext = new RenderingContext(getWidth(), getHeight()); // clear drawing area { @@ -219,8 +218,10 @@ public class View extends JPanel implements ComponentListener { * graphics is needed. */ public void updateView() { - renderingContext.mouseClick = null; - renderingContext.clickedItem = null; + if (renderingContext != null){ + renderingContext.mouseClick = null; + renderingContext.clickedItem = null; + } // compute time passed since last view update final long currentTime = System.currentTimeMillis(); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java index 474f05c..4d2bb1f 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java @@ -18,7 +18,7 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.texture.TextureBitmap; public class ForwardOrientedTexture extends AbstractCoordinateShape { - public static final double SIZE_MULTIPLIER = 0.005; + private static final double SIZE_MULTIPLIER = 0.005; public Texture texture; private double scale; @@ -78,7 +78,7 @@ public class ForwardOrientedTexture extends AbstractCoordinateShape { if (xEnd > targetRenderingArea.width) xEnd = targetRenderingArea.width; - final byte[] targetRenderingAreaBytes = targetRenderingArea.bytes; + final byte[] targetRenderingAreaBytes = targetRenderingArea.pixels; final int textureWidth = textureBitmap.width; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java index d5cb35c..b06699d 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/line/Line.java @@ -28,8 +28,8 @@ public class Line extends AbstractCoordinateShape { public Line(final Line parentLine) { this(parentLine.coordinates[0].coordinate.clone(), - parentLine.coordinates[1].coordinate.clone(), new Color( - parentLine.color), parentLine.width); + parentLine.coordinates[1].coordinate.clone(), + new Color(parentLine.color), parentLine.width); } public Line(final Point3D point1, final Point3D point2, final Color color, @@ -45,9 +45,9 @@ public class Line extends AbstractCoordinateShape { } - public void drawHorizontalLine(final LineInterpolator line1, - final LineInterpolator line2, final int y, - final RenderingContext renderBuffer) { + private void drawHorizontalLine(final LineInterpolator line1, + final LineInterpolator line2, final int y, + final RenderingContext renderBuffer) { int x1 = line1.getX(y); int x2 = line2.getX(y); @@ -79,7 +79,7 @@ public class Line extends AbstractCoordinateShape { final int drawnWidth = x2 - x1; int offset = ((y * renderBuffer.width) + x1) * 4; - final byte[] offSreenBufferBytes = renderBuffer.bytes; + final byte[] offSreenBufferBytes = renderBuffer.pixels; final int lineAlpha = color.a; @@ -108,8 +108,8 @@ public class Line extends AbstractCoordinateShape { } - public void drawSinglePixelHorizontalLine(final RenderingContext buffer, - final int alpha) { + private void drawSinglePixelHorizontalLine(final RenderingContext buffer, + final int alpha) { final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate; final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate; @@ -135,7 +135,7 @@ public class Line extends AbstractCoordinateShape { if (lineWidth == 0) return; - final byte[] offSreenBufferBytes = buffer.bytes; + final byte[] offSreenBufferBytes = buffer.pixels; final int backgroundAlpha = 255 - alpha; final int blueWithAlpha = color.b * alpha; @@ -164,8 +164,8 @@ public class Line extends AbstractCoordinateShape { } - public void drawSinglePixelVerticalLine(final RenderingContext buffer, - final int alpha) { + private void drawSinglePixelVerticalLine(final RenderingContext buffer, + final int alpha) { final Point2D onScreenPoint1 = coordinates[0].onScreenCoordinate; final Point2D onScreenPoint2 = coordinates[1].onScreenCoordinate; @@ -191,11 +191,11 @@ public class Line extends AbstractCoordinateShape { if (lineHeight == 0) return; - final byte[] offSreenBufferBytes = buffer.bytes; + final byte[] offScreenBufferBytes = buffer.pixels; final int backgroundAlpha = 255 - alpha; final int blueWithAlpha = color.b * alpha; - final int greenWithAplha = color.g * alpha; + final int greenWithAlpha = color.g * alpha; final int redWithAlpha = color.r * alpha; for (int relativeY = 0; relativeY <= lineHeight; relativeY++) { @@ -207,19 +207,19 @@ public class Line extends AbstractCoordinateShape { if ((x >= 0) && (x < buffer.width)) { int ramOffset = ((y * buffer.width) + x) * 4; - offSreenBufferBytes[ramOffset] = (byte) 255; + offScreenBufferBytes[ramOffset] = (byte) 255; ramOffset++; - offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256); + offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + blueWithAlpha) / 256); ramOffset++; - offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + greenWithAplha) / 256); + offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + greenWithAlpha) / 256); ramOffset++; - offSreenBufferBytes[ramOffset] = (byte) ((((offSreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256); + offScreenBufferBytes[ramOffset] = (byte) ((((offScreenBufferBytes[ramOffset] & 0xff) * backgroundAlpha) + redWithAlpha) / 256); } } } } - public int getLineInterpolator(final int startPointer, final int y) { + private int getLineInterpolator(final int startPointer, final int y) { for (int i = startPointer; i < li.length; i++) if (li[i].containsY(y)) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java index 992a406..00ae317 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/solidpolygon/SolidPolygon.java @@ -19,9 +19,9 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape; public class SolidPolygon extends AbstractCoordinateShape { - final static LineInterpolator polygonBoundary1 = new LineInterpolator(); - final static LineInterpolator polygonBoundary2 = new LineInterpolator(); - final static LineInterpolator polygonBoundary3 = new LineInterpolator(); + private final static LineInterpolator polygonBoundary1 = new LineInterpolator(); + private final static LineInterpolator polygonBoundary2 = new LineInterpolator(); + private final static LineInterpolator polygonBoundary3 = new LineInterpolator(); private Color color; public SolidPolygon(final Point3D point1, final Point3D point2, @@ -52,7 +52,7 @@ public class SolidPolygon extends AbstractCoordinateShape { final int width = x2 - x1; int offset = ((y * renderBuffer.width) + x1) * 4; - final byte[] offSreenBufferBytes = renderBuffer.bytes; + final byte[] offSreenBufferBytes = renderBuffer.pixels; final int polygonAlpha = color.a; final int b = color.b; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java index e4e30fc..686244c 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java @@ -105,7 +105,7 @@ public class TexturedPolygon extends AbstractCoordinateShape { x2 = renderBuffer.width - 1; int renderBufferOffset = ((y * renderBuffer.width) + x1) * 4; - final byte[] renderBufferBytes = renderBuffer.bytes; + final byte[] renderBufferBytes = renderBuffer.pixels; final double twidth = tx2 - tx1; final double theight = ty2 - ty1; -- 2.20.1