2

What I want to do is to click a space on the screen, get the coordinates and paint the image here. I did this using paintIcon and mouseListener in eclipse but how to do the same thing in android studio? Thanks!

public static Game game = new Game();

public Control(){
    this.setLayout(null);
    this.setBounds(0, 0, 780, 780);
    addMouseListener(this);
}

public void paint(Graphics g){
    setBackground(Color.GREEN);
    board.paintIcon(this, g, 0, 0);

    for(int i = 0; i < 19; i++){
        for(int j = 0; j < 19; j++){
            if(game.gameBoard.board[i][j] != 'E'){
                if(game.gameBoard.board[i][j] == 'B'){
                    black.paintIcon(this, g, j * 40, i * 40);
                }
                if(game.gameBoard.board[i][j] == 'W'){
                    white.paintIcon(this, g, j * 40, i * 40);
                }
            }
        }
    }
}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    mouseX = e.getX();
    mouseY = e.getY();
    int targetX = mouseX / 40;
    int targetY = mouseY / 40;
    game.move(targetX, targetY);
    repaint();
}
1
  • You just want use Java file in Android Studio which is build in Eclipse...right? Commented Nov 15, 2017 at 8:47

1 Answer 1

2

In Android you use a different system than in "Java"(you know Android is Java), for instance as is clear from your code you do not have a Mouse but a Touch event. The first thing you should do is to study this official link that explains the concept of Canvas. Then you will learn how to integrate the "tap" into your app that is instead this official link

This is an excellent post to learn ho to do it.

You begin extending the View and overriding the method onDraw(Canvas canvas) then you need to override a listener that say you when the user clicked the screen onTouchEvent(MotionEvent event) usually there are three motion events you want to cover MotionEvent ACTION_DOWN(you tap and the screen recognizes the X,Y coordinates, that is the one you need),ACTION_MOVE(if you drag your finger maintaining pressure on the screen) and ACTION_UP(when you release your finger)

Just remember you need to call invalidate() if you have an animation or something changes on the screen. Basically is a "forced" call to the "onDraw" method. On the basis of the three links I sent you can cover all thebasis of the 2d graphics in Android that is a bit different by Java because of the different features but also the Android dependency on the specific SDK classes

what I want to do is to click a space on the screen, get the coordinates and paint the image here

Here is an example of how you can obtain that in Android instead of "just" Java, please see the notes comments I did below with the double slashes //

public class YourBoard extends View {//EXTENDS VIEW this is important because you override methods

Drawable editIcon = getResources().getDrawable(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);

float xPos = 0;
float yPos = 0;

public YourBoard (Context context) {//HERE THE CONSTRUCTOR YOU CAN INITIALIZE THINGS HERE

    super (context);
}
@Override
protected void onDraw (Canvas canvas) {//This was your paint(Graphics g)
    super.onDraw(canvas);

    canvas.save();
    canvas.drawBitmap(mBitmap, 0, 0, null);
    canvas.translate(xPos, yPos);
    editIcon.draw(canvas);
    canvas.restore();


}
@Override
public boolean onTouchEvent (MotionEvent event) {//THIS WAS YOUR MOUSE LISTENER

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN :
            xPos = event.getX();
            yPos = event.getY();
            invalidate();//NOTICE THE INVALIDATE I MENTIONED
            break;
    }

    return true;

}
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

@CongYang thank you very much to you. Please feel free to upvote and accept the reply if you like
I accept the answer, but I cannot upvote yet since I'm new and do not have enough reputation, sorry about that...@trocchietto
now you can upvote:) I learned a lot from your question so you get my upvote that gives you the right to upvote. Good luck with your adventure with stackoverflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.