Java tutorial
/* Copyright (c) 2013 Richard G. Todd. * Licensed under the terms of the GNU General Public License (GPL) Version 3.0. */ package com.richtodd.android.quiltdesign.block; import java.security.InvalidParameterException; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.os.Parcel; import android.os.Parcelable; public class Quilt implements Parcelable { // private static final String TAG = "Quilt"; // // Fields // private boolean m_new; private int m_rowCount; private int m_columnCount; private float m_width; private float m_height; private ArrayList<ArrayList<QuiltBlock>> m_rows; // // Constructors // public Quilt(int rowCount, int columnCount, float width, float height) { if (rowCount < 0) throw new InvalidParameterException("Invalid rowCount " + rowCount); if (columnCount < 0) throw new InvalidParameterException("Invalid columnCount " + columnCount); if (width < 0) throw new InvalidParameterException("Invalid width " + width); if (height < 0) throw new InvalidParameterException("Invalid height " + height); m_new = true; m_rowCount = rowCount; m_columnCount = columnCount; m_width = width; m_height = height; m_rows = new ArrayList<ArrayList<QuiltBlock>>(); } public Quilt(Parcel in) { m_new = in.readInt() == 0 ? false : true; m_rowCount = in.readInt(); m_columnCount = in.readInt(); m_width = in.readFloat(); m_height = in.readFloat(); m_rows = new ArrayList<ArrayList<QuiltBlock>>(); for (int row = 0; row < m_rowCount; ++row) { for (int column = 0; column < m_columnCount; ++column) { QuiltBlock quiltBlock = (QuiltBlock) in.readParcelable(QuiltBlock.class.getClassLoader()); setQuiltBlock(row, column, quiltBlock); } } } // // Public // public boolean isNew() { return m_new; } public boolean isEmpty() { for (ArrayList<QuiltBlock> row : m_rows) { for (QuiltBlock block : row) { if (block != null) return false; } } return true; } public int getRowCount() { return m_rowCount; } public void setRowCount(int rowCount) { if (rowCount < 0) throw new InvalidParameterException("Invalid rowCount " + rowCount); m_rowCount = rowCount; } public int getColumnCount() { return m_columnCount; } public void setColumnCount(int columnCount) { if (columnCount < 0) throw new InvalidParameterException("Invalid columnCount " + columnCount); m_columnCount = columnCount; } public float getWidth() { return m_width; } public void setWidth(float width) { if (width < 0) throw new InvalidParameterException("Invalid width " + width); m_width = width; } public float getHeight() { return m_height; } public void setHeight(float height) { if (height < 0) throw new InvalidParameterException("Invalid height " + height); m_height = height; } public QuiltBlock getQuiltBlock(int row, int column) { if (m_rows.size() < row + 1) { return null; } ArrayList<QuiltBlock> blocks = m_rows.get(row); if (blocks.size() < column + 1) { return null; } return blocks.get(column); } public void setQuiltBlock(int row, int column, QuiltBlock quiltBlock) { expand(row, column); m_rows.get(row).set(column, quiltBlock); } public void draw(Bitmap bitmap, RenderOptions renderOptions) { Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.BLACK); draw(canvas, renderOptions); } // // Package-Private // void draw(Canvas canvas, RenderOptions renderOptions) { for (int row = 0; row < getRowCount(); ++row) { for (int column = 0; column < getColumnCount(); ++column) { draw(canvas, row, column, renderOptions); } } } // // JSON // JSONObject createJSONObject() throws JSONException { JSONArray jsonQuiltBlocks = new JSONArray(); for (int row = 0; row < m_rowCount; ++row) { for (int column = 0; column < m_columnCount; ++column) { QuiltBlock quiltBlock = getQuiltBlock(row, column); if (quiltBlock != null) { jsonQuiltBlocks.put(quiltBlock.createJSONObject()); } else { jsonQuiltBlocks.put(JSONObject.NULL); } } } JSONObject jsonQuilt = new JSONObject(); jsonQuilt.put("objectType", "quilt"); jsonQuilt.put("rowCount", m_rowCount); jsonQuilt.put("columnCount", m_columnCount); jsonQuilt.put("width", m_width); jsonQuilt.put("height", m_height); jsonQuilt.put("quiltBlocks", jsonQuiltBlocks); return jsonQuilt; } static final Quilt createFromJSONObject(JSONObject jsonObject) throws JSONException { int rowCount = jsonObject.optInt("rowCount", 0); int columnCount = jsonObject.optInt("columnCount", 0); float width = (float) jsonObject.optDouble("width", 0); float height = (float) jsonObject.optDouble("height", 0); Quilt quilt = new Quilt(rowCount, columnCount, width, height); if (jsonObject.has("quiltBlocks")) { JSONArray jsonQuiltBlocks = jsonObject.getJSONArray("quiltBlocks"); int index = -1; for (int row = 0; row < quilt.m_rowCount; ++row) { for (int column = 0; column < quilt.m_columnCount; ++column) { index += 1; if (index < jsonQuiltBlocks.length()) { JSONObject jsonQuiltBlock = jsonQuiltBlocks.optJSONObject(index); if (jsonQuiltBlock != null) { QuiltBlock quiltBlock = QuiltBlock.createFromJSONObject(jsonQuiltBlock); quilt.setQuiltBlock(row, column, quiltBlock); } } } } } quilt.m_new = false; return quilt; } // // Parcelable // @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel out, int flags) { out.writeInt(m_new ? -1 : 0); out.writeInt(m_rowCount); out.writeInt(m_columnCount); out.writeFloat(m_width); out.writeFloat(m_height); for (int row = 0; row < m_rowCount; ++row) { for (int column = 0; column < m_columnCount; ++column) { QuiltBlock quiltBlock = getQuiltBlock(row, column); out.writeParcelable(quiltBlock, 0); } } } public static final Parcelable.Creator<Quilt> CREATOR = new Creator<Quilt>() { @Override public Quilt[] newArray(int size) { return new Quilt[size]; } @Override public Quilt createFromParcel(Parcel in) { return new Quilt(in); } }; // // Private // private void draw(Canvas canvas, int row, int column, RenderOptions renderOptions) { int blockWidth = renderOptions.getWidth() / getColumnCount(); int blockHeight = renderOptions.getHeight() / getRowCount(); int blockSize = Math.min(blockWidth, blockHeight); RenderOptions blockRenderOptions = new RenderOptions(renderOptions); blockRenderOptions.setWidth(blockSize); blockRenderOptions.setHeight(blockSize); QuiltBlock quiltBlock = getQuiltBlock(row, column); if (quiltBlock != null) { PaperPiecedBlock block = quiltBlock.getBlock(); if (block != null) { int blockLeft = renderOptions.getLeft() + (column * blockSize); int blockTop = renderOptions.getTop() + (row * blockSize); // canvas.drawRect(blockLeft, blockTop, blockLeft // + m_cachedBlockSize, blockTop + m_cachedBlockSize, // m_debugPaint); blockRenderOptions.setLeft(blockLeft); blockRenderOptions.setTop(blockTop); block.draw(canvas, blockRenderOptions); } } } private void expand(int row, int column) { while (m_rows.size() < row + 1) { m_rows.add(new ArrayList<QuiltBlock>()); } ArrayList<QuiltBlock> blocks = m_rows.get(row); while (blocks.size() < column + 1) { blocks.add(null); } } }