I am new to android and Java and I am having trouble using the Thread.sleep function.My program is a simple game that's almost like table tennis with two players. The code is not complete but the Thread is not working properly.I am changing the margins to move the ball.Is the catch block being run because of some exception > My code is as follows :
package com.example.game;
import android.app.Activity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private int x1=0,x2=0,m=150;
public int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
Point size= new Point();
display.getSize(size);
// int width = size.x;
height = size.y;
showview2();
showview1();
game();
}
public void game()
{
buttonSetup1_rig();
buttonSetup1_lef();
buttonSetup2_rig();
buttonSetup2_lef();
//while(m>=0)
for(int i=0;i<150;i++)
{
showball();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
m=m-10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
protected void buttonSetup1_rig()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton2);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x1<(height-320))
x1=x1+10;
showview1();
}
});
}
protected void buttonSetup1_lef()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton1);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x1>(0))
x1=x1-10;
showview1();
}
});
}
protected void buttonSetup2_rig()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton6);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x2<(height-320))
x2=x2+10;
showview2();
}
});
}
protected void buttonSetup2_lef()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton4);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x2>(0))
x2=x2-10;
showview2();
}
});
}
public void showview1()
{
ImageView image=(ImageView)findViewById(R.id.imageView1);
LinearLayout.LayoutParams lp ;
lp=(LinearLayout.LayoutParams)(image.getLayoutParams());
lp.setMargins(x1,0,0,0);
image.setLayoutParams(lp);
}
public void showview2()
{
ImageView image=(ImageView)findViewById(R.id.imageView2);
LinearLayout.LayoutParams lp ;
lp=(LinearLayout.LayoutParams)(image.getLayoutParams());
lp.setMargins(x2,0,0,0);
image.setLayoutParams(lp);
}
public void showball()
{
ImageView img=(ImageView)findViewById(R.id.imageView3);
LinearLayout.LayoutParams l ;
l=(LinearLayout.LayoutParams)(img.getLayoutParams());
int leftMargin = l.leftMargin;
int rightMargin = l.rightMargin;
int topMargin = l.topMargin;
l.setMargins(m,0,m,0);
img.setLayoutParams(l);
}
}
My Xml is :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_above="@+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/left" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/right" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lc" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="280dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="140dp"
android:src="@drawable/untitled" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="64dp"
android:src="@drawable/lc" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="2.25" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/left" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageButton1"
android:layout_alignParentRight="true"
android:src="@drawable/right" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>