Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Buy OSRS Gold

Sea Shepard

PaintHandler Class - drawModel, drawtile, drawAreaOnMinimap, drawPath

Recommended Posts

Hey guys.

I created this simple paint helper class, since I found I was debugging a lot of the same paint information over and over. Hopefully this can help you out. I don't promise the code shown is perfect or uses best practices, so please refrain from nitpicking unless it's educational and beneficial to the snippit.

Here are some screenshots of it in action:

Here you can see everything the file does; draws npcs, tiles, paths on and off the minimap, and draws areas on the minimap.

Posted Image

Here is another shot of the pathing on and off the minimap

Posted Image

As always, suggestions and comments are welcome.

PaintHandler.java PASTEBIN - LINK - DOWNLOAD

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.util.LinkedList;
 
import org.tribot.api.General;
import org.tribot.api.Minimap;
import org.tribot.api.Screen;
import org.tribot.api2007.Game;
import org.tribot.api2007.Projection;
import org.tribot.api2007.types.RSModel;
import org.tribot.api2007.types.RSTile;
 
/**
 * Utility class for doing a couple different things with
 * paint like drawing models and tiles.
 *
 * Instantiate this in your main script class and implement
 * Painting. In your draw(Graphic g) method, simply pass the graphics
 * object into this class's methods casting it to Graphics2D
 * @author Sea Shepard
 *
 */
public class PaintHandler {
       
        /**
         * Draws a model onto the screen
         * @param model
         *              The model we want to draw
         * @param g
         *              Our grapics render
         * @param fill
         *              Whether or not we should fill the model
         */
        public void drawModel(RSModel model, Graphics2D g, boolean fill) {
                if(model.getAllVisiblePoints().length != 0) {
                        if(fill) {
                                //fill triangles
                                for(Polygon p : model.getTriangles()) {
                                        g.fillPolygon(p);
                                }
                        } else {
                                //draw triangles
                                for(Polygon p : model.getTriangles()) {
                                        g.drawPolygon(p);
                                }
                        }
                }
        }
       
        /**
         * Draws our path
         * @param path
         *              The tile array we are drawing
         * @param g
         *              Our graphics render
         * @param minimap
         *              Whether or not we are drawing to the minimap
         */
        public void drawPath(RSTile[] path, Graphics2D g, boolean minimap) {
                Point[] points = new Point[path.length];
                if(minimap) {
                        //Drawing on the other whatever
                        for(int i = 0; i < path.length-1; i++) {
                                Point tilePoint = Projection.tileToMinimap(path[i]);
                                if(Projection.isInMinimap(tilePoint)) {
                                        if(i < path.length-1) {
                                                Point nextPoint = Projection.tileToMinimap(path[i+1]);
                                                if(Projection.isInMinimap(nextPoint)) {
                                                        g.drawLine(tilePoint.x, tilePoint.y, nextPoint.x, nextPoint.y);
                                                }
                                        }
                                        g.fillOval(tilePoint.x, tilePoint.y, 3, 3);
                                }
                        }
                } else {
                        for(int i = 0; i < path.length-1; i++) {
                                if(path[i].isOnScreen()) {
                                        if(i > 0) { //could just start index
                                                Point currentTile = Projection.tileToScreen(path[i], 0);
                                                Point lastTile = Projection.tileToScreen(path[i-1], 0);
                                                g.drawLine(currentTile.x, currentTile.y,
                                                                lastTile.x, lastTile.y);
                                        }
                                        drawTile(path[i], g, false);
                                }
                        }
                }
        }
       
        /**
         * Draws a tile onto the screen
         * @param tile
         *              The tile you want to draw
         * @param g
         *              The graphics render we are using
         * @param color
         *              The color we want to use as filler
         * @param fill 
         *              Whether or not we are filling the tile
         */
        public void drawTile(RSTile tile, Graphics2D g, boolean fill) {
                if(tile.isOnScreen()) {
                        if(fill) {
                                g.fillPolygon(Projection.getTileBoundsPoly(tile, 0));
                        } else {
                                g.drawPolygon(Projection.getTileBoundsPoly(tile, 0));
                        }
                }
        }
       
        /**
         * Draws an area on the minimap
         * @param area
         *              The area we are drawing
         * @param g
         *              Our grapihcs render
         * @param fill
         *              Whether or not we should fill the area
         */
        public void drawAreaOnMinimap(RSArea area, Graphics2D g, boolean fill) {
                //Get the tiles of the area
                LinkedList<RSTile> tiles = new LinkedList<>();
                for(Point p : area.getPoints()) {
                        //no plane support
                        tiles.add(new RSTile(p.x, p.y));
                }
                //points to tiles to points again :3
                Polygon minimapArea = new Polygon();
                for(RSTile t : tiles) {
                        Point minimapPoint = Projection.tileToMinimap(t);
                        if(Projection.isInMinimap(minimapPoint)) {
                                minimapArea.addPoint(minimapPoint.x, minimapPoint.y);
                        }
                }
               
                if(fill) {
                        g.fillPolygon(minimapArea);
                } else {
                        g.drawPolygon(minimapArea);
                }
        }
}

RSArea.java PASTEBIN - LINK - DOWNLOAD

import java.awt.Point;
import java.awt.Polygon;
import java.util.LinkedList;
 
import org.tribot.api2007.types.RSCharacter;
import org.tribot.api2007.types.RSTile;
 
/**
 * An area in runescape created by two tiles.
 *
 * @author Sea Shepard, Some powerbot contributions as well
 *
 */
public class RSArea {
 
        /** The rectangle polygon representing this object */
        private Polygon areaPolygon;
 
        private LinkedList<Point> mPoints;
       
        public Polygon getAreaPolygon() {
                return areaPolygon;
        }
 
        public LinkedList<Point> getPoints() {
                return mPoints;
        }
       
        /** Creates an RSArea from the two tiles given. */
        public RSArea(RSTile tile1, RSTile tile2) {
                this(new RSTile(Math.min(tile1.getX(), tile2.getX()), Math.min(
                                tile1.getY(), tile2.getY())), new RSTile(Math.max(tile1.getX(),
                                tile2.getX()), Math.min(tile1.getY(), tile2.getY())),
                                new RSTile(Math.max(tile1.getX(), tile2.getX()), Math.max(
                                                tile1.getY(), tile2.getY())), new RSTile(Math.min(
                                                tile1.getX(), tile2.getX()), Math.max(tile1.getY(),
                                                tile2.getY())));
        }
 
        /**
         * Creates an RSArea based on the given tiles
         *
         * @param rsTiles
         */
        public RSArea(RSTile... rsTiles) {
                areaPolygon = new Polygon();
                mPoints = new LinkedList<>();
                for (RSTile tile : rsTiles) {
                        areaPolygon.addPoint(tile.getX(), tile.getY());
                        mPoints.add(new Point(tile.getX(), tile.getY()));
                }
        }
 
        /**
         * @return The center of our area, ignoring planes
         */
        public RSTile getCenter() {
                return areaPolygon.npoints > 0 ? new RSTile(
                                (int) Math.round(avg(areaPolygon.xpoints)),
                                (int) Math.round(avg(areaPolygon.ypoints))) : null;
        }
 
        /**
         * @param character
         *            The character we are checking
         * @return Whether or not the player is in the area. True if so, false
         *         otherwise.
         */
        public boolean contains(RSCharacter character) {
                return areaPolygon.contains(character.getPosition().getX(), character
                                .getPosition().getY());
        }
 
        /**
         * @param tile
         *            The tile we are checking
         * @return Whether or not the tile is in the area, true if so, false
         *         otherwise
         */
        public boolean contains(RSTile tile) {
                return areaPolygon.contains(tile.getX(), tile.getY());
        }
 
        /**
         * @param nums
         *            The numbers we are averaging
         * @return The average of the numbers
         */
        private double avg(final int... nums) {
                long total = 0;
                for (int i : nums) {
                        total += (long) i;
                }
                return (double) total / (double) nums.length;
        }
 
        /**
         * Checks whether or not the area contains the given point
         *
         * @param tile
         * @return
         */
        public boolean contains(int x, int y) {
                return areaPolygon.contains(x, y);
        }
}
Edited by iFluffee
  • Like 1

Share this post


Link to post
Share on other sites

Looks good mate :).

Thanks! You know what's embarrassing, I spent like two hours working out the offsets and writing tileToPoint methods etc because I didn't realize the Projection class had like everything already done. I was so proud of it and then I realized it was totally unnecessary. :c

Share this post


Link to post
Share on other sites

so do I just call drawModel, for example, and put it in my method that uses models? or does this go into the paint?

i am confused

Not really sure what is unclear about this. You create a PaintHandler object and pass in your graphics object that you receive from your draw method into any of the methods provided.

Share this post


Link to post
Share on other sites

so do I just call drawModel, for example, and put it in my method that uses models? or does this go into the paint?

i am confused

Not really sure what is unclear about this. You create a PaintHandler object and pass in your graphics object that you receive from your draw method into any of the methods provided.

I am a couple weeks new to java and I am having trouble implementing it. I want to use it to debug a few things in a script of mine (currently RSArea debugging). So I have created the PaintHandler object but my problem is in my onPaint() method. onPaint() requires Graphics objects rather than Graphics2D objects.

http://gyazo.com/d6aa0ee1c3651d44a84db58923286a40

This is the problem I am getting. The error is caused because g is a Graphics object rather than a Graphics2D object, and when I change the method to onPaint(Graphics2D g), TRiBot no longer implements it.

How did you go about implementing painting the Graphics2D object in your script? Sorry, like I said I am pretty new to java and I am trying to figure out why certain things work, not just copy code and paste it in my scripts. Thanks :)

Share this post


Link to post
Share on other sites

How did you go about implementing painting the Graphics2D object in your script? Sorry, like I said I am pretty new to java and I am trying to figure out why certain things work, not just copy code and paste it in my scripts. Thanks :)

You have to cast the graphics object to a graphics2d object. This can be done by doing

public void onPaint(Graphics2D g) {   Graphics2D graphics = (Graphics2D) g;   paintHandler.drawWhatever(graphics); //Replace with desired paint handler method}

Share this post


Link to post
Share on other sites

You have to cast the graphics object to a graphics2d object. This can be done by doing

public void onPaint(Graphics2D g) {   Graphics2D graphics = (Graphics2D) g;   paintHandler.drawWhatever(graphics); //Replace with desired paint handler method}

Ahh this works great, however, it is extremely laggy. This can be worked around of course, I am just wondering if its just me or if this causes you lag too? I am using your PaintHandler.drawAreaOnMinimap() method.

Share this post


Link to post
Share on other sites

This looks good.  One recommendation I gave someone else using this today is to make all the PaintHandler functions static.  That way they don't have to first create a PaintHandler object and then call the methods, they can just do PaintHandler.function();

I wasn't sure if that was poor practice when handling Graphic objects. I actually wrote it up that way at first but it seemed wrong to me, I don't really have a specific reason why. I'll edit the post containing directions to change this if users desire to do so.

Share this post


Link to post
Share on other sites

Hey just wanted to express some gratitude for this snippet and short explanation.

I'm just toying around with some of the more primitive Tribot features at the moment,

and this saved me a lot of the trouble, some of which you claimed you experienced! 

 

Thanks,

Warfront1

Share this post


Link to post
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.


  • Our picks

    • Gradle is a build tool used to accelerate developer productivity.

      We recently setup a Maven repository (TRiBot Central) to make it easier for scripters to create scripts. Check it out here: https://gitlab.com/trilez-software/tribot/tribot-central/-/packages

      Furthermore, we've released a simple Gradle project to make it easy to run TRiBot and develop scripts for it. Check it out here: https://gitlab.com/trilez-software/tribot/tribot-gradle-launcher

      The goals of TRiBot Central are to:

      Deliver updates to TRiBot faster


      Better organize TRiBot's dependencies (AKA dependancies)


      Make it easier to develop scripts for TRiBot


      Make it easier to use and run TRiBot


      Note: TRiBot won't be able to run scripts from within this project until TRiBot's next release.
      • 8 replies
    • Hi everyone,

      I'd like to thank everyone for their patience in this transition period. Since last week, we've worked out the remaining bugs with this integration.

      Some users have still been having issues with connecting their forums account to their Auth0 account. To resolve this, we've imported all forums accounts into Auth0.

      Unfortunately, the accounts which were imported today were using an unsupported password hashing algorithm. Hence, random passwords were set during the import.

      What does this mean for me?

      If you've previously linked your forums account to your Auth0 account, you don't have to do anything. Nothing changes for you.


      If you haven't logged in via our new login yet,

      Try logging in with your forums email address and the last password you used


      If you are unable to login, please use the "Forgot password" tool on the login page:
      Follow the instructions to reset your password
       
      • 15 replies
    • Hello everyone,

      Last week we tried to roll out Auth0 Login, but we lost that battle. Now it's time to win the war!

      Important changes

      When logging into the client, you'll now have to enter your Auth0 account credentials instead of your forums credentials

      Note: 2FA is still handled through your forums account (for the time being)



      Changes for existing users

      You'll have to link your Auth0 account to your forums account here: https://tribot.org/forums/settings/login/?service=11


      Auth0 accounts have been created for most existing users. Please use your forums email address and password to login.



      Important notes

      Make sure to verify your email address upon creating a new Auth0 account


      When we mention your Auth0 account, we mean your account used for auth.tribot.org as displayed below
      • 81 replies
    • To better support the upcoming changes (TRiBot X, new repository), we're switching our login handler to Auth0. Instead of logging in with the standard form, you'll now be required to login through our Auth0 application.

      All existing accounts which have been used within approximately the past year have been imported into Auth0 using the same email and password combination which has been stored on the forums.

      What does this mean for users?

      Your account credentials are now even more securely stored


      You'll be able to login via Facebook, Google, and others in the future


      Is there anything users have to do differently now?

      Existing users: You'll have to login with the standard login, open your Account Settings, then link your Auth0 account


      New users: You'll be redirected to our Auth0 app (auth.tribot.org) where you'll be able to create an account


      Why was this change made?

      The new apps we are creating (such as the new repository) aren't able to use the forums to handle user logins


      To centralize all user accounts in one area


      To ensure that the client login doesn't go down when the forums are having problems


      To speed up our development


      Other considerations

      There's no documentation or official support for using Invision Community combined with Auth0, so there are still a few kinks we're working out


      We're in the works of creating an account management panel specifically for Auth0 accounts (ETA August)


      It's not possible to change email addresses for the time being (this will be resolved this August)


      Changing passwords is a weird process for the time being. To change your password, you'll have to use the "Don't remember your password" tool on the Auth0 login page
      • 11 replies
    • Over the past month, we've been working hard on TRiBot's new repository - a much needed update. This change has been deemed necessary for TRiBot X, and will allow us to really speed up development of all aspects of TRiBot.

      Today we are going to share what we've been working on!


      Now you must be wondering what kind of features the new repository will have.... well, you'll have to be patient for a little while longer. We're still figuring out various technical aspects so we can't provide answers to all possible questions. We're also focusing on development rather than writing about it so that everyone can get access to our latest developments at lightning speed. I will however answer a few users' questions.

      We're planning on a release of this early to mid August, giving users some goodies before TRiBot X's release.

      Thank you all for being patient. I hope everyone is excited as much as I am!
      • 17 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...