Jump to content

Explv's Dank Paint Tutorial


Explv

Recommended Posts

Explv's Dank Paint Tutorial

 

78ZISi3.png78ZISi3.png78ZISi3.png78ZISi3.png78ZISi3.png

 

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 by Explv
  • Like 16
  • Heart 2
Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...
  • 3 months later...
  • 3 months later...
  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...