Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

18
LINES

< > BotCompany Repo | #1034405 // FitImageInComponentRenderable

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5805L/32K).

1  
srecord FitImageInComponentRenderable(BufferedImage srcImage) is Renderable {
2  
  settable Color backgroundColor = Color.white;
3  
  
4  
  public void renderOn(Graphics2D g, int w, int h) {
5  
    if (backgroundColor != null) {
6  
      g.setColor(Color.white);
7  
      g.fillRect(0, 0, w, h);
8  
    }
9  
    
10  
    if (srcImage == null) ret;
11  
    double scale = scaleToFitInside(srcImage, w, h);
12  
    int x = (w-iround(srcImage.getWidth()*scale))/2;
13  
    int y = (h-iround(srcImage.getHeight()*scale))/2;
14  
    g.translate(x, y);
15  
    g.scale(scale, scale);
16  
    g.drawImage(srcImage, 0, 0, null);
17  
  }
18  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034405
Snippet name: FitImageInComponentRenderable
Eternal ID of this version: #1034405/7
Text MD5: 6fa7e078f9a360c4717ad674669eee4a
Transpilation MD5: 52bd42a1645ed3a8d3eff19794cd9295
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-05 23:52:20
Source code size: 591 bytes / 18 lines
Pitched / IR pitched: No / No
Views / Downloads: 167 / 263
Version history: 6 change(s)
Referenced in: [show references]