package kr.co.testing;

import java.util.*;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class LayoutTesting extends Activity {
ArrayList<Shape> arShape = new ArrayList<Shape>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
MyView vw = new MyView(this);
setContentView(vw);
}
class Shape {
final static int RECT = 0;
final static int CIRCLE = 1;
final static int TRIANGLE = 2;
int color = 0;
int bgcolor = 0;
int what;
Rect rt;
}

protected class MyView extends View {
public MyView(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
for (int idx = 0; idx < arShape.size(); idx ++){
Paint Pnt = new Paint();
Rect rt = arShape.get(idx).rt;
if(arShape.get(idx).color != arShape.get(idx).bgcolor){
continue;
}
canvas.drawColor(arShape.get(idx).bgcolor); 
switch (arShape.get(idx).what) {
case Shape.RECT:
canvas.drawRect(rt, Pnt);
break;
case Shape.CIRCLE:
canvas.drawCircle(rt.left + rt.width()/2, rt.top + rt.height()/2,
rt.width()/2, Pnt);
break;
case Shape.TRIANGLE:
Path path = new Path();
path.moveTo(rt.left + rt.width()/2, rt.top);
path.lineTo(rt.left, rt.bottom);
path.lineTo(rt.right, rt.bottom);
canvas.drawPath(path, Pnt);
break;
}
}
}
void addShape(){
Random Rnd = new Random();
Rect rt = new Rect();
Shape shape = new Shape();
int Size = 32 + 16 * Rnd.nextInt(2);
rt.left = Rnd.nextInt(getWidth());
rt.top = Rnd.nextInt(getHeight());
rt.right = rt.left + Size;
rt.bottom = rt.top + Size;
shape.what=Rnd.nextInt(3);
switch (Rnd.nextInt(5)) {
case 0:
shape.color = Color.WHITE;
break;
case 1:
shape.color = Color.RED;
break;
case 2:
shape.color = Color.GREEN;
break;
case 3:
shape.color = Color.BLUE;
break;
case 4:
shape.color = Color.YELLOW;
break;
}
switch (Rnd.nextInt(5)) {
case 0:
shape.bgcolor = Color.WHITE;
break;
case 1:
shape.bgcolor = Color.RED;
break;
case 2:
shape.bgcolor = Color.GREEN;
break;
case 3:
shape.bgcolor = Color.BLUE;
break;
case 4:
shape.bgcolor = Color.YELLOW;
break;
}
shape.rt=rt;
arShape.add(shape);
}
}
}

xml 레이아웃 없이 로직상에서 view를 생성하고 화면에 띄우는 간단한 로직을 짜보았습니다. 에러는 안 뜨고 exception도 전혀 뜨지 않는데다 결과물도 뜨지 않습니다.(헛웃음이 나오네요)

간단하게 해쉬맵에다 사각형 삼각형 동그라미 받을 클래스 하나 선언해 놓고 addShape에서 도형의 크기와 위치를 셋팅하고 Shape에 저장하게 되면 onDraw에서 해쉬맵의 정보를 참조해다가 랜덤으로 배경색, 도형의 색, 모양을 하나씩 뿌리게 해놓았는데 NullPointExeption은 없는 걸 봐서는 요소는 제대로 적어 놓은 듯 하고 addShape도 값을 제대로 만들어 내는듯 한데 안되네요.

이거 전에 더 간단한 도형하나에 배경색 랜덤 도형색 랜덤까지는 됐는데 도형을 변결할라고 하니 콱 안되네요. 어디가 문제점인지 파악도 안되고...

그럼 부탁드리겠습니다.