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();
}
Cong Yang
  • 127
  • 1
  • 8

1 Answers1

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;

}
}
}
trocchietto
  • 2,607
  • 2
  • 23
  • 36
  • @CongYang thank you very much to you. Please feel free to upvote and accept the reply if you like – trocchietto Nov 15 '17 at 21:20
  • I accept the answer, but I cannot upvote yet since I'm new and do not have enough reputation, sorry about that...@trocchietto – Cong Yang Nov 15 '17 at 23:36
  • 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 – trocchietto Nov 16 '17 at 07:32