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 java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.UUID; 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.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.PointF; import android.os.Parcel; import android.os.Parcelable; public class PaperPiecedBlock implements Parcelable { // private static final String TAG = "PaperPiecedBlock"; // // Fields // private boolean m_dirty; private float m_width; private float m_height; private int m_backgroundColor; private ArrayList<PaperPiecedBlockPiece> m_pieces; private Paint m_backgroundPaint; private Paint m_backgroundPaintWhite; // // Constructors // public PaperPiecedBlock(float width, float height, int backgroundColor) { if (width < 0) throw new InvalidParameterException("Invalid width " + width); if (height < 0) throw new InvalidParameterException("Invalid height " + height); m_dirty = false; m_width = width; m_height = height; m_backgroundColor = backgroundColor; m_pieces = new ArrayList<PaperPiecedBlockPiece>(); m_backgroundPaint = new Paint(); m_backgroundPaint.setStyle(Style.FILL); m_backgroundPaint.setColor(m_backgroundColor); m_backgroundPaintWhite = new Paint(); m_backgroundPaintWhite.setStyle(Style.FILL); m_backgroundPaintWhite.setColor(Color.WHITE); } private PaperPiecedBlock(Parcel in) { m_dirty = in.readInt() == 0 ? false : true; m_width = in.readFloat(); m_height = in.readFloat(); m_backgroundColor = in.readInt(); m_pieces = new ArrayList<PaperPiecedBlockPiece>(); in.readTypedList(m_pieces, PaperPiecedBlockPiece.CREATOR); m_backgroundPaint = new Paint(); m_backgroundPaint.setStyle(Style.FILL); m_backgroundPaint.setColor(m_backgroundColor); m_backgroundPaintWhite = new Paint(); m_backgroundPaintWhite.setStyle(Style.FILL); m_backgroundPaintWhite.setColor(Color.WHITE); } // // Public // public boolean isDirty() { return m_dirty; } public void clearDirty() { m_dirty = false; } public void touch() { m_dirty = true; } public boolean isEmpty() { for (PaperPiecedBlockPiece piece : m_pieces) { if (piece != null) return false; } return true; } 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 int getBackgroundColor() { return m_backgroundColor; } public void setBackgroundColor(int backgroundColor) { m_backgroundColor = backgroundColor; m_backgroundPaint.setColor(m_backgroundColor); } public List<PaperPiecedBlockPiece> getPieces() { return m_pieces; } public void sort(HashMap<UUID, Integer> pieceIndexes) { final HashMap<UUID, Integer> pieceIndexesCaptured = pieceIndexes; Collections.sort(m_pieces, new Comparator<PaperPiecedBlockPiece>() { @Override public int compare(PaperPiecedBlockPiece lhs, PaperPiecedBlockPiece rhs) { Integer lhsValue = pieceIndexesCaptured.get(lhs.getId()); Integer rhsValue = pieceIndexesCaptured.get(rhs.getId()); if (lhsValue == null) lhsValue = 0; if (rhsValue == null) rhsValue = 0; return lhsValue.compareTo(rhsValue); } }); } public void draw(Bitmap bitmap, RenderOptions renderOptions) { Canvas canvas = new Canvas(bitmap); draw(canvas, renderOptions); } public Theme createTheme(int swatchCount) { Theme theme = new Theme(swatchCount); // Add swatch for background color. // { Swatch swatch = theme.getSwatch(0); if (swatch != null) { swatch.setColor(getBackgroundColor()); } } // Add swatches for piece colors. // for (PaperPiecedBlockPiece piece : m_pieces) { int color = piece.getColor(); if (theme.getSwatch(color) == null) { Swatch swatch = theme.getSwatch(0); if (swatch != null) { swatch.setColor(color); } } } return theme; } // // Package-Private // PaperPiecedBlockPiece find(int width, int height, PointF point) { for (int idx = m_pieces.size() - 1; idx >= 0; --idx) { PaperPiecedBlockPiece piece = m_pieces.get(idx); if (piece.containsPoint(point, width, height)) { return piece; } } return null; } void draw(Canvas canvas, RenderOptions renderOptions) { Paint paint = renderOptions.getRenderStyle() == RenderStyles.Color ? m_backgroundPaint : m_backgroundPaintWhite; canvas.drawRect(renderOptions.getLeft(), renderOptions.getTop(), renderOptions.getRight(), renderOptions.getBottom(), paint); for (PaperPiecedBlockPiece piece : getPieces()) { piece.draw(canvas, renderOptions); } } void flipHorizontal() { for (PaperPiecedBlockPiece piece : m_pieces) { piece.flipHorizontal(); } } void flipVertical() { for (PaperPiecedBlockPiece piece : m_pieces) { piece.flipVertical(); } } void rotateClockwise() { for (PaperPiecedBlockPiece piece : m_pieces) { piece.rotateClockwise(); } } void rotateCounterclockwise() { for (PaperPiecedBlockPiece piece : m_pieces) { piece.rotateCounterclockwise(); } } // // JSON // JSONObject createJSONObject() throws JSONException { JSONArray jsonPieces = new JSONArray(); for (PaperPiecedBlockPiece piece : m_pieces) { jsonPieces.put(piece.createJSONObject()); } JSONObject jsonBlock = new JSONObject(); jsonBlock.put("objectType", "block"); jsonBlock.put("pieces", jsonPieces); jsonBlock.put("width", m_width); jsonBlock.put("height", m_height); jsonBlock.put("backgroundColor", m_backgroundColor); return jsonBlock; } static final PaperPiecedBlock createFromJSONObject(JSONObject jsonObject) throws JSONException { float width = (float) jsonObject.optDouble("width", 0); float height = (float) jsonObject.optDouble("height", 0); int backgroundColor = jsonObject.optInt("backgroundColor", Color.WHITE); PaperPiecedBlock block = new PaperPiecedBlock(width, height, backgroundColor); JSONArray jsonPieces = jsonObject.getJSONArray("pieces"); for (int idx = 0; idx < jsonPieces.length(); ++idx) { JSONObject jsonPiece = jsonPieces.getJSONObject(idx); block.m_pieces.add(PaperPiecedBlockPiece.createFromJSONObject(jsonPiece)); } return block; } // // Parcelable // @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel out, int flags) { out.writeInt(m_dirty ? -1 : 0); out.writeFloat(m_width); out.writeFloat(m_height); out.writeInt(m_backgroundColor); out.writeTypedList(m_pieces); } public static final Parcelable.Creator<PaperPiecedBlock> CREATOR = new Creator<PaperPiecedBlock>() { @Override public PaperPiecedBlock[] newArray(int size) { return new PaperPiecedBlock[size]; } @Override public PaperPiecedBlock createFromParcel(Parcel in) { return new PaperPiecedBlock(in); } }; }