//우선 클래스는 두개입니다. 처음에 EditText에 써서 클릭하면 그래프가 보이게 하는 건데 밑에 보시면
package com.android.mathgraph;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MathGraph extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText t1 = (EditText)findViewById(R.id.EditText01);
t1.setText("");
final float in;
in = Integer.parseInt(t1.getText().toString()); // <-- 여기서 저장
Button btn = (Button)findViewById(R.id.Button01);
btn.setText("create a graph!");
View DrawGraph = (View) findViewById(R.id.View01);
final DrawGraph tv = new DrawGraph(this, null);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv.input = in; // <-- 클릭하면서 집어넣음
setContentView(tv);
}
});
}
}
package com.android.mathgraph;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
class DrawGraph extends View {
float input; //<-- 미리 만들어논 변수
public DrawGraph(Context context, AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
}
/* public DrawGraph(Context context, int i) {
super(context);
}*/
public void onDraw(Canvas canvas) {
Paint p = new Paint(); // 그래프의 색
p.setColor(Color.WHITE);
Paint p1 = new Paint(); // 기준선의 색, X축, Y축
p1.setColor(Color.WHITE);
float startingX = 160.0f; // 원점 X좌표
float startingY = 240.0f; // 원점 Y좌표
canvas.drawLine(0, startingY, 320, startingY, p1);
canvas.drawLine(startingX, 0, startingX, 480, p1);
float x = -160f,y;
y = input*((float) ((float) 0.01*Math.pow(x, 2))); // <-- 여기서 쓰고 싶음;
float x1 = x, y1 = y;
for (; x <= 160.0f; x++)
{
y = input*((float) ((float) 0.01*Math.pow(x, 2)));
canvas.drawLine(x1 + startingX, -y1 + startingY, x + startingX, -y
+ startingY, p);
x1 = x;
y1 = y;
}
}
}
에뮬레이터에서 실행하면 뭐 영어로 뜨고 안되네요;
뭐가 문제 일까요? 저거 없이 하면 그래프는 잘 그려집니다.
근데 그 밑에서 공백을 받아와서 정수로 변환 합니다. 이부분에서 Exception이 발생합니다.
공백을 숫자로 변환하지는 못하겠지요^^