Jump to content

Paint Utilities Class [Simple, Informative Paints]


NotoriousPP

Recommended Posts

Well I thought today I would release some of my private collection of different snippets to hopefully help new writers learn a little bit, and to maybe see a rise in quality of scripts throughout this forum.
 
The first release is my Painter class, which I originally got from a open source framework last year. I really liked what the writer did, so I then went and revised to work with OSBot, and to my what I thought was most optimal. I usually use this class in my personal scripts, becasue I just like knowing how my script is doing, I don't need a crazy ass paint to tell me that, just need the information clear and easy to read, and I know a lot of programmers feel the same way.
 
Original author: Coma - 08/22/13
Revision author: NotoriousPP - 02/13/14
 
What does this class actually do?
Well simply put, a class that will create a display a clean, easy to read paint, with multiple functions making the writers life a little bit easier.
 
How does is work?
In the example below, I will show the source for a paint, then what that paint will look like in your script.
** Timer class with a getElapsed() method is needed to use this! **


Code:

private Timer timer;
public int tarCollected = 0;
String status = "Nothing";
private final Painter paint = new Painter(getName(), getVersion() + "");
private final Painter.PaintProperty time = new Painter.PaintProperty();
private final Painter.PaintProperty status = new Painter.PaintProperty();
private final Painter.PaintProperty tar = new Painter.PaintProperty();
private final Painter.PaintProperty profit = new Painter.PaintProperty();

@Override
public void onPaint(Graphics graphics) {
        final long runtime = timer.getElapsed();
        final double tarHr = paint.getHourlyRate(tarCollected, runtime);
        final int profit = tarCollected * prices().getItemPrice(Constants.TAR_NAME);
        final double profitHr = paint.getHourlyRate(profit, runtime);
        paint.properties(
                time.value("Time: " + paint.formatTime(runtime)),
                status.value("Status: " + status),
                tar.value("Tar Looted: " + tarCollected + " (" + (int) tarHr + ")"),
                this.profit.value("Profit: " + paint.format(profit) + " (" + paint.format((int) profitHr) + ")")
        ).draw(graphics, this);
    }
}

Results:
97d6af15eb.jpg


 
Finally, where the magic happens:
The source code to the Painter class!

(Added JDoc information tables to help you guys understand what is going on a little easier)

import org.osbot.script.rs2.skill.Skill;

import java.awt.*;
import java.text.DecimalFormat;

/**
 * Original
 * @author Coma
 * Date: 8/22/13
 * Time: 10:41 PM
 * --------------
 * Updated
 * @author NotoriousPP
 * Date: 2/13/14
 * Time: 10:41 PM
 */

public class Painter {

    private String name,
                   version;
    private PaintProperty[] properties = new PaintProperty[0];

    private final AlphaComposite background = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .75f);
    private final AlphaComposite foreground = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
    private final Font title = new Font("Tahoma", Font.PLAIN, 16);
    private final Font stats = new Font("Tahoma", Font.PLAIN, 12);

    //We use this to format large amounts
    private final DecimalFormat format = new DecimalFormat("###,###,###");

    /**
     *
     * @param name Name of the script.
     * @param version Current version of the script.
     */
    public Painter(String name, String version) {
        this.name = name;
        this.version = version;
    }

    /**
     *
     * @param properties Each property represents item in the paint.
     * @return this.
     */
    public Painter properties(PaintProperty... properties) {
        this.properties = properties;
        return this;
    }

    /**
     *
     * @param i The amount you want to be formatted.
     * @return String of formatted double.
     */
    public String format(double i) {
        return this.format.format(i);
    }

    /**
     *
     * @param time Current time in ms.
     * @return String of formatted time in HH:MM:SS
     */
    public String formatTime(final long time) {
        final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
        return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
    }

    /**
     *
     * @param ctx Script context.
     * @param skill Which skill would like to track.
     * @param level Level you wish to reach
     * @return int of experience left till desired level.
     */
    public int getExperienceToLevel(Script ctx, Skill skill, int level) {
        return getXPForLevel(level) - ctx.client.getSkills().getExperience(skill);
    }

    /**
     *
     * @param ctx Script context.
     * @param skill Which skill would like to track.
     * @param goal Level you wish to reach.
     * @param xpPerHour Current Xp per hour.
     * @return String of formatted time till to desired level.
     */
    public String getTimeToLevel(Script ctx, Skill skill, int goal, final int xpPerHour) {
        return xpPerHour > 0 ? formatTime((long)(getExperienceToLevel(ctx, skill, goal) * 3600000D / xpPerHour)) : formatTime(0L);
    }

    /**
     *
     * @param level The level you wish to know experience for.
     * @return int of experience needed to level.
     */
    public int getXPForLevel(int level) {
        int points = 0,
            output = 0;
        for (int lvl = 1; lvl <= level; lvl++) {
            points += Math.floor((double) lvl + 300.0 *
                      Math.pow(2.0, (double) lvl / 7.0));
            if (lvl >= level)
                return output;
            output = (int) Math.floor(points / 4);
        }
        return 0;
    }

    /**
     *
     * @param count Amount you wish to get hourly rate for.
     * @param runTime The current time in ms.
     * @return double of current hourly rate of inputted count.
     */
    public double getHourlyRate(int count, long runTime) {
        return runTime > 0 ? (count * 3600000D) / (runTime) : 0;
    }

    /**
     *
     * @param render The current graphic instance
     */
    public void draw(Graphics render) {
        draw(render, null);
    }

    /**
     *
     * @param render The current graphic instance.
     * @param ctx Script context.
     */
    public void draw(Graphics render, Script ctx) {
        final Graphics2D g = (Graphics2D) render;
        final FontMetrics fm = g.getFontMetrics(title);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);

        g.setFont(title);
        //Background color
        g.setColor(new Color(156, 64, 152));
        g.setComposite(background);
        g.fillRect(5, 5, 180, 50 + (properties.length * 20));
        //Text color
        g.setComposite(foreground);
        g.setColor(Color.WHITE);
        g.drawRect(5, 5, 180, 50 + (properties.length * 20));

        if (ctx != null) {
            final int x = ctx.client.getMousePosition().x,
                      y = ctx.client.getMousePosition().y;
            g.drawLine(x + 5, y, x - 5, y);
            g.drawLine(x, y + 5, x, y - 5);
        }

        final int w1 = fm.stringWidth(version);
        final int w2 = fm.stringWidth(name);

        g.drawString(version, (180 / 2) + 5 - ((w1 / 2)), 20);
        g.drawString(name, (180 / 2) + 5 - ((w2 / 2)), 40);
        g.drawLine(15, 45, 175, 45);

        g.setFont(stats);

        for (int i = 0; i < properties.length; i++) {
            final PaintProperty p = properties[i];
            g.drawString(p.getValue(), 15 + p.getOffset(), 70 + (i * 20));
        }
    }

    /**
     * The paint objects we use inside of the Painter.class
     */
    public static class PaintProperty {
        private String value;
        private int offset;

        public PaintProperty(String value, int offset) {
            this.value = value;
            this.offset = offset;
        }

        public PaintProperty(String value) {
            this(value, 0);
        }

        public PaintProperty() {
            this("");
        }

        public PaintProperty value(String value) {
            this.value = value;
            return this;
        }

        public PaintProperty offset(int offset) {
            this.offset = offset;
            return this;
        }

        public String getValue() {
            return this.value;
        }

        public int getOffset() {
            return this.offset;
        }
    }
}


 
Questions/Comments?:
If you see anything I messed up on, or should be improved, please let me know, but be respectful about it, we have too make keyboard warriors thinking their hot shit, yet do nothing but bash others and never give any useful resources.
Even if you have a question, free to ask me, just please refrain from asking me blatant obvious questions, or ones you did little to no research on before asking, I'm not here to spoon feed you, though I am willing to help someone is trying.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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