Explv Posted December 2, 2015 Share Posted December 2, 2015 (edited) Explv's Dank Paint Tutorial Run Time (ms) To calculate the run time of your script, first, you need to create a global variable that will store the start time, and initialise it in the onStart method: private long startTime; @Override public final void onStart(){ startTime = System.currentTimeMillis(); } To then calculate the run time at any point you simply need to calculate the difference in the current time and start time: final long runTime = System.currentTimeMillis() - startTime; Formatting Time This method returns a time String in the format hh:mm:ss It works by calculating seconds, minutes and hours from the given milliseconds parameter, and then returns a String with each value padded with a 0 if it is less than 10. For example if the value passed to the method is 2000ms, it will return 00:00:02 public final String formatTime(final long ms){ long s = ms / 1000, m = s / 60, h = m / 60; s %= 60; m %= 60; h %= 24; return String.format("%02d:%02d:%02d", h, m, s); } Dynamic Time Formatting (Includes Days) This method is an extension of the previous "Formatting Time" method. When the time is less than an hour it will only return minutes and seconds. For example 54:23. When the time is less than a day, it will only return hours, minutes and seconds. For example 14:54:23. When the time is greater than a day it will return days, hours, minutes and seconds. For example 01:14:54:23. public final String formatTime(final long ms){ long s = ms / 1000, m = s / 60, h = m / 60, d = h / 24; s %= 60; m %= 60; h %= 24; return d > 0 ? String.format("%02d:%02d:%02d:%02d", d, h, m, s) : h > 0 ? String.format("%02d:%02d:%02d", h, m, s) : String.format("%02d:%02d", m, s); } Formatting Money and XP This method should be self explanatory. It takes an integer value and returns a shorter form if possible, e.g. in m or k. public final String formatValue(final long l) { return (l > 1_000_000) ? String.format("%.2fm", ((double) l / 1_000_000)) : (l > 1000) ? String.format("%.1fk", ((double) l / 1000)) : l + ""; } Starting the Experience Tracker The Experience Tracker class provides us with several useful statistics for our paint. Before we can use these statistics we must start the Experience Tracker tracking the skills we want statistics for. (This should be done in the onStart method) // Tracking a single skill getExperienceTracker().start(Skill.WOODCUTTING); // Tracking several skills for(final Skill skill : new Skill[]{Skill.WOODCUTTING, Skill.FIREMAKING, Skill.FLETCHING}) { getExperienceTracker().start(skill); } // Tracking all skills getExperienceTracker().startAll(); Using the Experience Tracker Now that we have started the Experience Tracker we have access to the following methods (note, these can be used with the functions shown at the top to display output in a more readable format): // Getting the time since we started the Experience Tracker getExperienceTracker().getElapsed(Skill.ATTACK); // Getting the number of levels gained since we started the Experience Tracker getExperienceTracker().getGainedLevels(Skill.ATTACK); // Getting the amount of XP gained since we started the Experience Tracker getExperienceTracker().getGainedXP(Skill.ATTACK); // Getting the amount of XP gained per hour since we started the Experience Tracker getExperienceTracker().getGainedXPPerHour(Skill.ATTACK); // Getting the time until we reach the next level in this skill getExperienceTracker().getTimeToLevel(Skill.ATTACK); The Skills class The Skills class provides methods for getting useful information about the player's stats. // Get the current level for a skill (affected by potions etc.) getSkills().getDynamic(Skill.ATTACK); // Get the actual level for a skill getSkills().getStatic(Skill.ATTACK); // Get the amount of experience the player has in the skill getSkills().getExperience(Skill.ATTACK); // Get the amount of experience to the next level in a skill getSkills().experienceToLevel(Skill.ATTACK); // Get the amount of experience required for a specific level getSkills().getExperienceForLevel(40); Percentage to next level public final double percentToNextLevel(final Skill skill){ int curLvl = getSkills().getStatic(skill), curXP = getSkills().getExperience(skill), xpCurLvl = getSkills().getExperienceForLevel(curLvl), xpNextLvl = getSkills().getExperienceForLevel(curLvl + 1); return (((curXP - xpCurLvl) * 100) / (xpNextLvl - xpCurLvl)); } Price of an item This method uses the RSBuddy Exchange API to retrieve the price of an item with the given ID. If you use this for your paint, ensure you call this method only in onStart, as we only need to retrieve the price once. private Optional<Integer> getPrice(int id){ Optional<Integer> price = Optional.empty(); try { URL url = new URL("http://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + id); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); con.setUseCaches(true); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String[] data = br.readLine().replace("{", "").replace("}", "").split(","); br.close(); price = Optional.of(Integer.parseInt(data[0].split(":")[1])); } catch(Exception e){ e.printStackTrace(); } return price; } Overall price, Buying price, Buying quantity, Selling price, Selling quantity of an item This method uses the RSBuddy Exchange API to retrieve the exchange info of an item with the given ID. If you use this for your paint, ensure you call this method only in onStart, as we only need to retrieve the price once. private HashMap<String, Integer> getExchangeInfo(int id){ HashMap<String, Integer> exchangeInfo = new HashMap<>(); try { URL url = new URL("http://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + id); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); con.setUseCaches(true); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String json = br.readLine(); br.close(); json = json.replaceAll("[{}\"]", ""); String[] items = json.split(","); for(String item : items) { String[] splitItem = item.split(":"); exchangeInfo.put(splitItem[0], Integer.parseInt(splitItem[1])); } } catch (final Exception e){ e.printStackTrace(); } return exchangeInfo; } Usage: HashMap<String, Integer> exchangeInfo= getExchangeInfo(1515); exchangeInfo.get("overall"); exchangeInfo.get("buying"); exchangeInfo.get("buyingQuantity"); exchangeInfo.get("selling"); exchangeInfo.get("sellingQuantity"); The following code requires a Graphics 2D instance, and therefore should be used within the onPaint method (as this method has a Graphics 2D instance as a parameter). We assume that the name of the variable passed to the onPaint method is 'g'. Drawing text on screen The drawString method is used to draw text on screen, the parameters are the text to be drawn, the x coordinate and y coordinate where the text should be drawn. g.drawString("Hello world!", 10, 10); Changing text font and size // Creating and setting a new font that is Open Sans, Bold and size 18 Font font = new Font("Open Sans", Font.BOLD, 18); g.setFont(font); // Changing the size of the current font to size 18 g.setFont(g.getFont().deriveFont(18.0f)); Drawing shapes on screen The following methods are useful for drawing paint backgrounds. // Drawing a rectangle g.drawRect(x, y, width, height); // Drawing an oval g.drawOval(x, y, width, height); // Drawing a line g.drawLine(startX, startY, endX, endY); Changing text / shape colour The colour of text or shapes can be changed by calling a set colour method BEFORE drawing text / a shape: // Using a constant value g.setColor(Color.black); // Using a HEX colour g.setColor(Color.decode("#FFFFFF")); // Using an RGB colour (values are between (0-255) if ints and (0.0 - 1.0) if floats g.setColor(new Color(red, green, blue)); Changing opacity To change how opaque your text / background / shapes are, you can use an RGBA colour. A stands for the alpha channel which determines opacity. (Lower is less opaque) // Values are between (0-255) if ints and (0.0 - 1.0) if floats g.setColor(new Color(red, green, blue, alpha)); Drawing an image Assuming your images are stored in a directory called "resources" Firstly you need to read the image from the file. This should NOT be done inside the onPaint method as this would be VERY inefficient. Declare the images you need as global variables of type BufferedImage, for example: ... public class Main extends Script{ BufferedImage background; @Override public void onStart(){ } ... } Now we need to read the image from its file into the variable. This should be done inside onStart, as we only need to do it once: ... public class Main extends Script{ BufferedImage background; @Override public void onStart(){ try{ background = ImageIO.read(Main.class.getResourceAsStream("/resources/background.png")); } catch(IOException e){ log(e); } } ... } Now that the image has been read from its file we can draw it inside the onPaint method using g.drawImage: public class Main extends Script{ BufferedImage background; @Override public void onStart(){ try{ background = ImageIO.read(Main.class.getResourceAsStream("/resources/background.png")); } catch(IOException e){ log(e); } } ... @Override public void onPaint(Graphics2D g){ if(background != null){ g.drawImage(background, null, x, y); } } } Customising the mouse This is a basic example that shows how you can draw two lines that intersect at the mouse position. // Get current mouse position Point mP = getMouse().getPosition(); // Draw a line from top of screen (0), to bottom (500), with mouse x coordinate g.drawLine(mP.x, 0, mP.x, 500); // Draw a line from left of screen (0), to right (800), with mouse y coordinate g.drawLine(0, mP.y, 800, mP.y); Drawing a rectangle around the mouse: // Get current mouse position Point mP = getMouse().getPosition(); // Draw a rectangle starting at x-10, y-10, with width and height of 20 g.drawRect(mP.x - 10, mP.y - 10, 20, 20); Drawing an 'X' crosshair Point mP = getMouse().getPosition(); g.drawLine(mP.x - 5, mP.y + 5, mP.x + 5, mP.y - 5); g.drawLine(mP.x + 5, mP.y + 5, mP.x - 5, mP.y - 5); Edited June 4, 2017 by Explv 16 2 Quote Link to comment Share on other sites More sharing options...
Lambos Bruh Posted December 2, 2015 Share Posted December 2, 2015 10/10 would paint 3 Quote Link to comment Share on other sites More sharing options...
qw3 Posted December 2, 2015 Share Posted December 2, 2015 Dude. This is Dank 1 Quote Link to comment Share on other sites More sharing options...
Chris Posted December 2, 2015 Share Posted December 2, 2015 10/10 write my wyverns for me plz this hacked my account? reported 1 Quote Link to comment Share on other sites More sharing options...
qverkk Posted December 2, 2015 Share Posted December 2, 2015 Nice tutorial Quote Link to comment Share on other sites More sharing options...
Deceiver Posted December 2, 2015 Share Posted December 2, 2015 Font font = new Font("Open Sans", Font.PLAIN, 13); Quote Link to comment Share on other sites More sharing options...
Vilius Posted December 2, 2015 Share Posted December 2, 2015 dank, too op for nubs Quote Link to comment Share on other sites More sharing options...
Bobrocket Posted December 2, 2015 Share Posted December 2, 2015 For any web requests, remember to cache Quote Link to comment Share on other sites More sharing options...
Genii Posted December 2, 2015 Share Posted December 2, 2015 Dank Quote Link to comment Share on other sites More sharing options...
Explv Posted December 2, 2015 Author Share Posted December 2, 2015 (edited) Font font = new Font("Open Sans", Font.PLAIN, 13); Fixed For any web requests, remember to cache Fixed, sort of :xdoge: Edited December 4, 2015 by Explv Quote Link to comment Share on other sites More sharing options...
Saiyan Posted January 24, 2016 Share Posted January 24, 2016 How 2 do item p/h plz Quote Link to comment Share on other sites More sharing options...
AmericanKid Posted April 10, 2016 Share Posted April 10, 2016 (edited) "this.previousCount = getInventory().getAmount(id);" this should be "this.prevInvCount = getInventory().getAmount(id);" Edited April 10, 2016 by namerplz Quote Link to comment Share on other sites More sharing options...
Nebulae Posted August 7, 2016 Share Posted August 7, 2016 Wow, thanks a lot for this. Quote Link to comment Share on other sites More sharing options...
combat_acc Posted November 13, 2016 Share Posted November 13, 2016 thanks to you Quote Link to comment Share on other sites More sharing options...
TheWind Posted December 5, 2016 Share Posted December 5, 2016 Love this tutorial Quote Link to comment Share on other sites More sharing options...