Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Deltac0

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

22 posts in this topic

★★★ 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
7 people like this

Share this post


Link to post
Share on other sites

Thank you for this, very helpful for some of the newer scripts. Also your graphics are very nicely done. :)

Share this post


Link to post
Share on other sites

Thank you for this, very helpful for some of the newer scripts. Also your graphics are very nicely done. :)

 

Best tutorial I've seen so far! Keep up the good work bro!

 

Thanks! :) I will add antialiasing and the mouse-paint section today.

Share this post


Link to post
Share on other sites

Thanks! :) I will add antialiasing and the mouse-paint section today.

Would I do this exact thing for this paint?

eom2qaO.png

Share this post


Link to post
Share on other sites

Would I do this exact thing for this paint?

eom2qaO.png

Yes, you can use that image with this tutorial.

Actually this tutorial is perfect for your paint. :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Good job, well written a lot of detail put into this.

 

Thanks! Awesome to get positive feedback from such a high-ranked member ;)

Share this post


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

Share this post


Link to post
Share on other sites

Great tut, i'm having a problem with @Override though, it says 'The annotation @Override is disallowed for this location. Any ideas?

Share this post


Link to post
Share on other sites

Great tut, i'm having a problem with @Override though, it says 'The annotation @Override is disallowed for this location. Any ideas?

 

Make sure you implement Painting

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Excellent guide. I was expecting it to be more difficult, and it certainly would have been without this. Thank you for making it easy!

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.