Skip to content

Instantly share code, notes, and snippets.

@kakajika
Created January 7, 2019 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakajika/1cb63119015ed4a7c684daf52e10a1e8 to your computer and use it in GitHub Desktop.
Save kakajika/1cb63119015ed4a7c684daf52e10a1e8 to your computer and use it in GitHub Desktop.
class Dot {
final Paint paint = new Paint(); {
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
}
final RectF rect = new RectF();
}
class DotPict {
private static final int ROWS = 16;
private static final int COLUMNS = 20;
private final Dot[][] dots = new Dot[ROWS][COLUMNS]; {
for (int row = 0; row < ROWS; ++row) {
for (int column = 0; column < COLUMNS; ++column) {
dots[row][column] = new Dot();
}
}
}
private final Paint framePaint = new Paint(); {
framePaint.setStyle(Paint.Style.STROKE);
framePaint.setStrokeWidth(2.0f);
framePaint.setColor(Color.LTGRAY);
}
void setSize(int width, int height) {
float xSpan = width / COLUMNS;
float ySpan = height / ROWS;
for (int row = 0; row < ROWS; ++row) {
for (int column = 0; column < COLUMNS; ++column) {
Dot dot = dots[row][column];
dot.rect.set(
xSpan * column,
ySpan * row,
xSpan * (column + 1),
ySpan * (row + 1)
);
}
}
}
void touch(PointF point) {
for (Dot[] rowDots : dots) {
for (Dot dot : rowDots) {
if (dot.rect.contains(point.x, point.y)) {
dot.paint.setColor(Color.BLACK);
}
}
}
}
void draw(Canvas canvas) {
for (Dot[] rowDots : dots) {
for (Dot dot : rowDots) {
canvas.drawRect(dot.rect, dot.paint); // 矩形の描画
canvas.drawRect(dot.rect, framePaint); // 枠線の描画
}
}
}
}
public class DotPictView extends View {
private final DotPict dotPict = new DotPict();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
dotPict.setSize(w, h);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
dotPict.draw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
dotPict.touch(new PointF(event.getX(), event.getY()));
invalidate();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment