When someone touches a red circle on my app nothing happens - java

I am making an app that displays circles randomly on the screen. The circles are either red or green. The purpose of the app is so that when someone touches a green circle something good happens, like they get points added to their score. When a red circle is clicked something bad happens like a new activity is started and the page says you failed or something. Here is my code for this app. In this code i receive no errors, nothing in logcat, everything works just fine. The circles display randomly on the screen as well as the score which is by default 0. The problem i am having with this app is that when a red or a green circle is clicked nothing happens.
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
int randomWidth = 0;
int randomHeight = 0;
public static int addPoints = 0;
private final Runnable updateCircle = new Runnable() {
#Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
if(random == null){
randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
}else {
randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
}
canvas.drawCircle(randomWidth, randomHeight, radius, paint);
paint.setColor(Color.BLACK);
paint.setTextSize(150);
canvas.drawText("Score: " + addPoints, 120, 300, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true){
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
} else {
addPoints++;
}
}else {
}
return true;
}
public boolean isInsideCircle(int x, int y){
if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius))){
return true;
}
return false;
}
}

Your view might not be implementing the View.OnTouchListener interface, hence it not calling the onTouch() method.
Your view is not being set a View.OnTouchListener via the view's View.setOnTouchListener(View.OnTouchListener) method.
Android Developer Reference - View.OnTouchListener
Either way it feels wrong to make a view implement this interface to listen to itself. Instead you may want to take a look at the View.onTouchEvent(MotionEvent event) method; perhaps it serves your purpose (or it should in this case). Listener interfaces are supposed to be implemented by external components. Say, for example, if you wanted to make your TextView listen every time a Button or ImageView is touched, you could extend TextView/Button and make them implement the Listener interface then pass it to you view as a parameter of its setOnTouchListener(View.OnTouchListener).
However, all views have a method called onTouchEvent(). This View.onTouchEvent() method should be used if you want to listen to events within the view itself, as it is by default called whenever the view is, well touched. Should you need a reference to the view itself with this approach, you call this since the view itself would be your current scope.
Android Developer Reference - View.onTouchEvent(MotionEvent event)
If you do it like this, all you would need to do in order to make your code work is change your onTouch() method to an override of onTouchEvent() like this: (Also added verification of touch action as suggested by Pooja and Tom so you don't take into account MOVE or UP events. Change DOWN for UP depending on when you want to trigger the event)
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true) {
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
this.getContext().startActivity(i);
} else {
addPoints++;
}
} else {
//Handle case not inside the circle
}
}
return true;
}

The onTouch method is meant to be used with OnTouchListener and would normally be defined outside your custom view class. For example:
this.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// touch handling code
return true;
}
});
In the case where you are looking for touch events inside your custom view, you should implement the onTouchEvent method. You may also want to check for either ACTION_UP or ACTION_DOWN or you will be processing multiple touch events.
#Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true) {
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
} else {
addPoints++;
}
result = true;
}
}
return result;
}
See the following for more details:
Input Events

Related

How to use onClickListener on two CanvasView Objects?

I am trying make a game such that when user click on shooter it changes its position and and shoots and if user click on shooted balls (or shoots as named in project) they disappear CODE HERE
Shooter.JAVA
public class shooter extends View {
//Consider all variables are declared
public shooter(int color, Context c) {
super(c);
paint = new Paint();
paint.setColor(color);
mContext = c;
}
public void move() {
//Moves Cannon On Click (CODE NOT SHOWN PURPOSELY)
invalidate();
}
public float getPosition()
{
return shootPoint;
}
public void draw(Canvas canvas)
{
super.draw(canvas);
// simply draws a rectangle (shooter)
cW=getWidth();
cH=getHeight();
center=new Point(cW/2,cH);
left=0; right=center.x; shootPoint=right/2;
canvas.drawRect(left,top,right,bottom,paint);
}
}
Another Java Class Named shoot.java is also there to make shoot balls when shootbutton is clicked
but I want that when user click on those balls(drawn on canvas ) they should reset
Main Game View Class
public class GameView extends FrameLayout implements View.OnClickListener {
//Consider all objects and variables are declared as used
public GameView(Context context, AttributeSet attrs) {
super(context,attrs);
//CONTAIN INITIALIZATION OF OBJECTS AS USED OF OTHER CLASSES
bullets = new ArrayList<shoot> ();
addView(cannon);
for (int i = 0; i < bullets.size(); i++ ) {
addView(bullets.get(i));
bullets.get(i).setOnClickListener(this);// an arrays of objects of shoot class
}
cannon.setOnClickListener(this);
level=3;level++;
}
#Override
public void onClick(View view) {
switch()// Here is the MAIN PROBLEM HOW SHOULD I DIFFERENTIATE THAT CANNON IS CLICKED OR //BULLETS LIKE USING VIEW.GETTAG()
{
case ----1:// WHAT CASE SSHOULD I WRITE
cannon.move();
break;
case ----2: // HERE ALSO
bullets.remove();
break;
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawGameBoard(canvas);
try
{
Thread.sleep(5);
} catch (InterruptedException e) {
}
invalidate();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
aliens.setBounds(0,0,width,height);
for (int i = 0; i < bullets.size(); i++ ) {
bullets.get(i).setBounds(0,0,width,height);
}
}
public void drawGameBoard(Canvas canvas) {
cannon.draw(canvas);
for ( int i = bullets.size() - 1; i >= 0; i--) {
if (bullets.get(i) != null) {
bullets.get(i).draw(canvas);
}
}
for (int i = explosions.size() - 1; i >= 0; i--) {
if (explosions.get(i) != null) {
if (!explosions.get(i).draw(canvas)) {
explosions.remove(i);
}
}
}
if (aliens != null) {
aliens.draw(canvas);
RectF guyRect = aliens.getRect();
for (int i = bullets.size() - 1; i >= 0; i--) {
if (RectF.intersects(guyRect, bullets.get(i).getRect())) {
explosions.add(new explosion(Color.RED,mContext, aliens.getX()-aliens.dst, aliens.getY()-aliens.dst));
aliens.reset();
bullets.remove(i);
break;
}
}
if (!aliens.move()) {
aliens = null;
}
}
}
// Whenever the user shoots a bullet, create a new bullet moving upwards
public void shootCannon() {
bullets.add(new shoot(Color.RED, mContext, cannon.getPosition(), (float) (height-100)));
}
}
I have showed the part of the code where I am having the problem that is the overridden function ONCLICK in GAMEVIEW.JAVA by comments like how to know what is clicked so todo their respected functions
please notify me if you didn't understand my question
If I understand the question correctly, you want to use one onClickListener function to handle the click events for both your cannon and your bullets.
Since both are different classes you may differentiate between them via '''instanceof'''.
That means your onClickListener would look something like this:
#Override
public void onClick(View view) {
if(view instanceof shooter) {
cannon.move();
}
else if(view instanceof shoot) {
bullets.remove();
}
}
I hope this helps you. If anything remains unclear I'll gladly response :)
If you want to know the coordinates of the touch which you can use to figure out, if an item is tapped,
consider using View.OnTouchListener instead of View.OnClickListener.
View.OnTouchListener has this function:
onTouch(View v, MotionEvent event)
How to use:
check if event is a tap: event.getAction() ==
MotionEvent.ACTION_DOWN
get the coordinates: event.getX() and event.getY()
And after that, replace cannon.setOnClickListener(this); with cannon.setOnTouchListener(this);
If there are any questions, do not hesitate to ask them!

Making a Bitmap clickable using onTouchEvent - Android

I've created a SurfaceView and I've put an onTouchEvent on it, and the MotionEvent gets the whole screen, like this:
#Override
public boolean onTouchEvent (MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
if (!player.getPlaying()&& newGameCreated && reset) {
player.setPlaying(true);
player.fly(true);
lives = 3;
}
if (player.getPlaying()) {
if (!started) started = true;
reset = false;
player.fly(true);
}
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP) {
player.fly(false);
return true;
}
// This is inside a class that extends SurfaceView
return super.onTouchEvent(event);
}
And I need to check touch on bitmap or not using x-y position of touch and do a different action if the condition is true, how can I do this on this onTouchEvent method?
You need to know coordinates of bitmap, his height and width. If you know all of it you can declare method:
public boolean clickedOnBitmap(final int x, final int y) {
return x > bitmap.x && x < bitmap.x + bitmap.width
&& y > bitmap.y && y< bitmap.y + bitmap.height;
}

Bitmap.getPixel in SurfaceView

I have this code where there is a Backgroung under an Hand, both a Bitmap images. Now, i have to control with an onClick method if the click touches the hand or not. For this, i wrote here and some guys tell me to use a method called "bitmap.getPixel(int x, int y)" that gives the color of pixel touched. For control if the click doesn't touch the hand, i write "bitmap.getPixel(x, y) != Color.TRASPARENT", but doesn't work perfectly. Under this i write the code of SurfaceView.
P.S. The bitmap "Hand" is a picture of a central Hand with parts trasparent around.
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private long missileStartTime;
private ThreadLv1 thread;
private OggettiLv1 hand;
private int conto=0;
private Sfondo sfondo;
MediaPlayer ouch;
public GamePanel(Context context){
super(context);
getHolder().addCallback(this);
thread = new ThreadLv1(getHolder(), this);
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}
#Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(retry)
{
try{thread.setRunning(false);
thread.join();
}catch(InterruptedException e){e.printStackTrace();}
retry = false;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder){
hand = new OggettiLv1(BitmapFactory.decodeResource(getResources(), R.drawable.mano5));
sfondo = new Sfondo(BitmapFactory.decodeResource(getResources(), R.drawable.sfondo));
ouch= MediaPlayer.create(getContext(), R.raw.ouch);
knifesong.start();
thread.setRunning(true);
thread.start();
}
public void update() {
}
#Override
public void draw(Canvas canvas)
{
if(canvas!=null) {
final int savedState = canvas.save();
background.draw(canvas);
hand.draw(canvas);
drawText(canvas);
canvas.restoreToCount(savedState);
}
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
int c = hand.sprite.getPixel((int) event.getX(), (int) event.getY());
if(c!=Color.TRANSPARENT){
conto++;
}
return true;
}
return false;
}
}
Someone know the problem and the solution in this code? Thanks in advance.
I think this might be the problem:
int c = hand.sprite.getPixel((int) event.getX(), (int) event.getY());
I think you are getting the value of a pixel at the x,y location on the sprite, you should get the value of the pixel on canvas.
EDIT:
Since your canvas is static, I suggest doing the following:
add a Bitmap to your class
private Bitmap canvasState;
In the draw function, initialize canvasState before anything else
canvasState = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.RGB_565));
canvas.setBitmap(canvasState);
after that, every change that you make to the canvas will be made to the canvasState, and you can use that in your event handler like so:
int c = canvasState.getPixel((int) event.getX(), (int) event.getY());
I'm not sure if the code I wrote will compile (I wrote them from the top of my head) but I think you can work out compile errors yourself.

How to get (x,y) cordinates of image in android?

I have drawn a image on ONTOUCH with (X,Y)cordinates while moving the image it's cordinates should move along with the image when it reaches it end point the image should be drawn there or else the image should go back to it's starting position.
For eg:(in our mobile if kepad lock we will drag it to open if it reaches it end point the lock will open or else the image will reaches it's starting position).
Reffered link:http://www.vogella.com/tutorials/AndroidTouch/article.html
Here is my code:
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mLocked) {
return false;
}
final int action = event.getAction();
float x = event.getX();
float y = event.getY();
if (mOnDrawerScrollListener != null) {
mOnDrawerScrollListener.onScrollStarted();
}
final int pt = getSide();
mTouchDelta = (int)(y - pt);
prepareTracking(pt);
mVelocityTracker.addMovement(event);
final Rect frame = mFrame;
final View handle = mHandle;
If anyone have idea about this please help me friends.
If you want to get(x, y) cordinates of view (ImageView), just use view.getX(), view.getY(). But when you get it in onCreate(), the results will be (0, 0), because it is not created yet. You should use view.getX(), view.getY() in onGlobalLayoutListener(). For e.g:
rlRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
rlRoot.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.d("X", view.getX()+"");
Log.d("Y", view.getY()+"");
}
});
With rlRoot is the layout includes your view and view may be ImageView or any View.
Try it, hope it help.

Changing reference to object causes previous object data to be overwritten

I seem to have a problem with references to java objects, probably as a result of years of programming in C. The following code is supposed to enable me to move objects of "WordPart" around a canvas. When I choose the first object, it moves as expected. When I release the first object, and choose the second, the location of of both the first and the second objects becomes identical. (I have removed code needed for compilation, but irrelevant to the question.) As far as I can tell, the activeImage reference seems to point to both objects in the wordPartList simultaneously. Please help me find my misunderstanding.
...
public class ONG2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new SurfaceView2(this));
}
public class SurfaceView2 extends SurfaceView implements SurfaceHolder.Callback {
ArrayList<WordParts> wordPartList;
WordParts activeImage;
Point activePosition;
String TAG = "SurfaceView2";
public SurfaceView2(Context context) {
super(context);
wordPartList = NewPartList(context);
this.getHolder().addCallback(this);
setFocusable(true);
activeImage = null;
activePosition = new Point();
}
/**
*
* #return list of word parts
*/
private ArrayList<WordParts> NewPartList(Context context) {
ArrayList<WordParts> newParts = new ArrayList<WordParts>();
newParts.add(new WordParts(context, new Point(400, 100), "foo"));
newParts.add(new WordParts(context, new Point(200, 100), "bar");
return newParts;
}
#Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "inside onDraw");
canvas.drawColor(Color.BLACK);
if (activeImage != null) {
activeImage.NewPosition(activePosition);
}
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(100, 300));
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(200, 300));
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(300, 300));
for (WordParts wp : wordPartList) {
Point center = wp.getCenter();
Log.d(TAG, String.format("Word part %s at x=%d, y=%d", wp.mWordPartString, center.x, center.y));
draw(canvas, wp.getBitMap(), center);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, String.format("inside on touch, event is %s", event.toString()));
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// if within wordPart, set as active
for (WordParts wp : wordPartList) {
if (wp.isInside(event)) {
activeImage = wp;
Log.d(TAG, String.format("Touch down inside word part %s", wp.mWordPartString));
break; // end iteration
}
}
break;
case MotionEvent.ACTION_UP:
for (WordParts wp : wordPartList) {
if (activeImage == wp){
wp.endTouch();
Log.d(TAG, String.format("Released word part %s", wp.mWordPartString));
}
}
activeImage = null;
break;
default:
break;
}
activePosition.x = (int) event.getX();
activePosition.y = (int) event.getY();
if (activeImage != null)
this.postInvalidate(); // force call to onDraw
return true;
}
/**
* Draw the bitmap centered at specified point
* #param canvas - target of drawing
* #param bm - bitmap to draw
* #param centerPt
*/
public void draw(Canvas canvas, Bitmap bm, Point centerPt) {
canvas.drawBitmap( bm,
centerPt.x - (bm.getWidth() / 2),
centerPt.y - (bm.getHeight() / 2), null);
Log.d(TAG, String.format("canvas %s, bm %s, point %s", canvas.toString(), bm.toString(), centerPt.toString()));
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height)
{
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("surface", "Surface created");
setWillNotDraw(false);
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("surface", "Surface destroyed");
}
}
}
I suspect this is the problem:
if (activeImage != null) {
activeImage.NewPosition(activePosition);
}
Unfortunately we don't have the code for unconventionally-named NewPosition, but presumably it sets the value of a field in a WordParts. If you set the value of two fields within two separate WordParts objects to refer to the same Point, then later when you change the values within that Point like this:
activePosition.x = (int) event.getX();
activePosition.y = (int) event.getY();
then yes, those changes will be visible via both WordParts objects, as they both refer to the same object.
If you're new to Java, I would strongly advise you to learn the basics of the language through simple console apps. Learn about objects, references and primitives, inheritance, the core collections and IO etc, then start working in the trickier environment of UIs and mobile.

Categories