aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/2/6_pixel/core
diff options
context:
space:
mode:
Diffstat (limited to 'Занимательное программирование/2/6_pixel/core')
-rw-r--r--Занимательное программирование/2/6_pixel/core/build.gradle5
-rw-r--r--Занимательное программирование/2/6_pixel/core/src/net/caraus/pixelimage/PixelImage.java90
2 files changed, 95 insertions, 0 deletions
diff --git a/Занимательное программирование/2/6_pixel/core/build.gradle b/Занимательное программирование/2/6_pixel/core/build.gradle
new file mode 100644
index 0000000..c2fa637
--- /dev/null
+++ b/Занимательное программирование/2/6_pixel/core/build.gradle
@@ -0,0 +1,5 @@
+[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
+
+sourceSets.main.java.srcDirs = [ "src/" ]
+
+eclipse.project.name = appName + "-core"
diff --git a/Занимательное программирование/2/6_pixel/core/src/net/caraus/pixelimage/PixelImage.java b/Занимательное программирование/2/6_pixel/core/src/net/caraus/pixelimage/PixelImage.java
new file mode 100644
index 0000000..df49b3b
--- /dev/null
+++ b/Занимательное программирование/2/6_pixel/core/src/net/caraus/pixelimage/PixelImage.java
@@ -0,0 +1,90 @@
+package net.caraus.pixelimage;
+
+import java.util.random.RandomGenerator;
+
+import com.badlogic.gdx.ApplicationAdapter;
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.files.FileHandle;
+import com.badlogic.gdx.graphics.Pixmap;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.Pixmap.Format;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.math.RandomXS128;
+import com.badlogic.gdx.utils.ScreenUtils;
+
+public class PixelImage extends ApplicationAdapter {
+ private SpriteBatch batch;
+ private Texture img;
+ private Pixmap canvas;
+ private Pixmap etalon;
+ private int[][][] buffer;
+ private static final int X_OFFSET = 200;
+ private static final int Y_OFFSET = 100;
+ private int iterations = 0;
+
+ @Override
+ public void create () {
+ batch = new SpriteBatch();
+
+ FileHandle fileHandle = Gdx.files.internal("badlogic.jpg");
+ etalon = new Pixmap(fileHandle);
+
+ canvas = new Pixmap(etalon.getWidth() + X_OFFSET * 2, etalon.getHeight() + Y_OFFSET * 2, Format.RGB888);
+ buffer = new int[etalon.getWidth()][etalon.getHeight()][2];
+
+ for (int i = 0; i < buffer.length; ++i) {
+ for (int j = 0; j < buffer[i].length; ++j) {
+ buffer[i][j][0] = X_OFFSET + i;
+ buffer[i][j][1] = Y_OFFSET + j;
+ }
+ }
+ img = new Texture(canvas);
+ }
+
+ @Override
+ public void render () {
+ ScreenUtils.clear(1, 1, 1, 1);
+
+ canvas.setColor(1, 1, 1, 1);
+ canvas.fill();
+
+ // Draw the buffer.
+ for (int i = 0; i < buffer.length; ++i) {
+ for (int j = 0; j < buffer[i].length; ++j) {
+ int pixelColor = etalon.getPixel(i, j);
+
+ canvas.setColor(pixelColor);
+ canvas.drawPixel(buffer[i][j][0], buffer[i][j][1]);
+ }
+ }
+
+ if (iterations < 20) {
+ RandomGenerator randomGenerator = new RandomXS128();
+ // Change the pixel position.
+ for (int i = 0; i < buffer.length; ++i) {
+ for (int j = 0; j < buffer[i].length; ++j) {
+ int step = 5 * iterations + 1;
+ int moveX = randomGenerator.nextInt(step) - step / 2;
+ int moveY = randomGenerator.nextInt(step) - step / 2;
+
+ buffer[i][j][0] += moveX;
+ buffer[i][j][1] += moveY;
+ }
+ }
+ ++iterations;
+ }
+ img.draw(canvas, 0, 0);
+
+ batch.begin();
+ batch.draw(img, 0, 0);
+ batch.end();
+ }
+
+ @Override
+ public void dispose () {
+ batch.dispose();
+ img.dispose();
+ canvas.dispose();
+ etalon.dispose();
+ }
+}