Jump to content

Welcome to DreamBot!

Download for Free

Supercharge Your Bots

Run unlimited bots today using DreamBot's Covert Mode and
stay more protected.

Upgrade Now
Frequently Asked Questions
  • Are you not able to open the client? Make sure you have Java 8 installed
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
OSRS Gambling

Interested in advertising your business? Reach out today!

Download the DreamBot client today!
Pug

A Pug Tutorial - All About Making The Best Paint

Recommended Posts

title_fw.png


 

Welcome to my first tutorial. I've seen quite alot of questions round the forums as of late regarding paints and little miss-haps members are having. I'd like to think paints for my script have always been pretty snazzy and informative. Now its time to make everyone's paint a bit more special. I'l be covering some methods that have since been included into the API but i like to calculate them myself as it keeps you're brain active and is generally good coding practice to do as much as you can yourself. All my tutorials are given in laymens terms to help those with no experience.

 

Area's Covered In This Tutorial:

 

Script Running Time

 


So where to start, well maybe you just want to show the time your script runs for?

First we need two variables. One to tell us when the script started. We will call thistimeBegan.The other variable will be to tell us how long the script has ran for. We shall call thistimeRan.First we need to declare these two variables at the top of our script like so:



private long timeBegan;
private long timeRan;


ok so now that is out of the way we need to assign values to these variables. Lets start withtimeBeganfirst. In your onStart() method we need to tell the script that the current time needs to equal our variable so:



timeBegan = System.currentTimeMillis();


The code above essentially recording the start time of the script. Now we need to do a short calculation to work out how long our script has been running. It is as simple as the current time minus the time when the script was started. We then store this value in the variable we made earlier calledtimeRanThis will then constantly update as time progresses. Put this at the top of your onPaint() method:

 

timeRan = System.currentTimeMillis() - this.timeBegan;

Now we need to display out timeRan variable. To do this we need to use java's drawstring method. We also need to format the time into human readable time. If you didnt the time ran would show in milliseconds and that would get majorly confusing. Sounderyour onPaint() method put this other method:



private String ft(long duration)
{
String res = "";
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(duration));
if (days == 0) {
res = (hours + ":" + minutes + ":" + seconds);
} else {
res = (days + ":" + hours + ":" + minutes + ":" + seconds);
}
return res;
}


the above method will format seconds, minutes & hours into seconds. After copying and pasting the above you will need to import timeUnit else you will get errors, the import should go on line 1 of your script with all the other imports you may be using.



import java.util.concurrent.TimeUnit;


Now we have our formatting method we now need to display that variable. We do this by using the code below. Add it into your onPaint() method:



g.drawString(ft(timeRan), 1, 1);


Lets look at what we have above for a second so that you understand it. The two letters ft are relating to the formatting method above. So you are saying format(timeRan). You then have two numbers after that, 1 and 1. these relate to X and Y on the screen. you can find these values by turning on the ' Mourse Position Debug' in the client settings.

So now you can display the time your script has ran for, congratulations.

 

Example of the above tutorial:http://pastebin.com/46zGh9Gp


 

Xp Gained

 


Ok so now we have time down lets look at something more interesting, how to work out the xp gained for what ever skill you are training. For this we again need to start with three variables, the first gives us the beginning xp of the skill we are training, we shall call thisbeginningXpwe also need a variable to tell us the up to date xp of that skill. We shall call thiscurrentXp. Lastly we need a variable to show the xp that has been gained, we shall call thisxpGainedDeclare them in your variables at the start of the script like so:



private int beginningXP;
private int currentXp;
private int xpGained;


Ok so now like we did with time we need assign the starting xp of our skill to the variablebeginningXp. We do this by accessing the skill class within the API. The skill class is a really useful addition to our API and i really recommend playing around with it as it can tell you alot about the player. So, we in your onStart() method put the following:



beginningXp = getSkills().getExperience(Skill.RUNECRAFTING);


You can replace 'RUNECRAFTING' with what ever skill you are training. Also notice that 'skills' and 'Skill' are different classes which do different things. 'skills is a class which can be used to do such things as get the experince until a level, get the exp, get the level, hover over the skill and numerous other things. 'Skill' is to identify a particular skill such as ATTACK or HITPOINTS. Please also note using the Skill class will require the import as follows:



import org.dreambot.api.methods.skills.Skill;


Ok so now we need another short calculation in our onPaint() method to work out our current xp like so:



currentXp = getSkills().getExperience(Skill.RUNECRAFTING);


the above goes in your onPaint() method just so we are crystal clear. So now we have the xp we started with, and we have the xp which we currently have. We now need to minus one against the other and put it in a drawstring. So first we take one from the other. Put this next bit below our current xp calcluation.

 



xpGained = currentXp - beginningXp;


Ok so we have the xp gained equals the current xp minus the beginning xp. Simple yes? good. Now we need to display the variablexpGainedin a drawstring. Put the next bit below the last calculation or below your time ran drawstring if you did that bit to.



g.drawString("" + xpGained, 1, 1);


Above you can see that we have two speech marks. Between these you can put text such as "Xp Gained: " but as i use a image i include the text in that image and its therefore not needed in the drawstring. We also have the xpGained variable in there and also the 1 and 1 again. These as i said above relate to X and Y on screen. Congrats you have xp gained.

 

Example of the above tutorial:http://pastebin.com/kHeAn4PC

 


 

Showing Levels

 


 

Ok so now we have a half ok paint. So what about if we want to show our current level in a skill, or the beginning level or how many levels we have gained. Welll i will show all three of these now. So lets start off with some more variables, yay i hear you say. Ok so we need three more variables which you should put at the top of script where the others have gone before. The first is for showing your beginning level, we shall call this ironically enoughbeginningLevel. Next we need a variable to show the current level, guess what this one is called? yep,currentLevel. Lastly we need a variable to show how many levels we have gained, we shall call this onelevelsGained. So declare these like so: (you should now be able to be skipping ahead slightly and declaring them on your own)



private int currentLevel;
private int beginningLevel;
private int levelsGained;


Right so now we need to grab the level we had on starting the script much like we did with the Xp calculations, this is similar, so in your onStart() method put:



beginningLevel = getSkills().getRealLevel(Skill.RUNECRAFTING);


You will notice that we are using the 'skills' class once again expect this time we are using getStatic. I found this odd at first as it didnt make sense but you will get used to it. Then you have once again 'Skill'. And again i have used an example of RUNECRAFTING though you will obviously change it to what skill you are wanting the level for.

So far we have declared our variables and we have our beginning level. We now need to need to go back into our onPaint() method and assign ourcurrentLevela value like so:



currentLevel = getSkills().getRealLevel(Skill.RUNECRAFTING);


Again to be crystal clear the above code goes into your onPaint() method. It's exacttly the same code as ourbeginningLevelvariable, the only difference being that this code is called over and over again where as the when we used it in the onStart() method it will only be called once.

Now we have outbeginningLeveland ourcurrentLevel. You can display these in your onPaint() method if you wish like so:



g.drawString("" + beginningLevel, 1,1);
g.drawString("" + currentLevel, 1,1);


Like i explained above the 1 and 1 stand for X & Y co-ordinates on screen, Now if you want to display how many levels you have gained we will have to do one more simple calculation. That's going to be your current level minus your beginning level. The code below should go above the drawstrings & below to where we assigned values tocurrentLevel



levelsGained = currentLevel - beginningLevel;


Lastly we display the value oflevelsGainedin our paint, by doing the following:



g.drawString("" + levelsGained, 1, 1);


That's it, you should now have the ability to display your levels in a way that suits you. Some people like to display the levels in a way such as:

1 / 5 ( +4 )

beginning level / current level ( + 4 levels )

 

Example of the above tutorial:http://pastebin.com/qp6iHfk1


 

Adding Gold Made

 


Ok so this is going to be quite an important one i think, how to calculate your profits and display them correctly. So first thing is first lets declare some more variables. Before we do though this could get a lot more complicated than its going to. I say this because if you for example are runecrafting then you will obviously need to take into account the cost of ess as well which in fact brings your profits down to what they should be. But for the purpose of this particular segment il be keeping it at the basics. We are going to have four variables to declare, the first beingcostOfItem. You can get this by grabbing the live price from zybez which i will show you how to do in another section of this tutorial. We will also needitemsMadewhether this be logs, runes, bows, fish, etc.Thirdly we will be needinggpGained, which will be used to store the value of a calculation. Lastly we needtotalGpGained, il explain the use of this later. So declare the above variables at the top of your script like so:



private int costOfItem;
private int itemsMade;
private double gpGained;
private double totalGpGained;


So now we have those, we are going to assign a value tocostOfItem, and like i said above in another section i will show you how to grab the live price but for now we are going to go ahead and assign a random value of 500. So put the following into your onStart() method:



costOfItem = 500;


So now we have the price of our item but we dont know how many items we have fished, cut, cooked, made. To work this out you would have the following code either in your onMessage() method where by every time the client detects "You cut a log" you add one to the variableItemsMadeor you would add one to the same variable each time you were inside for example the crafting method of your runecrafting script. Either way the code for that is as follows:



itemsMade += 1;


if you have more than 1 item gained at a time, for example 28 runes per trip, but want it to be accurate you could also use:



itemsMade += inventory.getAmount("your Item");


So now we have how many items have been made and how much said item costs. We can now work out the profits! yay. Ok so we will use our variablegpGainedto store the value ofitemsMademultiplied by the value oritemCost. this is displayed as the following in your onPaint() method:



gpGained = itemsMade - costOfItem;


Ok so we are making good progress, we have worked out how much gold has been made but as it is at the moment its going to be displayed something like this:Profit Made: 5634545You dont really want all those annoying numbers not formatted at all do we. So first we are going to dividegpGainedby one thousand to give us (using the above example 5634545) would be5634.545k instead, thats slightly better no? so copy the following code into your onPaint() method and put it below the above code you copied:



totalGpGained = gpGained / 1000;


Finally we shall be formatting the end result to remove the numbers after the decimal point (using the above example it will then display as 5634k which is what we are after) So copy the following code into your onPaint() method below the above code:



DecimalFormat df = new DecimalFormat("#");
g.drawString(" "+ df.format(totalGpGained) + " ", 1,1);


Ok so let me just take a minute to explain the above code so your not like what the actual fuck. Do we have 'DecialFormat' this is doing to remove sections of a integer we dont want, we do this by using the # you see in the top line. So putting just the one # will mean that the formatting will remove everything after the decimal point. If you wanted to display an integer to one decimal point you would put #.# two decimal places would be #.##

So you get the picture. Moving onto the second line we have our drawString again, we then have df.format which is refering to line one where you see where 'df' has been declared much like you would a variable. the part between the brackets in this case totalGpGained is whats going to be formatted. We then have a text string " k" and your 1 and 1 which are the X and Y co-ordinates again.

We now have our formatted total gold made, congratulations.

 

Example of the above tutorial:http://pastebin.com/iZvDLcr9


 

Adding Gold Made Per Hour

 


So to follow on from the above segment we are going to work out profit made per hour. I obviously recommend reading the above section on working out Gold Made before using this section. Ok so first we need to declare another two variables. the first isgpPerHourwhich is going to store the value of the current time minus when the script started which we then divide bygpGainedand lastly divide that by3600000. A little complicated i know but once you see it below it will be easier to understand. The second variable istotalGpPerHour, this is going to be the value ofgpPerHourdivided by 1000. So put the following at the top of your script where your variables have been going:



private int gpPerHour;
private int totalGpPerHour;


Next we need to put the following into your onPaint() method above the formatting segment:



gpPerHour = (int)(gpGained / ((System.currentTimeMillis() - timeBegan) / 3600000.0D));


This is the long calculation i spoke of above. I hope it is now slightly more clear than it was when i explained it, if not dont worry alls you need to know is that calculation gives you the gp made per hour in raw un-formatted numbers. You also need to have the timeBegan in your code from the first segment of this tutorial when we worked out how to display the amount of time our script has been running. We will now go on to format that value as we did in the previous segment. So first we want to dividegpPerHourby 1000. Copy and paste the following into your onPaint() method below the previousgpPerHourcalculation:



totalGpPerHour = gpPerHour / 1000;


Lastly we can now remove the numbers after the decimal place and input that value into our drawString, place the following into your onPaint method below the last drawString of TotalGpMade:



g.drawString("" + df.format(totalGpPerHour) + "", 1, 1);


Note you dont need to declare the df.format part again as its been used in the last segment. Your paint should now display gold made per hour correctly. Congrats.

 

Example of the above tutorial:http://pastebin.com/VNWsULHH


 

Adding a background Image

 


Ok so now we have the basics id like to add a little more depth to the tutorial. We have some numbers displayed and basic stats about our script so now we need a snazzy background image to go behind this text. So your first port of call is one of two places. The image editor of your choice (Photoshop, fireworks, paint, illustrator to name but a few) or you can go to the graphics section of the forum and request a paint background image for yourself. Alot of our members will be more than happy to make one for you im sure. Now i like to make my own because i can then tweak it at my own leisure. So lets start off basic and say we have our background image done in paint its a nice colour and you have the font all set out far enough apart that you can put the text next to it via our friend drawString.

 

An example would be something like this:

image.png

Above you can see my own paint which i made a few months ago. I achieved this by starting from the back with the meshy grey colour then making the border or orange with black glow and then started adding in the text. Anyway, so you have your image done. First thing is first we need to upload this image to a image hosting website online. I use www.postimg.org because its free and reliable. So upload your image and get the direct link to it.

We will first need to import the image from the net to our script so we assign the image to a variable. Put this at the top where your other variables have been going:



private final Image bg = getImage("www.linkToYourImage.com/image.png");


After pasting this in you will get an error but dont worry its because we havent added a method to grab the image from the internet yet. Add the following method to the script below your onPaint() method. Note i said below not inside it. Make sure its still inside the last } though.


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

Ok so now we have the method to grab images from the internet and we have our image stored in a variable ready to be shown. Now we just have to put this variable into use in our onPaint() method. So add the following line into your onPaint() method:



g.drawImage(bg, 1, 1, null);


Looking at the above code we have 'bg' which is out variable that holds the image, then we have once again 1 and 1 which are the X and Y co-ordinates on screen where you want your picture to be displayed note that the x and y should be the top left corner of the image. After you have that you are done it really was that simple. Congrats, check out the sample code below to make sure you have everything in the right place.

 

Example of the above tutorial:http://pastebin.com/gynBYzLc

 


 

Percent To Next Level

 


 

Now this is where those of you that have never really scripted before may start to struggle. This is going to get a little technical and long so if you are ready lets go. So first like the other segments we are going to declare some new variables. Lets declare those at the top of your script:



private int currentXp;
private int currentLevel;
private double currentLevelXp;
private double nextLevelXp;
private double percentTNL;


Some of these you will have seen in previous segments, others maybe not.CurrentXp&currentLevelare self explanatory,currentLevelXpis new to you. We will assign a value to this variable from a XP table which i will provide shortly. We also havenextLevelXpwhich is again going to have a value assigned from the XP table. Lastly we have our end goal variablepercentTNL. this is going to be a value from 1-100.

As i said above here is the XP Table:



final int[] XP_TABLE =
{
 0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154,
 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018,
 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833,
 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721,
 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254,
 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428,
 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895,
 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068,
 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294,
 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614,
 8771558, 9684577, 10692629, 11805606, 13034431, 200000000
};


put the above method below your variables and above the onStart() method. The xp table probably isnt needed anymore im not sure but i like it. Basically what it does is provide all the xp for each level 1-99. This will help us calculate percent until the next level. Once we have these things we can start assigning values to the variables above. First iscurrentXpput this at the top of your onPaint() method:



currentXp = getSkills().getExperience(Skill.RUNECRAFTING);


Next we need to assign a value tocurrentLevel, i have used the RUNECRAFTING skill as an example but again you dont have to, you would apply the skill that suits your needs, so put this under the above code ^



currentLevel = getSkills().getStatic(Skill.RUNECRAFTING);


Now we are going to grab data from the XP table for our current level, i have used the RUNECRAFTING skill as an example but again you dont have to, you would apply the skill that suits your needsput this under the above code ^



currentLevelXp = XP_TABLE[currentLevel];


if you wish to use the dreambot api code please use the following instead of the above to work out the current level xp:



currentLevelXp = getSkills().getExperienceForLevel(currentLevel);


Now to get the xp for the next level, put this under the above code ^



nextLevelXp = XP_TABLE[currentLevel + 1];


Again if you wish to use the dreambot api please use the following instead of the above to work out the next levels xp:



nextLevelXp = getSkills().getExperienceForLevel(currentLevel +1);


Now we can make the calculation to get percent until next level, put this under the above code ^



percentTNL = ((currentXp - currentLevelXp) / (nextLevelXp - currentLevelXp) * 100);


Great we have our percentTNL value. Now we need to format that to one decimal place and then display it on screen using our drawString. So put the following under the above code ^



DecimalFormat df = new DecimalFormat("#.#");
g.drawString("" + df.format(percentTNL), 1, 1);


Ok so if you have followed the above segments you will already be familiar with the formatting code. I have chosen one decimal place but that is up to you, maybe you will just want 70% instead of 70.5% The choice is yours. Well that was alot easier to explain than i thought it would be. Congrats if you stuck with it.

 


 

Time Until Next Level

 


OK so now we are beginning to get into the segments of this tutorial where there is alot of variables required to finish the calculation i apologise in advance haha. So first we need to once again declare some more variables. Like so:



private long timeBegan;
private int xpGained;
private int xpPerHour;
private int currentXp;
private int currentLevel;
private int beginningXp;
private double nextLevelXp;
private double xpTillNextLevel;
private long timeTNL;


Thanks to good naming i believe all the variables above are easily read and understood if you have got this far. I will go through each one in more details as we assign values. We will again be needing the XP table. Put the following code below the variables above:



 final int[] XP_TABLE =
 {
 0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154,
 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018,
 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833,
 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721,
 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254,
 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428,
 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895,
 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068,
 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294,
 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614,
 8771558, 9684577, 10692629, 11805606, 13034431, 200000000
 };


We can now start to assign values to our variables, firstly lets assign the current time when starting the script totimeBegan. Put the following code in your onStart() method:



timeBegan = System.currentTimeMillis();


Next we assign beginningXp a value by using the following code. Put it below the above code ^ in your onStart() method



beginningXp = getSkills().getExperience(Skill.RUNECRAFTING);


we have to assign a value of zero to timeTNL on start also so that you dont get and bunch of random number upon starting your script, put the following code below the above code in your onStart() method:



timeTNL = 0;


Now we have the variables set onStart() we can proceed to fill out the rest that go in the onPaint() method. First we assign the current xp for your chosen skill, I have chosen RUNECRAFTING but of course you can change that to what you need. put this in your onPaint() method:



currentXp = getSkills().getExperience(Skill.RUNECRAFTING);


next we assign the current level, put the following below the above code ^



currentLevel = getSkills().getRealLevel(Skill.RUNECRAFTING);


next we work out the xpGained by subtracting the beginning xp from the current xp, put the following below the above code ^



xpGained = currentXp - beginningXp;


We also need a xp per hour calculation which is very similar to the gpPerHour which we used in a previous segment, put the following code in your onPaint() method below the above code ^



xpPerHour = (int)( xpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D));


Next we need to assign a value to the variable nextLevelXp. We get this from the xp table once again, put the following code below the code above ^



nextLevelXp = XP_TABLE[currentLevel + 1];


If you wish to use the dreambot api instead of the code above please use the following to work out next level xp:



nextLevelXp = getSkills().getExperienceForLevel(currentLevel +1);


We also need the xp until the next level, which is a key part of the final calculation, put the following below the above code ^



xpTillNextLevel = nextLevelXp - currentXp;


Now we can work out the value of time until the next level. First we need to add a small if statement into our onPaint method to stop the script churning out a screen full of random numbers, put the following below the above code:



if (xpGained >= 1)
{
timeTNL = (long) ((xpTillNextLevel / xpPerHour) * 3600000);
}


Now we can display our time until next level with time formatting, first you will need to put the following method after your onPaint() method if you havent added it already :



private String ft(long duration)
{
String res = "";
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(duration));
if (days == 0) {
res = (hours + ":" + minutes + ":" + seconds);
} else {
res = (days + ":" + hours + ":" + minutes + ":" + seconds);
}
return res;
}


Now to display the time till next level in our drawString, put this within your onPaint method:



g.drawString("" + ft(timeTNL), 1,1);


And there we go you have done it, abit long winded i know but worth it congratulations.


 

% Progress Bar

 


A progress bar for those who are unsure if they are looking for the correct thing or for those who dont know is a coloured bar that grows with each exp gained until it reaches full at 100% They are usually added to show how close to the next level you are without showing numbers or perhaps to add that something a little extra special. So here i'm going to show you how to add this.

We will be using percent to next level calculated in the previous segment of this tutorial so please do that first if you wish to use this.

First you need to work out how big the width of your bar is going to be. Mine in this example is 337 pixels in length & will be full when it reaches 100% So copy the following code into your onPaint method below the calculations for percent to next level and before the g.drawstrings begin.



double width = (337 * (percentTNL / 100));


Let me explain the above. We declare a double variable because thats what we used to work out our percentage. We then label our new variable width and then do the calculation percent to next level divided by 100, the percentage our bar will be when full. Then divde that value by the total length of the bar which in our example is 337 pixels long. Next we want to make that value an integer so copy the following below:



int widdth = (int) width;


This now coverts the double into an int. Much easier to work with when displaying rectangles. Next we want to display out growing rectangle on the screen so put this above the first g.drawstring:



g.fillRect(169,427, widdth, 20);


The above is simply saying positon on the screen of X so 169 pixels from the left edge of the screen horizontally. Then the Y value the height on which the screen it will be displayed which is 427. Then we have our changing variable widdth which changes as our percent increases. Then lastly you have the height of the bar which is 20, you can adjust this to fit it in with your paint.

Thats it, the most complicated calculation you will have in your paint in three simple steps!


 

Changing Font Colors

 


This is actually quite easy to do, but i remember people asking about it so it shall be included here! First we want to declare our new color. This can be done by pasting the following above any g.drawString or g.fillRect that you want to be this color:



Color orange = new Color(254, 127, 0);


As you see above we are declaring the color variable and then you see three numbers. These are red, green & blue (RGB) values. You can use photoshop/fireworks/paint to grab these values of the color you want. After declaring our color, below you need to set the new color when you want to use it. Any g.drawStrings or g.fillRect's you put below this set function will appear in the desired color. to set another color as well masybe for a different part of the paint, you will need to declare a different color and set it the other code. I will provide an example at the bottom of this section with explained code. Anyway paste this below the color declaration and above the g.drawString you wish to color:



g.setColor(orange);


Here is an example of using multiple colors:



Color orange = new Color(254, 127, 0); // declare orange color
Color blue = new Color(123, 456, 0); // declare blue color
g.setColor(orange); // set orange color
g.drawString("text coloured orange"), 317, 442); // text here will appear orange
g.drawString("text coloured orange"), 317, 462); // text here will appear orange
g.setColor(blue); // set blue color
g.drawString("text coloured blue"), 317, 482); // text here will appear blue
g.drawString("text coloured blue"), 317, 502); // text here will appear blue


Hope you find this little nugget helpful!


 

Hide paint button

 


Ok so this is really useful shit, especially when you see someone talking to you and you cant see shit because the paint is in the way. No worries click dat hide button and boom, talk away. You could even get fancy and display the paint elsewhere when the button is pressed but in this example il just hide it. First we need to declare some variables that will help us to do what we need. So copy the following into your variable declarations at the top of your script:



Point p;
boolean hide = false;
Rectangle close = new Rectangle(10, 457, 118, 20);
Rectangle open = new Rectangle(10, 457, 118, 20);


Ok so we have p, this is the variable for our mouse event. When then mouse is clicked, the location value will be stored in p. We also have a boolean hide. Booleans are yes or no. 1 or 0, true or false for those noobs out there. We shall be using it as true or false and settings it as false to begin with. Next we have two rectangle declarations. These will be the box's in which if clicked, your paint will hide or open.

Next we need to set up our mouse event. So that we can detect if the mouse is clicked or not. And if its within our box's. Paste the following code below before the LAST curly } bracket.



public void onMouse(MouseEvent e)
{
p = e.getPoint();
if (close.contains(p) && !hide) {
hide = true;
} else if (open.contains(p) && hide) {
hide = false;
}
}


Have a look at the code above a little. So we are making a method which says, If the mouse is clicked within our close box and the paint is not hidden, then set our hide boolean to true. Else if the mouse is clicked within our open box and the paint is hidden, then set our hide boolean to false. Its effectively an on/off switch.

Next we can set what we want to display when the paint is on & off. So use the below code as an example and i shall explain below:



public void onPaint(Graphics g)
{
if (!hide)
{
g.drawImage(this.hideImage, 10, 457, null);
}
if(hide)
{
g.drawImage(this.showImage, 10, 457, null);
}
}


Ok so above have a look at the code. We have a basic on paint method. Inside it we have two if statements. One for when the paint is displayed which is !hide and the other for when the paint is hidden 'hide' inside these you will have what you wish to display when the paint is hidden or showing, but please pay attention to the X & Y values of the images. They must align with the X & Y values we set for a our rectangles open & close. If you have a rectangle on screen that isnt visible via an image, how is your user going to know where to click?

I understand this may be a bit confusing so if anyone has questions feel free to post them here.

 

please find an example of the above tutorial here: http://pastebin.com/L2yyJ6C7


 

 

If you want any more please post here

 

Like this post if it helped

Edited by Pug

Share this post


Link to post
Share on other sites

I suggest taking a look at http://dreambot.org/javadocs/org/dreambot/api/methods/skills/SkillTracker.html and checking out Timer.formatTime(timeRan); Helps a lot for simplification and less lines. I can share my progress bar method too if you want it.

have it already trying to get it all typed up :) tutorial is from my old osbot tutorial. Plus its better to type in full especially when learning to program. To many kids copy n pasting these days not knowing what it does Edited by Pug

Share this post


Link to post
Share on other sites

 

title_fw.png

 

Welcome to my first tutorial. I've seen quite alot of questions round the forums as of late regarding paints and little miss-haps members are having. I'd like to think paints for my script have always been pretty snazzy and informative. Now its time to make everyone's paint a bit more special. I'l be covering some methods that have since been included into the API but i like to calculate them myself as it keeps you're brain active and is generally good coding practice to do as much as you can yourself. All my tutorials are given in laymens terms to help those with no experience.

 

Area's Covered In This Tutorial:

 

Script Running Time

 

 

So where to start, well maybe you just want to show the time your script runs for?

First we need two variables. One to tell us when the script started. We will call this timeBegan. The other variable will be to tell us how long the script has ran for. We shall call this timeRan. First we need to declare these two variables at the top of our script like so:

private long timeBegan;
private long timeRan;

ok so now that is out of the way we need to assign values to these variables. Lets start with timeBegan first. In your onStart() method we need to tell the script that the current time needs to equal our variable so:

timeBegan = System.currentTimeMillis();

The code above essentially recording the start time of the script. Now we need to do a short calculation to work out how long our script has been running. It is as simple as the current time minus the time when the script was started. We then store this value in the variable we made earlier called timeRan This will then constantly update as time progresses. Put this at the top of your onPaint() method:

timeRan = System.currentTimeMillis() - this.timeBegan; 

Now we need to display out timeRan variable. To do this we need to use java's drawstring method. We also need to format the time into human readable time. If you didnt the time ran would show in milliseconds and that would get majorly confusing. So under your onPaint() method put this other method:

private String ft(long duration)
        {
            String res = "";
            long days = TimeUnit.MILLISECONDS.toDays(duration);
            long hours = TimeUnit.MILLISECONDS.toHours(duration)
            - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
            long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
            .toHours(duration));
            long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
            .toMinutes(duration));
            if (days == 0) {
            res = (hours + ":" + minutes + ":" + seconds);
            } else {
            res = (days + ":" + hours + ":" + minutes + ":" + seconds);
            }
            return res;
        } 

the above method will format seconds, minutes & hours into seconds. After copying and pasting the above you will need to import timeUnit else you will get errors, the import should go on line 1 of your script with all the other imports you may be using.

import java.util.concurrent.TimeUnit; 

Now we have our formatting method we now need to display that variable. We do this by using the code below. Add it into your onPaint() method:

g.drawString(ft(timeRan), 1, 1); 

Lets look at what we have above for a second so that you understand it. The two letters ft are relating to the formatting method above. So you are saying format(timeRan). You then have two numbers after that, 1 and 1. these relate to X and Y on the screen. you can find these values by turning on the ' Mourse Position Debug' in the client settings.

So now you can display the time your script has ran for, congratulations.

 

 

 

Xp Gained

 

 

Ok so now we have time down lets look at something more interesting, how to work out the xp gained for what ever skill you are training. For this we again need to start with three variables, the first gives us the beginning xp of the skill we are training, we shall call this beginningXp we also need a variable to tell us the up to date xp of that skill. We shall call this currentXp. Lastly we need a variable to show the xp that has been gained, we shall call this xpGained Declare them in your variables at the start of the script like so:

private int beginningXP;
private int currentXp;
private int xpGained;

Ok so now like we did with time we need assign the starting xp of our skill to the variable beginningXp. We do this by accessing the skill class within the API. The skill class is a really useful addition to our API and i really recommend playing around with it as it can tell you alot about the player. So, we in your onStart() method put the following:

beginningXp = getSkills().getExperience(Skill.RUNECRAFTING); 

You can replace 'RUNECRAFTING' with what ever skill you are training. Also notice that 'skills' and 'Skill' are different classes which do different things. 'skills is a class which can be used to do such things as get the experince until a level, get the exp, get the level, hover over the skill and numerous other things. 'Skill' is to identify a particular skill such as ATTACK or HITPOINTS. Please also note using the Skill class will require the import as follows:

import org.dreambot.api.methods.skills.Skill;

Ok so now we need another short calculation in our onPaint() method to work out our current xp like so:

currentXp = getSkills().getExperience(Skill.RUNECRAFTING); 

the above goes in your onPaint() method just so we are crystal clear. So now we have the xp we started with, and we have the xp which we currently have. We now need to minus one against the other and put it in a drawstring. So first we take one from the other. Put this next bit below our current xp calcluation.

xpGained = currentXp - beginningXp; 

Ok so we have the xp gained equals the current xp minus the beginning xp. Simple yes? good. Now we need to display the variable xpGained in a drawstring. Put the next bit below the last calculation or below your time ran drawstring if you did that bit to.

g.drawString("" + xpGained, 1, 1); 

Above you can see that we have two speech marks. Between these you can put text such as "Xp Gained: " but as i use a image i include the text in that image and its therefore not needed in the drawstring. We also have the xpGained variable in there and also the 1 and 1 again. These as i said above relate to X and Y on screen. Congrats you have xp gained.

 

 

 

Showing Levels

 

 

Ok so now we have a half ok paint. So what about if we want to show our current level in a skill, or the beginning level or how many levels we have gained. Welll i will show all three of these now. So lets start off with some more variables, yay i hear you say. Ok so we need three more variables which you should put at the top of script where the others have gone before. The first is for showing your beginning level, we shall call this ironically enough beginningLevel. Next we need a variable to show the current level, guess what this one is called? yep, currentLevel. Lastly we need a variable to show how many levels we have gained, we shall call this one levelsGained. So declare these like so: (you should now be able to be skipping ahead slightly and declaring them on your own) 

private int currentLevel;
private int beginningLevel;
private int levelsGained;

Right so now we need to grab the level we had on starting the script much like we did with the Xp calculations, this is similar, so in your onStart() method put:

beginningLevel = getSkills().getRealLevel(Skill.RUNECRAFTING);

You will notice that we are using the 'skills' class once again expect this time we are using getStatic. I found this odd at first as it didnt make sense but you will get used to it. Then you have once again 'Skill'. And again i have used an example of RUNECRAFTING though you will obviously change it to what skill you are wanting the level for.

So far we have declared our variables and we have our beginning level. We now need to need to go back into our onPaint() method and assign our currentLevel a value like so:

currentLevel = grtSkills().getRealLevel(Skill.RUNECRAFTING);

Again to be crystal clear the above code goes into your onPaint() method. It's exacttly the same code as our beginningLevel variable, the only difference being that this code is called over and over again where as the when we used it in the onStart() method it will only be called once.

Now we have out beginningLevel and our currentLevel. You can display these in your onPaint() method if you wish like so:

g.drawString("" + beginningLevel, 1,1);
g.drawString("" + currentLevel, 1,1);

Like i explained above the 1 and 1 stand for X & Y co-ordinates on screen, Now if you want to display how many levels you have gained we will have to do one more simple calculation. That's going to be your current level minus your beginning level. The code below should go above the drawstrings & below to where we assigned values tocurrentLevel

levelsGained = currentLevel - beginningLevel;

Lastly we display the value of levelsGained in our paint, by doing the following:

g.drawString("" + levelsGained, 1, 1);

That's it, you should now have the ability to display your levels in a way that suits you. Some people like to display the levels in a way such as: 

1 / 5 ( +4 )

beginning level / current level ( + 4 levels )

 

 

 

Adding Gold Made

 

 

Ok so this is going to be quite an important one i think, how to calculate your profits and display them correctly. So first thing is first lets declare some more variables. Before we do though this could get a lot more complicated than its going to. I say this because if you for example are runecrafting then you will obviously need to take into account the cost of ess as well which in fact brings your profits down to what they should be. But for the purpose of this particular segment il be keeping it at the basics. We are going to have four variables to declare, the first being costOfItem. You can get this by grabbing the live price from zybez which i will show you how to do in another section of this tutorial. We will also need itemsMade whether this be logs, runes, bows, fish, etc.Thirdly we will be needing gpGained, which will be used to store the value of a calculation. Lastly we need totalGpGained, il explain the use of this later. So declare the above variables at the top of your script like so:

private int costOfItem;
private int itemsMade;
private double gpGained;
private double totalGpGained;

So now we have those, we are going to assign a value to costOfItem, and like i said above in another section i will show you how to grab the live price but for now we are going to go ahead and assign a random value of 500. So put the following into your onStart() method:

costOfItem = 500; 

So now we have the price of our item but we dont know how many items we have fished, cut, cooked, made. To work this out you would have the following code either in your onMessage() method where by every time the client detects "You cut a log" you add one to the variable ItemsMade or you would add one to the same variable each time you were inside for example the crafting method of your runecrafting script. Either way the code for that is as follows:

itemsMade += 1;

if you have more than 1 item gained at a time, for example 28 runes per trip, but want it to be accurate you could also use:

itemsMade += inventory.getAmount("your Item"); 

So now we have how many items have been made and how much said item costs. We can now work out the profits! yay. Ok so we will use our variable gpGained to store the value of itemsMade multiplied by the value or itemCost. this is displayed as the following in your onPaint() method:

gpGained = itemsMade - costOfItem;

Ok so we are making good progress, we have worked out how much gold has been made but as it is at the moment its going to be displayed something like this: Profit Made: 5634545 You dont really want all those annoying numbers not formatted at all do we. So first we are going to divide gpGained by one thousand to give us (using the above example 5634545) would be 5634.545k instead, thats slightly better no? so copy the following code into your onPaint() method and put it below the above code you copied:

totalGpGained = gpGained / 1000;

Finally we shall be formatting the end result to remove the numbers after the decimal point (using the above example it will then display as 5634k which is what we are after) So copy the following code into your onPaint() method below the above code:

DecimalFormat df = new DecimalFormat("#");
g.drawString("" + df.format(totalGpGained) + " k", 1,1);

Ok so let me just take a minute to explain the above code so your not like what the actual fuck. Do we have 'DecialFormat' this is doing to remove sections of a integer we dont want, we do this by using the # you see in the top line. So putting just the one # will mean that the formatting will remove everything after the decimal point. If you wanted to display an integer to one decimal point you would put #.# two decimal places would be #.##

So you get the picture. Moving onto the second line we have our drawString again, we then have df.format which is refering to line one where you see where 'df' has been declared much like you would a variable. the part between the brackets in this case totalGpGained is whats going to be formatted. We then have a text string " k" and your 1 and 1 which are the X and Y co-ordinates again.

We now have our formatted total gold made, congratulations.

 

 

 

Adding Gold Made Per Hour

 

 

So to follow on from the above segment we are going to work out profit made per hour. I obviously recommend reading the above section on working out Gold Made before using this section. Ok so first we need to declare another two variables. the first is gpPerHour which is going to store the value of the current time minus when the script started which we then divide by gpGained and lastly divide that by 3600000. A little complicated i know but once you see it below it will be easier to understand. The second variable is totalGpPerHour, this is going to be the value of gpPerHour divided by 1000. So put the following at the top of your script where your variables have been going:

private int gpPerHour;
private int totalGpPerHour;

Next we need to put the following into your onPaint() method above the formatting segment:

gpPerHour = (int)(gpGained / ((System.currentTimeMillis() - timeBegan) / 3600000.0D));

This is the long calculation i spoke of above. I hope it is now slightly more clear than it was when i explained it, if not dont worry alls you need to know is that calculation gives you the gp made per hour in raw un-formatted numbers. You also need to have the timeBegan in your code from the first segment of this tutorial when we worked out how to display the amount of time our script has been running. We will now go on to format that value as we did in the previous segment. So first we want to divide gpPerHour by 1000. Copy and paste the following into your onPaint() method below the previous gpPerHour calculation:

totalGpPerHour = gpPerHour / 1000;

Lastly we can now remove the numbers after the decimal place and input that value into our drawString, place the following into your onPaint method below the last drawString of TotalGpMade:

g.drawString("" + df.format(totalGpPerHour) + " k/hr", 1, 1);

Note you dont need to declare the df.format part again as its been used in the last segment. Your paint should now display gold made per hour correctly. Congrats.

 

 

 

Adding a background Image

 

 

Ok so now we have the basics id like to add a little more depth to the tutorial. We have some numbers displayed and basic stats about our script so now we need a snazzy background image to go behind this text. So your first port of call is one of two places. The image editor of your choice (Photoshop, fireworks, paint, illustrator to name but a few) or you can go to the graphics section of the forum and request a paint background image for yourself. Alot of our members will be more than happy to make one for you im sure. Now i like to make my own because i can then tweak it at my own leisure. So lets start off basic and say we have our background image done in paint its a nice colour and you have the font all set out far enough apart that you can put the text next to it via our friend drawString.

An example would be something like this:

      image.png

Above you can see my own paint which i made a few months ago. I achieved this by starting from the back with the meshy grey colour then making the border or orange with black glow and then started adding in the text. Anyway, so you have your image done. First thing is first we need to upload this image to a image hosting website online. I use www.postimg.org because its free and reliable. So upload your image and get the direct link to it.

We will first need to import the image from the net to our script so we assign the image to a variable. Put this at the top where your other variables have been going:

private final Image bg = getImage("www.linkToYourImage.com/image.png");

After pasting this in you will get an error but dont worry its because we havent added a method to grab the image from the internet yet. Add the following method to the script below your onPaint() method. Note i said below not inside it. Make sure its still inside the last } though wink.png

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

Ok so now we have the method to grab images from the internet and we have our image stored in a variable ready to be shown. Now we just have to put this variable into use in our onPaint() method. So add the following line into your onPaint() method:

g.drawImage(bg, 1, 1, null);

Looking at the above code we have 'bg' which is out variable that holds the image, then we have once again 1 and 1 which are the X and Y co-ordinates on screen where you want your picture to be displayed note that the x and y should be the top left corner of the image. After you have that you are done it really was that simple. Congrats, check out the sample code below to make sure you have everything in the right place.

 

 

 

Percent To Next Level

 

 

Now this is where those of you that have never really scripted before may start to struggle. This is going to get a little technical and long so if you are ready lets go. So first like the other segments we are going to declare some new variables. Lets declare those at the top of your script:

private int currentXp;
private int currentLevel;
private double currentLevelXp;
private double nextLevelXp;
private double percentTNL;

Some of these you will have seen in previous segments, others maybe not. CurrentXp & currentLevel are self explanatory, currentLevelXp is new to you. We will assign a value to this variable from a XP table which i will provide shortly. We also have nextLevelXp which is again going to have a value assigned from the XP table. Lastly we have our end goal variable percentTNL. this is going to be a value from 1-100.

As i said above here is the XP Table:

    final int[] XP_TABLE =
    {
             0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154,
     1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018,
     5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833,
     16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
     41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721,
     101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254,
     224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428,
     496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895,
     1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068,
     2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294,
     4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614,
     8771558, 9684577, 10692629, 11805606, 13034431, 200000000
    };

put the above method below your variables and above the onStart() method. The xp table probably isnt needed anymore im not sure but i like it. Basically what it does is provide all the xp for each level 1-99. This will help us calculate percent until the next level. Once we have these things we can start assigning values to the variables above. First is currentXp put this at the top of your onPaint() method:

currentXp = getSkills().getExperience(Skill.RUNECRAFTING); 

Next we need to assign a value to currentLevel, i have used the RUNECRAFTING skill as an example but again you dont have to, you would apply the skill that suits your needs, so put this under the above code ^

currentLevel = getSkills().getStatic(Skill.RUNECRAFTING); 

Now we are going to grab data from the XP table for our current level, i have used the RUNECRAFTING skill as an example but again you dont have to, you would apply the skill that suits your needs put this under the above code ^

currentLevelXp = XP_TABLE[currentLevel]; 

if you wish to use the dreambot api code please use the following instead of the above to work out the current level xp:

currentLevelXp = getSkills().getExperienceForLevel(currentLevel);

Now to get the xp for the next level, put this under the above code ^

nextLevelXp = XP_TABLE[currentLevel + 1]; 

Again if you wish to use the dreambot api please use the following instead of the above to work out the next levels xp:

nextLevelXp = getSkills().getExperienceForLevel(currentLevel +1);

Now we can make the calculation to get percent until next level, put this under the above code ^

percentTNL = ((currentXp - currentLevelXp) / (nextLevelXp - currentLevelXp) * 100); 

Great we have our percentTNL value. Now we need to format that to one decimal place and then display it on screen using our drawString. So put the following under the above code ^

DecimalFormat df = new DecimalFormat("#.#");
g.drawString("" + df.format(percentTNL), 1, 1); 

Ok so if you have followed the above segments you will already be familiar with the formatting code. I have chosen one decimal place but that is up to you, maybe you will just want 70% instead of 70.5% The choice is yours. Well that was alot easier to explain than i thought it would be. Congrats if you stuck with it.

 

 

 

Time Until Next Level

 

 

OK so now we are beginning to get into the segments of this tutorial where there is alot of variables required to finish the calculation i apologise in advance haha. So first we need to once again declare some more variables. Like so:

private long timeBegan;
private int xpGained;
private int xpPerHour;
private int currentXp;
private int currentLevel;
private int beginningXp;
private double nextLevelXp;
private double xpTillNextLevel;
private long timeTNL;

Thanks to good naming i believe all the variables above are easily read and understood if you have got this far. I will go through each one in more details as we assign values. We will again be needing the XP table. Put the following code below the variables above:

     final int[] XP_TABLE =
         {
             0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154,
     1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018,
     5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833,
     16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
     41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721,
     101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254,
     224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428,
     496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895,
     1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068,
     2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294,
     4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614,
     8771558, 9684577, 10692629, 11805606, 13034431, 200000000
         };

We can now start to assign values to our variables, firstly lets assign the current time when starting the script to timeBegan. Put the following code in your onStart() method:

timeBegan = System.currentTimeMillis(); 

Next we assign beginningXp a value by using the following code. Put it below the above code ^ in your onStart() method

beginningXp = getSkills().getExperience(Skill.RUNECRAFTING); 

we have to assign a value of zero to timeTNL on start also so that you dont get and bunch of random number upon starting your script, put the following code below the above code in your onStart() method:

timeTNL = 0; 

Now we have the variables set onStart() we can proceed to fill out the rest that go in the onPaint() method. First we assign the current xp for your chosen skill, I have chosen RUNECRAFTING but of course you can change that to what you need. put this in your onPaint() method:

currentXp = getSkills().getExperience(Skill.RUNECRAFTING);

next we assign the current level, put the following below the above code ^

currentLevel = getSkills().getRealLevel(Skill.RUNECRAFTING);

next we work out the xpGained by subtracting the beginning xp from the current xp, put the following below the above code ^

xpGained = currentXp - beginningXp;

We also need a xp per hour calculation which is very similar to the gpPerHour which we used in a previous segment, put the following code in your onPaint() method below the above code ^

xpPerHour = (int)( xpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D));

Next we need to assign a value to the variable nextLevelXp. We get this from the xp table once again, put the following code below the code above ^

nextLevelXp = XP_TABLE[currentLevel + 1];

If you wish to use the dreambot api instead of the code above please use the following to work out next level xp:

nextLevelXp = getSkills().getExperienceForLevel(currentLevel +1);

We also need the xp until the next level, which is a key part of the final calculation, put the following below the above code ^

xpTillNextLevel = nextLevelXp - currentXp;

Now we can work out the value of time until the next level. First we need to add a small if statement into our onPaint method to stop the script churning out a screen full of random numbers, put the following below the above code:

if (xpGained >= 1)
{
timeTNL = (long) ((xpTillNextLevel / xpPerHour) * 3600000);
}

Now we can display our time until next level with time formatting, first you will need to put the following method after your onPaint() method if you havent added it already :

    private String ft(long duration)
    {
        String res = "";
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        long hours = TimeUnit.MILLISECONDS.toHours(duration)
        - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
        .toHours(duration));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
        .toMinutes(duration));
        if (days == 0) {
        res = (hours + ":" + minutes + ":" + seconds);
        } else {
        res = (days + ":" + hours + ":" + minutes + ":" + seconds);
        }
        return res;
    }

Now to display the time till next level in our drawString, put this within your onPaint method:

g.drawString("" + ft(timeTNL), 1,1);

And there we go you have done it, abit long winded i know but worth it biggrin.png congratulations.

 

 

 

% Progress Bar

 

 

A progress bar for those who are unsure if they are looking for the correct thing or for those who dont know is a coloured bar that grows with each exp gained until it reaches full at 100% They are usually added to show how close to the next level you are without showing numbers or perhaps to add that something a little extra special. So here i'm going to show you how to add this.

We will be using percent to next level calculated in the previous segment of this tutorial so please do that first if you wish to use this.

First you need to work out how big the width of your bar is going to be. Mine in this example is 337 pixels in length & will be full when it reaches 100% So copy the following code into your onPaint method below the calculations for percent to next level and before the g.drawstrings begin.

double width = (337 * (percentTNL / 100)); 

Let me explain the above. We declare a double variable because thats what we used to work out our percentage. We then label our new variable width and then do the calculation percent to next level divided by 100, the percentage our bar will be when full. Then divde that value by the total length of the bar which in our example is 337 pixels long. Next we want to make that value an integer so copy the following below:

int widdth = (int) width; 

This now coverts the double into an int. Much easier to work with when displaying rectangles. Next we want to display out growing rectangle on the screen so put this above the first g.drawstring:

g.fillRect(169,427, widdth, 20); 

The above is simply saying positon on the screen of X so 169 pixels from the left edge of the screen horizontally. Then the Y value the height on which the screen it will be displayed which is 427. Then we have our changing variable widdth which changes as our percent increases. Then lastly you have the height of the bar which is 20, you can adjust this to fit it in with your paint.

Thats it, the most complicated calculation you will have in your paint in three simple steps!

 

 

 

Changing Font Colors

 

 

This is actually quite easy to do, but i remember people asking about it so it shall be included here!  First we want to declare our new color. This can be done by pasting the following above any g.drawString or g.fillRect that you want to be this color:

Color orange = new Color(254, 127, 0); 

As you see above we are declaring the color variable and then you see three numbers. These are red, green & blue (RGB) values. You can use photoshop/fireworks/paint to grab these values of the color you want.  After declaring our color, below you need to set the new color when you want to use it. Any g.drawStrings or g.fillRect's you put below this set function will appear in the desired color. to set another color as well masybe for a different part of the paint, you will need to declare a different color and set it the other code. I will provide an example at the bottom of this section with explained code. Anyway paste this below the color declaration and above the g.drawString you wish to color:

g.setColor(orange); 

Here is an example of using multiple colors:

Color orange = new Color(254, 127, 0);				// declare orange color
Color blue = new Color(123, 456, 0);				// declare blue color
g.setColor(orange);						// set orange color
g.drawString("text coloured orange "), 317, 442);		// text here will appear orange
g.drawString("text coloured orange "), 317, 462);		// text here will appear orange
g.setColor(blue);						// set blue color
g.drawString("text coloured blue "), 317, 482);			// text here will appear blue
g.drawString("text coloured blue "), 317, 502);			// text here will appear blue 

Hope you find this little nugget helpful!

 

 

 

Hide paint button

 

 

Ok so this is really useful shit, especially when you see someone talking to you and you cant see shit because the paint is in the way. No worries click dat hide button and boom, talk away. You could even get fancy and display the paint elsewhere when the button is pressed but in this example il just hide it. First we need to declare some variables that will help us to do what we need. So copy the following into your variable declarations at the top of your script:

  	Point p;
  	boolean hide = false;
	Rectangle close = new Rectangle(10, 457, 118, 20);
	Rectangle open = new Rectangle(10, 457, 118, 20); 

Ok so we have p, this is the variable for our mouse event. When then mouse is clicked, the location value will be stored in p. We also have a boolean hide. Booleans are yes or no. 1 or 0, true or false for those noobs out there. We shall be using it as true or false and settings it as false to begin with. Next we have two rectangle declarations. These will be the box's in which if clicked, your paint will hide or open.

Next we need to set up our mouse event. So that we can detect if the mouse is clicked or not. And if its within our box's. Paste the following code below before the LAST curly } bracket. 

	public void onMouse(MouseEvent e)
	{
	    p = e.getPoint();
	    if (close.contains(p) && !hide) {
	        hide = true;
	    } else if (open.contains(p) && hide) {
	        hide = false;
	    }
	} 

Have a look at the code above a little. So we are making a method which says, If the mouse is clicked within our close box and the paint is not hidden, then set our hide boolean to true. Else if the mouse is clicked within our open box and the paint is hidden, then set our hide boolean to false. Its effectively an on/off switch.

Next we can set what we want to display when the paint is on & off. So use the below code as an example and i shall explain below:

public void onPaint(Graphics g) 
{
	if (!hide)
	{
		g.drawImage(this.hideImage, 10, 457, null);
	}
	if(hide)
	{
		g.drawImage(this.showImage, 10, 457, null); 
	}	
} 

Ok so above have a look at the code. We have a basic on paint method. Inside it we have two if statements. One for when the paint is displayed which is !hide and the other for when the paint is hidden 'hide' inside these you will have what you wish to display when the paint is hidden or showing, but please pay attention to the X & Y values of the images. They must align with the X & Y values we set for a our rectangles open & close. If you have a rectangle on screen that isnt visible via an image, how is your user going to know where to click?

I understand this may be a bit confusing so if anyone has questions feel free to post them here.

 

 

 

 

If you want any more please post here 

 

Like this post if it helped

 

 

@Pug thank you so much for this, This will help out a lot of members, especially myself, 

I'm making a complete user guide of Dreambot, and was wondering if I can add this to it, for quick access, 

(I'll just make a tab for Scripting, and have a link to this thread)

Share this post


Link to post
Share on other sites

@Pug thank you so much for this, This will help out a lot of members, especially myself, 

I'm making a complete user guide of Dreambot, and was wondering if I can add this to it, for quick access, 

(I'll just make a tab for Scripting, and have a link to this thread)

 sure, would be happy for it to be included :)

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.

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.


×
×
  • Create New...