Reputation: 313
Hi I'm doing a practice project which could draw a line with the coordinates I put. The screen only have two textfields and one button. For example if I put "20" and "30" in those two textfields and click the "draw" button, I want the app to draw a line from (0,0) to (20,30) in ANOTHER VIEW. The problem is when I click the button, the values in those two textfields are passed into the setCoordinates() function, but the view just doesn't show the line. I tried change coordinates[0] and coordinates[1] to 50 and 50 in the canvas.drawLine() function then the line shows up, so I guess may be the invalidate() function is not working? Anyway please help find where my problem is, thanks!!!!! Here is my code:
MazeBuilder.java
package com.example.maze;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.app.Activity;
import android.graphics.Color;
public class MazeBuilder extends Activity {
private DrawMaze drawMaze ;
private EditText editTextX;
private EditText editTextY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawMaze = new DrawMaze(this);
setContentView(R.layout.activity_maze_builder);
editTextX = (EditText) findViewById(R.id.editText1);
editTextY = (EditText) findViewById(R.id.editText2);
}
public void buildMaze(View view){
final int x = getValue(editTextX);
final int y = getValue(editTextY);
drawMaze.setCoordinates(x, y);
}
private static int getValue (EditText text) {
try {
return Integer.parseInt(text.getText().toString());
} catch (NumberFormatException ex) {
return 0;
}
}
}
DrawMaze.java
package com.example.maze;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawMaze extends View{
private Paint paint = new Paint();
private int[] coordinates = new int[2];
//===============================Constructors==============================================
//
//
public DrawMaze(Context context) {
super( context );
}
public DrawMaze(Context context, AttributeSet attrs) {
super( context, attrs );
}
public DrawMaze(Context context, AttributeSet attrs, int defStyle) {
super( context, attrs, defStyle );
}
//===============================Initialize the color of line and background==========================================
//
//
public void init(){
paint.setColor(Color.BLACK);
this.setBackgroundColor(Color.WHITE);
}
public void setCoordinates(int x, int y){
coordinates[0] = x;
coordinates[1] = y;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
System.out.println("=============coordinates[0]:"+coordinates[0]+"================coordinates[1]:"+coordinates[1]+"========");
canvas.drawLine(0, 0, coordinates[0] , coordinates[1] , paint);
}
}
activity_maze_builder.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MazeBuilder" >
<com.example.maze.DrawMaze
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout1" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="500dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_centerHorizontal="true"
android:text="Columns: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="buildMaze"
android:text="Build" />
<EditText
android:id="@+id/editText1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_toLeftOf="@+id/editText1"
android:text="Rows: "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Upvotes: 3
Views: 6998
Reputation: 7104
instead of
drawMaze = new DrawMaze(this);
use
drawMaze = (DrawMaze)findViewById(R.id.relativeLayout2);
but under
setContentView()
the reason is that when you set the content view with your layout, it is using the DrawMaze from your layout and not the
drawMaze = new DrawMaze(this);
which was instantiated but never set into the content view
Upvotes: 2