Jump to content

onPaint() Vs Buffered images - What is efficient?


Recommended Posts

Posted (edited)

The aim is Efficiency, I am wondering what would put more stress on the program. Option 1 is using only 1 thread onPaint() but if that thread is constantly looping then it's actually a lot of code that can all be removed if you just used a BufferedImage However...

Using option 2 would also be re-placing the image over and over again, And i'm not exactly sure if that's being downloaded each looping cycle or it's being Cached naturally to the user. ( I'm not talking about syncing my resource file with the sdn, Im talking about when the script is on the SDN will the osbot client cache the image onto the Bot-Users personal Computer? Is that something I'll have to implement myself?

What is more memory/cpu intense?

  • Reloading an image each cycle
    or
  • changing font types, re-drawing rectangles and colors 10-20 times per loop



Option 1

@Override
	public void onPaint(Graphics2D g) {
g.setColor(new Color(34, 30, 73, 255));
          g.fillRect(0, 132, 84, 24);		// g.drawRect(x, y, width, height);
          g.fillRect(0, 182, 84, 24);		// g.drawRect(x, y, width, height);
          g.fillRect(0, 232, 60, 24);		// g.drawRect(x, y, width, height);
	g.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		g.setColor(new Color(255, 209, 102));
          g.drawString("XP Gained", 3, 129);
          g.drawString("XP P/hour", 3, 179);
          g.drawString("Lvls Gained", 3, 229);	
    }



Option 2

    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);
        }
    }
}


Thanks for all your answers
☕            :gnome: wtf these Icons are to big lmao?

Edited by Elixar
Posted

The first function uses higgghly optimized draw functions to create the rectangle, and uses less memory as your image would require to be stored in OldGen and never be collected by the garbage collector. It's by far more efficient then drawing an image. Both are still very efficient however and wont make a noticeable performance impact on your script. 

As for your second question regarding option 2. "Main.class.getResourceAsStream("/resources/background.png")" is grabbing your "background.png" directly from the loaded jar. It is not downloading anything that wasn't already streamed to the client. So in terms of "cacheing" it, it already exists in memory and therefor I guess you could say it's cached. 

 

Tl;Dr: Use BufferedImage if you'd like to create a more advanced/pretty background using an image. Use Option 1 if you just want something very fast, with little to no memory overhead. Either way, both are very efficient. 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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