S3D-Flood-It/src/Text3D.java
2018-11-15 00:06:29 +02:00

138 lines
3.8 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import com.s3d.Matrix3D;
import com.s3d.Mesh;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
* aNNiMON 2012
* For more info visit http://annimon.com/
*/
/**
* Создание и отрисовка 3D текста
* @author aNNiMON
*/
public class Text3D {
private Mesh[][] mesh;
/**
* Установить текст для отображения в виде 3D
* @param text текст
*/
public void setText(String text) {
Image img = getImageFromText(text, 0x00, 0xFFFFFF);
int w = img.getWidth();
int h = img.getHeight();
int[] pix = new int[w * h];
img.getRGB(pix, 0, w, 0, 0, w, h);
img = null;
createMeshes(pix, w, h);
}
/**
* Отрисовка 3D текста
* @param g контекст графики
* @param matrix матрица преобразований
*/
public void paint(Graphics g, Matrix3D matrix) {
for (int i = 0; i < mesh.length; i++) {
for (int j = 0; j < mesh[i].length; j++) {
if (mesh[i][j] == null) {
continue;
}
mesh[i][j].paint(g, matrix);
}
}
}
/**
* Создать набор моделей в виде текста
* @param pix массив цветов
* @param w ширина массива
* @param h высота
*/
private void createMeshes(int[] pix, int w, int h) {
mesh = new Mesh[h][w];
boolean[] medianColor = getMedian(pix);
for (int i = 0; i < h; i++) {
// mesh[i] = new Mesh[h];
for (int j = 0; j < w; j++) {
// Пропускаем фоновые пиксели
if (!medianColor[i * w + j]) {
continue;
}
int meshColor = Main.COLORS[Util.random(0, Main.MAX_COLORS)];
mesh[i][j] = new Box(Main.BOX_SIZE).getBox(meshColor);
int x = (j - w / 2) * Main.BOX_SIZE;
int y = (i - h / 2) * Main.BOX_SIZE;
mesh[i][j].move(x, -y, 0);
}
}
}
/**
* Получить картинку с текстом
* @param text текст
* @param bgcolor цвет фона
* @param textcolor цвет текста
* @return картинка с текстом
*/
private Image getImageFromText(String text, int bgcolor, int textcolor) {
Font smallFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
int width = smallFont.stringWidth(text);
int height = smallFont.getHeight();
Image out = Image.createImage(width, height);
Graphics g = out.getGraphics();
g.setColor(bgcolor);
g.fillRect(0, 0, width, height);
g.setFont(smallFont);
g.setColor(textcolor);
g.drawString(text, 0, 0, 20);
return out;
}
/**
* Получить черно-белую матрицу по усреднённому цвету
* @param pix массив цветов
* @return усреднённый цвет
*/
private boolean[] getMedian(int[] pix) {
float size = pix.length;
float mr = 0;
for (int i = 0; i < pix.length; i++) {
float r = ((pix[i] >> 16) & 0xFF);
r = r + ((pix[i] >> 8) & 0xFF);
r = r + (pix[i] & 0xFF);
r /= 3;
mr = mr + (r / size);
}
boolean[] out = new boolean[pix.length];
for (int i = 0; i < pix.length; i++) {
float r = ((pix[i] >> 16) & 0xFF);
r = r + ((pix[i] >> 8) & 0xFF);
r = r + (pix[i] & 0xFF);
r /= 3;
out[i] = (r > mr);
}
return out;
}
}