Created
March 4, 2012 15:59
-
-
Save thomcc/1973654 to your computer and use it in GitHub Desktop.
old piece of 9 which never saw the light of day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.thomcc.nine.render; | |
import java.awt.Image; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.DataBufferInt; | |
import javax.swing.ImageIcon; | |
import javax.swing.JOptionPane; | |
public class Radar { | |
public final static int RADAR_BG_COLOR = 0xff002001; | |
public final static int RADAR_BG_COLOR_2 = 0xff001501; | |
public final static int RADAR_LINE_COLOR = 0xff003702; | |
public final static int RADAR_LINE_COLOR_2 = 0xff003001; | |
public final static int RADAR_OUTLINE_1 = 0xff000000; | |
public final static int RADAR_OUTLINE_2 = 0xff212121; | |
public final static int RADAR_SWEEP_COLOR = 0xff008607; | |
public final static int RADAR_SWEEP_COLOR_2 = 0xff007007; | |
public final int size; | |
public int xc, yc; | |
public final int[][] pixels; | |
public Radar(int size) { | |
this.size = size; | |
xc = size/2; | |
yc = size/2; | |
pixels = new int[size][size]; | |
initPixels(); | |
} | |
public void setPixel(int x, int y, int v) { | |
if (inBounds(x, y)) pixels[y][x] = v; | |
else System.out.format("setPixel: (%s, %s) is not in range!\n", x, y); | |
} | |
public void drawCircle(int x0, int y0, int radius, int value) { | |
int f = 1-radius; | |
int ddF_x = 1; | |
int ddF_y = -2*radius; | |
int x = 0; | |
int y = radius; | |
setPixel(x0, y0+radius, value); | |
setPixel(x0, y0-radius, value); | |
setPixel(x0+radius, y0, value); | |
setPixel(x0-radius, y0, value); | |
while (x < y) { | |
if (f >= 0) { | |
--y; | |
ddF_y += 2; | |
f += ddF_y; | |
} | |
++x; | |
ddF_x += 2; | |
f += ddF_x; | |
setPixel(x0 + x, y0 + y, value); | |
setPixel(x0 - x, y0 + y, value); | |
setPixel(x0 + x, y0 - y, value); | |
setPixel(x0 - x, y0 - y, value); | |
setPixel(x0 + y, y0 + x, value); | |
setPixel(x0 - y, y0 + x, value); | |
setPixel(x0 + y, y0 - x, value); | |
setPixel(x0 - y, y0 - x, value); | |
} | |
} | |
private void initPixels() { | |
} | |
private void doFill(int x, int y, int v, int comp) { | |
if (inBounds(x, y) && pixels[y][x] == comp) { | |
pixels[y][x] = v; | |
doFill(x+1, y, v, comp); | |
doFill(x-1, y, v, comp); | |
doFill(x, y-1, v, comp); | |
doFill(x, y+1, v, comp); | |
} | |
} | |
private void doFillPattern(int x, int y, int comp, int[][] pat, int w, int h) { | |
if (inBounds(x, y) && pixels[y][x] == comp) { | |
pixels[y][x] = pat[y % h][x % w]; | |
doFillPattern(x+1, y, comp, pat, w, h); | |
doFillPattern(x-1, y, comp, pat, w, h); | |
doFillPattern(x, y-1, comp, pat, w, h); | |
doFillPattern(x, y+1, comp, pat, w, h); | |
} | |
} | |
public void fill(int x, int y, int[][] pattern) { | |
doFillPattern(x, y, pixels[y][x], pattern, pattern[0].length, pattern.length); | |
} | |
private void fill(int x, int y, int val) { | |
int[][] pat = new int[1][1]; | |
pat[0][0] = val; | |
doFillPattern(x, y, pixels[y][x], pat, 1, 1); | |
} | |
private void fillCircle(int x0, int y0, int radius, int value) { | |
drawCircle(x0, y0, radius, value); | |
fill(x0, y0, value); | |
} | |
public boolean inBounds(int x, int y) { | |
return x >= 0 & y >= 0 && x < size && y < size; | |
} | |
public void drawLineFromCenter(double angle, int value) { | |
int c = size/2-2; | |
double sa = c * Math.sin(angle); | |
double ca = c * Math.cos(angle); | |
drawLine(size/2, size/2, (int)ca+size/2, (int)sa+size/2, value); | |
} | |
public void drawLine(int x0, int y0, int x1, int y1, int v) { | |
int dx = Math.abs(x1-x0); | |
int dy = Math.abs(y1-y0); | |
int sx = x0 < x1 ? 1 : -1; | |
int sy = y0 < y1 ? 1 : -1; | |
int err = dx-dy; | |
while (x0 != x1 && y0 != y1) { | |
setPixel(x0, y0, v); | |
int e2 = 2*err; | |
if (e2 > -dy) { | |
err -= dy; | |
x0 += sx; | |
} | |
if (e2 < dx) { | |
err += dx; | |
y0 += sy; | |
} | |
} | |
} | |
public static void main(String args[]) { | |
int sz = 60; | |
Radar r = new Radar(sz); | |
r.fillCircle(sz/2, sz/2, sz/2-1, 2); | |
r.drawCircle(sz/2, sz/2, sz/2-1, 1); | |
r.drawCircle(sz/2, sz/2, sz/2-3, 1); | |
int[][] pat = new int[sz/10][sz/10]; | |
for (int j = 0; j < sz/10; ++j) | |
for (int i = 0; i < sz/10; ++i) | |
pat[j][i] = (i == sz/10-1 || j == sz/10-1) ? 4 : 3; | |
r.fill(sz/2, sz/2, pat); | |
for (int i = -10; i < 10; ++i) { | |
r.drawLineFromCenter(((double)i)*Math.PI/10d, 5); | |
} | |
//r.drawLineFromCenter(1.0, 5); | |
int[][] pix = r.pixels; | |
BufferedImage img = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_ARGB); | |
int[] ipix = ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); | |
for (int y = 0; y < sz; ++y) { | |
for (int x = 0; x < sz; ++x) { | |
int v; | |
switch (pix[y][x]) { | |
case 0: v = 0;break; | |
case 1: v = RADAR_OUTLINE_1;break; | |
case 2: v = RADAR_OUTLINE_2;break; | |
case 3: v = (y % 2 == 0) ? RADAR_BG_COLOR : RADAR_BG_COLOR_2;break; | |
case 4: v = (y % 2 == 0) ? RADAR_LINE_COLOR : RADAR_LINE_COLOR_2;break; | |
case 5: v = (y % 2 == 0) ? RADAR_SWEEP_COLOR : RADAR_SWEEP_COLOR_2;break; | |
default: v = 0xffff00ff;break; | |
} | |
ipix[x+y*sz] = v; | |
} | |
} | |
JOptionPane.showMessageDialog( | |
null, | |
null, | |
"radar will look like this more or less", | |
JOptionPane.YES_NO_OPTION, | |
new ImageIcon(img.getScaledInstance(img.getWidth()*2, | |
img.getHeight()*2, Image.SCALE_AREA_AVERAGING))); | |
for (int y = 0; y < sz; ++y) { | |
System.out.println(); | |
for (int x = 0; x < sz; ++x) { | |
char v = '&'; | |
switch (pix[y][x]) { | |
case 0: v = ' '; break; | |
case 1: v = '#'; break; | |
case 2: v = '&'; break; | |
case 3: v = '.'; break; | |
case 4: v = '+'; break; | |
} | |
System.out.print(v); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment