Jump to content

User input click handling


lisabe96

Recommended Posts

I have only 1 option for the user to chose and it's kinda ridiculous to make a GUI for that,

so I'd like to draw a toggle button to the game screen and handle it when it's clicked by the user.

I have no experience what so ever with this, how would I go about implementing this?

Edited by lisabe96
Link to comment
Share on other sites

Drawing a button is of course just g.drawX(...) in onPaint. Next, you basically just implement MouseListener (or extend MouseAdapter) and tell osbot you want to receive mouse events bot.addMouseListener(yourListener).  Then you implement mouseClicked(MouseEvent e), e has a property which gives you the location of the click, you can use this to determine if the click was within the bounds of your button.

Link to comment
Share on other sites

Drawing a button is of course just g.drawX(...) in onPaint. Next, you basically just implement MouseListener (or extend MouseAdapter) and tell osbot you want to receive mouse events bot.addMouseListener(yourListener).  Then you implement mouseClicked(MouseEvent e), e has a property which gives you the location of the click, you can use this to determine if the click was within the bounds of your button.

Oh so it's basically just default java implementation, I was expecting something more complex built into the API.

Thanks I think i can figure it out

  • Like 1
Link to comment
Share on other sites

@Override
public void onStart() {
	getBot().addMouseListener(this);
		
	try {
		normalImg = ImageIO.read(new URL("https://i.imgur.com/tXYml3w.png"));
		powerImg = ImageIO.read(new URL("https://i.imgur.com/LVpXu5C.png"));
		displayImage = normalImg;
	} catch (MalformedURLException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

@Override
public void mouseClicked(MouseEvent event) {
	int x = event.getX();
	int y = event.getY();
		
	if (x > 413 && x < 471 && y > 388 && y < 458) {
		powerfishing = !powerfishing;
		displayImage = powerfishing ? powerImg : normalImg;
	}
}

@Override
public void mouseMoved(MouseEvent event) {
	int x = event.getX();
	int y = event.getY();

	tooltip = x > 413 && x < 471 && y > 388 && y < 458;
}

@Override
public void onPaint(Graphics2D graphics) {
	Graphics2D g = (Graphics2D) graphics;
	g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		
		
	if (displayImage != null) {
		graphics.drawImage(displayImage, 420, 400, null);
	}
	if (tooltip) {
		g.setColor(Color.BLUE);
		g.drawString("Click to " + (powerfishing ? "disable" : "enable") 
			+ " powerfishing.", 311, 15);
	}
}

Got it all working perfectly smile.png

Thanks, code posted for people looking for this in the future

Edited by lisabe96
  • Like 2
Link to comment
Share on other sites

instead of this boolean

 if (x > 413 && x < 471 && y > 388 && y < 458) {

 

you could grab the position of the click. Use the shape of your button and see if the click is within the shape. be sides that your good.

The image is transparent, and I don't want users to have to click inside the fish, that would be a bad

case of UI handling. It has to work when they click the area around it as well.

s05zeOD.png

Edited by lisabe96
Link to comment
Share on other sites

The image is transparent, and I don't want users to have to click inside the fish, that would be a bad

case of UI handling. It has to work when they click the area around it as well.

s05zeOD.png

 

You would use a rectangle for that. Something along the lines of:

public static final Rectangle BUTTON_AREA = new Rectangle(413,388,58,70); // (x,y,width,height)

@Override
public void mouseClicked(MouseEvent event) {
	int x = event.getX();
	int y = event.getY();
		
	if (BUTTON_AREA.contains(x,y)) {
		powerfishing = !powerfishing;
		displayImage = powerfishing ? powerImg : normalImg;
	}
}

@Override
public void mouseMoved(MouseEvent event) {
	int x = event.getX();
	int y = event.getY();

	tooltip = BUTTON_AREA.contains(x,y);
}
Link to comment
Share on other sites

 

You would use a rectangle for that. Something along the lines of:

public static final Rectangle BUTTON_AREA = new Rectangle(413,388,58,70); // (x,y,width,height)

@Override
public void mouseClicked(MouseEvent event) {
	int x = event.getX();
	int y = event.getY();
		
	if (BUTTON_AREA.contains(x,y)) {
		powerfishing = !powerfishing;
		displayImage = powerfishing ? powerImg : normalImg;
	}
}

@Override
public void mouseMoved(MouseEvent event) {
	int x = event.getX();
	int y = event.getY();

	tooltip = BUTTON_AREA.contains(x,y);
}

I feel like it's a bit overkill here as I have no other similar situations and only have to check twice.

However you'd be right if you said it's a more proper way of doing it

Link to comment
Share on other sites

I feel like it's a bit overkill here as I have no other similar situations and only have to check twice.

However you'd be right if you said it's a more proper way of doing it

 

It's not overkill, it's managing your code better. You could also replace your image's x/y rendering with a call to that rectangles x/y coords, and use that in the future if you ever need to re-position your image/button.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...