Jump to content

[TUT] [PAINT] ★★★ Advanced Paint Tutorial - Calculations, images & more! ★★★


Recommended Posts

★★★ Advanced Paint Tutorial ★★★



So, you made a script and now you want to know how much gp/h or xp/h it makes for you?
Or perhaps you already made a paint for your script, but you just think it's too ugly with the white text everywhere?
 
Here's a few examples of different paints (all rights to the image owners):
 
diLPJrJ.png



Table of contents

1. Adding a paint

2. Let's edit the font & color

3. Calculations & time etc.

4. Adding image background & little style to your paint

5. Mouse paint [COMING SOON]



1. Adding a paint
Ok, let's add a paint to your script.

Import these:
 

import java.awt.Color; //to get different colors
import java.awt.Font; //to change font
import java.awt.Graphics; //paint
import java.awt.Graphics2D; //needed for the image
import java.awt.Image; //same as above
import java.io.IOException; //this is needed for the loading of the image
import java.net.URL; //same as above

import javax.imageio.ImageIO; //same as above

import org.tribot.api.Timing; //to calculate time things
import org.tribot.api2007.Skills; //to get XP/levels
import org.tribot.script.interfaces.Painting; //for onPaint()


First we need to change something from your script. Find a line looking about like this:

public class fisher extends Script {

 
and add before the bracket "implements Painting"
 

public class fisher extends Script implements Painting {

 
Now your IDE will probably tell we need to add an unimplemented method called onPaint().
I like to add onPaint() at the end of script:
 

public class fisher extends Script implements Painting {
    public void run() {
        //DO SOMETHING
    }
 
    @Override
    public void onPaint(Graphics g)  {
    }

}

 
 
Great! Now we can actually draw something to the screen:
 

public class fisher extends Script implements Painting {
    public void run() {
        //DO SOMETHING
    }

    public void onPaint(Graphics g)  {
        g.drawString("Some text here?", 300, 300);
    }
}

The text you want to display is in quotes, the 300s are the coordinates on the screen, (x,y).
 

eJ6aTSE.png

 
 
 
 
 
2. Let's edit the font & color
Ok now let's edit the look of the text. You can of course change the font, styling and the size.
Add something like this:

Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {
    g.setFont(font);
    g.drawString("Some text here?", 300, 300);
}


Let's edit the color. Colors are made in RGB.

Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {
    g.setFont(font);
    g.setColor(new Color(200, 0, 200));
    g.drawString("Some text here?", 300, 300);
}

GWP1Clo.png

 
 
 
 
3. Calculations & time etc.
So you want to display the runtime for example? Before your onPaint() method, add something like this:

private static final long startTime = System.currentTimeMillis();
Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {
    g.setFont(font);
    g.setColor(new Color(200, 0, 200));
    g.drawString("Some text here?", 300, 300);
}

 
 
and on your onPaint() add this:

private static final long startTime = System.currentTimeMillis();
Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {
    long timeRan = System.currentTimeMillis() - startTime;
    g.setFont(font);
    g.setColor(new Color(200, 0, 200));
    g.drawString("Some text here?", 300, 300);
}

 
 
Now every time your onPaint() method runs, it calculates the runtime, so let's print it on the screen:
 
 

private static final long startTime = System.currentTimeMillis();
Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {
    long timeRan = System.currentTimeMillis() - startTime;
    g.setFont(font);
    g.setColor(new Color(200, 0, 200));
    g.drawString("Runtime: " + timeRan, 300, 300);
}

 
Now you'll notice that the time looks pretty weird. Well, it's in milliseconds so lets convert it:
 
 

private static final long startTime = System.currentTimeMillis();

Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {

long timeRan = System.currentTimeMillis() - startTime;
g.setFont(font);

g.setColor(new Color(200, 0, 200));
g.drawString("Runtime: " + Timing.msToString(timeRan), 300, 300);
}

 
 

BuApbtr.png

And now we can see for how long the script has ran.
Let's add something else... Current lvl for example:
 

private static final long startTime = System.currentTimeMillis();

Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {

long timeRan = System.currentTimeMillis() - startTime;
int currentLvl = Skills.getActualLevel("Fishing");
g.setFont(font);

g.setColor(new Color(200, 0, 200));
g.drawString("Runtime: " + Timing.msToString(timeRan), 300, 300);
g.drawString("Current lvl: " + currentLvl, 200, 100);
}

 
Notice I used coordinate (200, 100) this time:
 
 

MKPju05.png

 
 
Let's add gained levels and a few xp things:
 

private static final long startTime = System.currentTimeMillis();
private int startLvl = Skills.getActualLevel("Fishing");
private int startXP = Skills.getXP("Fishing");


Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {

long timeRan = System.currentTimeMillis() - startTime;
int currentLvl = Skills.getActualLevel("Fishing");
int gainedLvl = currentLvl - startLvl;
int gainedXP = Skills.getXP("Fishing") - startXP;
int xpToLevel = Skills.getXPToNextLevel("Fishing");
int xpPerHour = (long)(gainedXP * 3600000 / timeRan);


g.setFont(font);

g.setColor(new Color(44, 44, 44));
g.drawString("Runtime: " + Timing.msToString(timeRan), 300, 370);
g.drawString("Current lvl: " + currentLvl + " (+" + gainedLvl + ")", 300, 390);
g.drawString("XP Gained: " + gainedXP, 300, 410);
g.drawString("XP TNL: " + xpToLevel, 300, 430);
g.drawString("XP/H: " + xpPerHour, 300, 450);

}

 
 
Notice I also changed the color and coordinates to make the paint look better:
 
 

s3ept76.png

 
 
Starting to look a little better, huh?
 
 
 
4. Adding image background & little style to your paint
 
So, you wanna make your paint look superdupercool? Well, let's add some images to it.
First you should edit your code like this:
 

private Image getImage(String url) {
try {
return ImageIO.read(new URL(url));
} catch(IOException e) {
return null;
}
}
private final Image img = getImage("your_img_url");


private static final long startTime = System.currentTimeMillis();
private int startLvl = Skills.getActualLevel("Fishing");
private int startXP = Skills.getXP("Fishing");

Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {
 
Graphics2D gg = (Graphics2D)g;
gg.drawImage(img, 0, 300, null);

 
long timeRan = System.currentTimeMillis() - startTime;
int currentLvl = Skills.getActualLevel("Fishing");
int gainedLvl = currentLvl - startLvl;
int gainedXP = Skills.getXP("Fishing") - startXP;
int xpToLevel = Skills.getXPToNextLevel("Fishing");
int xpPerHour = (long)(gainedXP * 3600000d / timeRan);

g.setFont(font);

g.setColor(new Color(44, 44, 44));
g.drawString("Runtime: " + Timing.msToString(timeRan), 300, 370);
g.drawString("Current lvl: " + currentLvl + " (+" + gainedLvl + ")", 300, 390);
g.drawString("XP Gained: " + gainedXP, 300, 410);
g.drawString("XP TNL: " + xpToLevel, 300, 430);
g.drawString("XP/H: " + xpPerHour, 300, 450);
}

 
 
Replace "your_img_url" with your image url and edit the coordinates so it looks good. I used coordinate (0, 300) as you can see.
 
I made these images for my tutorial in just a few minutes with Photoshop:
 
DqEsuRc.png

eeTP0S9.png
 
Finally I changed the text color to light grey and aligned the image correctly:
 
 

private Image getImage(String url) {
try {
return ImageIO.read(new URL(url));
} catch(IOException e) {
return null;
}
}
private final Image img = getImage("http://i.imgur.com/DqEsuRc.png");

private static final long startTime = System.currentTimeMillis();
private int startLvl = Skills.getActualLevel("Fishing");
private int startXP = Skills.getXP("Fishing");

Font font = new Font("Verdana", Font.BOLD, 14);
public void onPaint(Graphics g) {

Graphics2D gg = (Graphics2D)g;
gg.drawImage(img, 0, 304, null);

long timeRan = System.currentTimeMillis() - startTime;
int currentLvl = Skills.getActualLevel("Fishing");
int gainedLvl = currentLvl - startLvl;
int gainedXP = Skills.getXP("Fishing") - startXP;
int xpToLevel = Skills.getXPToNextLevel("Fishing");
int xpPerHour = (long)(gainedXP * 3600000d / timeRan);

g.setFont(font);

g.setColor(new Color(200, 200, 200));
g.drawString("Runtime: " + Timing.msToString(timeRan), 300, 370);
g.drawString("Current lvl: " + currentLvl + " (+" + gainedLvl + ")", 300, 390);
g.drawString("XP Gained: " + gainedXP, 300, 410);
g.drawString("XP TNL: " + xpToLevel, 300, 430);
g.drawString("XP/H: " + xpPerHour, 300, 450);
}

 
 
 
And the final result:
 

WrSl1bp.png




6. Antialiasing
I already posted this to snippets but I think it's good to add it here too:
Just a quick trick to make your paint look smoother. This works with shapes & text.

bnkoMhc.png
Antialiased is on the right.
 

I assume you already have added a paint. I think onPaint() method only accepts Graphics as an arg0, so you need to "convert" it to Graphics2D in order to make antialiasing work:



public void onPaint(Graphics g) {
    Graphics2D gg = (Graphics2D) g;
    gg.drawString("Runtime: " + Timing.msToString(timeRan), 300, 370);
}


before your onPaint() add this

private final RenderingHints aa = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


and inside your onPaint() add this

gg.setRenderingHints(aa);

 
Also import any needed stuff like java.awt.Graphics2D and java.awt.RenderingHints.
 
 
Finished code:

private final RenderingHints aa = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
public void onPaint(Graphics g) {
    Graphics2D gg = (Graphics2D) g;
    gg.setRenderingHints(aa);
    gg.drawString("Runtime: " + Timing.msToString(timeRan), 300, 370);
}

Edited by Deltac0
  • Like 7
  • Thanks 1
Link to post
Share on other sites

Good stuff.

But also experiencing the same problem:

When I use the exact same function for exp/hour as you, I get negative numbers after a certain amount of time. Like: -3495xp/hour. Do you know what could cause this? I'm thinking about the use of int, but when I try long, it's just the same.

Edited by KMJT
Link to post
Share on other sites
  • 1 month later...
  • 2 weeks later...
  • 2 weeks later...
  • 2 weeks later...
  • 3 months later...

Amazing tutorial! Followed step by step and now i'm a paint master :D

 

Found out a typo though..

 

long xpPerHour = (long)(gainedXP * 3600000 / timeRan);

 

should be

 

long xpPerHour = (long)(gainedXP * (3600000 / timeRan));

 

or it will say weird numbers like -41, 421...

 

Actually nevermind. It still shows like half of the real xp it should get in hour

Edited by thunderkill
Link to post
Share on other sites
  • 9 months later...
  • 1 year later...
  • 2 months later...

To anyone who uses this tutorial, two things to note when calculating XP per hour (Or anything per hour)

 

In this code where it says 

int xpPerHour = (long)(gainedXP * 3600000 / timeRan);

this will give you negative numbers for xpPerHour(because gainedXp  * 3600000 will very quickly go over 2^31 -1 and give big negative numbers messing up your calculations, so you shuold write it like this: 

long xpPerHour = (gainedXP * (3600000 / timeRan));

i see no reason to leave xp Per hour as an int aswell

 

However, if you look closely at these calculations, you will realize that it is slightly off, and it starts to get more and more wrong the longer your script runs, this is because we are using integer division.. So, for it to do the rounding last, we will now write it like this: 

long xpPerHour = (long)(gainedXP * (3600000 / (double)timeRan));

By first casting one of the numbers in the fraction to a double, it will now return a decimal that we can then switch back to an integer and removing any rounding issues.

 

 

Hope this helps!

  • Thanks 1
Link to post
Share on other sites
  • 4 years later...
  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Our picks

    • We've heard your complaints - the TRiBot API could be much easier to use. We've been dedicating our time to improving the scripter experience here and spent the past year working on a new and improved API - the TRiBot Script SDK.

       

      The TRiBot Script SDK is an easy-to-use library for building TRiBot scripts. It is the recommended approach to building scripts moving forward over the old TRiBot API. It contains all the core things you need to build a script, and a ton of additional helpful stuff to get you using your scripts quicker. See the documentation section for everything offered, and check out the brief overview link too.

       

      The SDK was announced in preview here:

       

      It is now officially released. The official release guarantees we will support backwards compatibility for some period of time. See the 'backwards compatibility' section below for more info.

       

      How to use:

      There is multiple options, listed in the order they are recommended.

      1) Use the gradle template mentioned below

      2) Obtain through gradle

      Add this dependency: api("org.tribot:tribot-script-sdk:+")

      Add this repository: maven("https://gitlab.com/api/v4/projects/20741387/packages/maven")

      3) Take from your local filesystem in your .tribot/install folder. For example, on windows, you'd find it at "C:\Users\[user]\AppData\Roaming\.tribot\install\tribot-client\lib\tribot-script-sdk-[version].jar"

       

      Documentation:

      Java docs: https://runeautomation.com/docs/sdk/javadocs/index.html?overview-summary.html

      Kotlin docs: https://runeautomation.com/docs/sdk/kdocs/index.html

       

      Backwards compatibility:

      We will be following a deprecation schedule whenever we perform a possible breaking change. We will deprecate the respective methods or classes and announce it in a topic. It will remain deprecated for some period of time (weeks or months), and then removed. This will give you time to fix anything, if we need to make a breaking change.

       

      Gradle template:

      Easily build scripts with a new pre-configured scripting gradle template

       

      Users of the current API:

      There is no plans to remove TRiBot API. It will still be available. However, non-critical bugs probably won't be fixed. The SDK does depend on some of the API so fixing some things in the SDK will indirectly fix the API. However, bugs that have existed in the API for awhile will likely not be fixed. It's recommended to use the SDK moving forward. Let us know if there's something the SDK is missing.

       

      Brief overview of the changes:

       

      Bug reports:

      Post bug reports in the bug reports section of the forums

       

       

      Let us know what your thoughts are! If you have questions, feel free to ask below or in discord.

       
      • 0 replies
    • Support for the gradle launcher is being dropped. Read more about the new launcher here.
      • 8 replies
    • What to expect from TRiBot moving forward.
      • 11 replies
    • TRiBot 12 Release Candidate

      The TRiBot team has been hard at work creating the last major version of TRiBot before the TRiBot X release. We've noticed many problems with TRiBot 11 with a lot of users preferring TRiBot 10 over 11. We've heard you, so we took TRiBot 10, added the new features introduced with 11, introduced some other new things, and created TRiBot 12. So without further adieu, here's TRiBot 12.
        • Like
      • 40 replies
    • 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.
      • 13 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...