Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

User input click handling

Featured Replies

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

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.

  • Author

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

  • Author
@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

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.

  • Author

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

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);
}
  • Author

 

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

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

Use a rectangle and do

Rectangle.contains (event.getPosition ())

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.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.