Jump to content

onPaint() Vs Buffered images - What is efficient?


Elixar

Recommended Posts

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
Link to comment
Share on other sites

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. 

Link to comment
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.
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...