728x90
반응형
문제
- [직접풀어보기 9-2] 를 이전에 그린 도형이 계속 화면에 남아있도록 수정하시오.
결과 화면

XML 코드
얘는 별도의 xml코드가 없다.
JAVA 코드
package com.example.homework5;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
//메뉴 선택에서 모양 고르는 변수
final static int LINE = 1, CIRCLE = 2, RECTANGLE = 3;
static int curShape = LINE;
static int color = Color.BLACK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyGraphicView(this));
}
//메뉴 생성
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "선 그리기");
menu.add(0, 2, 0, "원 그리기");
menu.add(0, 3, 0, "사각형 그리기");
SubMenu subMenu = menu.addSubMenu("색상 변경 >> ");
subMenu.add(0, 4, 0, "빨강");
subMenu.add(0, 5, 0, "초록");
subMenu.add(0, 6, 0, "파랑");
return true;
}
//메뉴 클릭 시
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
curShape = LINE;
return true;
case 2:
curShape = CIRCLE;
return true;
case 3:
curShape = RECTANGLE;
return true;
case 4:
color = Color.RED;
return true;
case 5:
color = Color.GREEN;
return true;
case 6:
color = Color.BLUE;
return true;
}
return super.onOptionsItemSelected(item);
}
//그림판 만들기
private static class MyGraphicView extends View {
//Myshape라는 클래스를 만들어주고 도형이 남아있어야 되는 동적 리스트를 하나 만들어준다.
Myshape currentShape = null;
ArrayList<Myshape> MyshapeArrayList = new ArrayList<>();
public MyGraphicView(Context context) {
super(context);
}
//터치 이벤트 설정
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//위에서 정의한 Myshape객체인 currentShape 객체에 시작좌표, 끝좌표, 색깔을 지정해주고..
currentShape = new Myshape(curShape);
currentShape.color = color;
currentShape.startX = (int) event.getX();
currentShape.startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
currentShape.stopX = (int) event.getX();
currentShape.stopY = (int) event.getY();
this.invalidate();
break;
case MotionEvent.ACTION_UP:
currentShape.stopX = (int) event.getX();
currentShape.stopY = (int) event.getY();
//손을 뗄 때 동적 리스트에 더해준다.
MyshapeArrayList.add(currentShape);
currentShape = null;
this.invalidate();
break;
}
return true;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
//for each 문으로 MyshapeArrayList의 인덱스를 하나씩 호출해준다.
//그릴때마다 다시 그려준다고 생각하면 편함!
for (Myshape currentShape : MyshapeArrayList) {
paint.setColor(currentShape.color);
drawShape(currentShape, canvas, paint);
}
if (currentShape != null) {
drawShape(currentShape, canvas, paint);
}
}
//그림을 그리는 함수
private void drawShape(Myshape currentShape, Canvas canvas, Paint paint) {
switch (currentShape.shape) {
case LINE:
canvas.drawLine(currentShape.startX, currentShape.startY,
currentShape.stopX, currentShape.stopY, paint);
break;
case CIRCLE:
int radius = (int) Math.sqrt(Math.pow(currentShape.stopX - currentShape.startX, 2) +
Math.pow(currentShape.stopY - currentShape.startY, 2));
canvas.drawCircle(currentShape.startX, currentShape.startY, radius, paint);
break;
case RECTANGLE:
Rect rect = new Rect(currentShape.startX, currentShape.startY,
currentShape.stopX, currentShape.stopY);
canvas.drawRect(rect, paint);
break;
}
}
//그린 도형들을 저장해 줄 때 필요한 함수를 만들어줬다.
private static class Myshape {
int shape, startX, startY, stopX, stopY, color;
public Myshape(int shape) {
this.shape = shape;
}
}
}
}
결과

진짜 이문제 과제로 풀려고 너무오래동안 붙잡고있었다 개어려웠음 ㅠㅠㅠㅠㅠㅠ
728x90
반응형
'Android, IOS > Android' 카테고리의 다른 글
[에러] android.support와 androidx 에러날 때 (0) | 2019.12.14 |
---|---|
[Android] RecyclerView 기본 사용법 (0) | 2019.12.14 |
[에러] all com.android.support libraries must use the exact same version specification (0) | 2019.11.30 |
[Android Sutdio를 활용한 안드로이드 프로그래밍 개정 5판] 직접 풀어보기 8-2 및 응용 (0) | 2019.11.23 |
[Android Sutdio를 활용한 안드로이드 프로그래밍 개정 5판] 연습문제 7-6 (0) | 2019.11.09 |