001package jmri.jmrix.can.cbus.swing.eventtable; 002 003import java.awt.Graphics; 004import java.awt.Image; 005import java.awt.image.BufferedImage; 006import java.io.File; 007import java.io.IOException; 008import javax.imageio.ImageIO; 009import javax.swing.ImageIcon; 010 011import org.slf4j.Logger; 012import org.slf4j.LoggerFactory; 013 014/** 015 * Renderer for Bean Icons, formatting them to similar height for use in tables. 016 * Not a permanent location for this class, location likely to change in near future. 017 */ 018public class CbusBeanRenderer { 019 020 private ImageIcon tOff; 021 private ImageIcon tOn; 022 private ImageIcon sOff; 023 private ImageIcon sOn; 024 private ImageIcon lOff; 025 private ImageIcon lOn; 026 027 /** 028 * Create a new Renderer Instance. 029 * @param iconHeight Target height of Icons 030 */ 031 public CbusBeanRenderer(int iconHeight) { 032 initImages(iconHeight); 033 } 034 035 /** 036 * Get the Bean Icon, scaled to standard height. 037 * 038 * @param beanTypeChar Standard Bean Type, currently TSL supported. 039 * @param beanState Bean State 040 * @return Image of the Bean, else null ( with error log ) if unavailable. 041 */ 042 public ImageIcon getBeanIcon(String beanTypeChar, int beanState){ 043 044 ImageIcon img = null; 045 switch (beanTypeChar) { 046 case "T": 047 img = ( beanState==jmri.DigitalIO.ON ? tOn : tOff); 048 break; 049 case "S": 050 img = ( beanState==jmri.DigitalIO.ON ? sOn : sOff); 051 break; 052 case "L": 053 img = ( beanState==jmri.DigitalIO.ON ? lOn : lOff); 054 break; 055 default: 056 log.error("no image for bean {} state {}", beanTypeChar, beanState); 057 break; 058 } 059 return img; 060 } 061 062 private final static String ROOTPATH = "resources/icons/misc/switchboard/"; // also used in display.switchboardEditor 063 064 private static ImageIcon getTurnoutIcon(String file, int iconHeight) throws IOException { 065 BufferedImage bigImage = ImageIO.read(new File(ROOTPATH+file)); 066 return new ImageIcon(bigImage.getScaledInstance((int)Math.round(iconHeight*1.5), iconHeight-2, Image.SCALE_DEFAULT)); 067 } 068 069 private static ImageIcon getSensorIcon(String file, int iconHeight) throws IOException { 070 BufferedImage bufimg = ImageIO.read(new File(ROOTPATH+file)); 071 BufferedImage img = bufimg.getSubimage(11, 23, 59, 60); 072 BufferedImage copyOfImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 073 074 Graphics g = copyOfImage.createGraphics(); 075 g.drawImage(img, 0, 0, null); 076 077 return new ImageIcon(img.getScaledInstance(iconHeight+2, iconHeight+2, Image.SCALE_DEFAULT)); 078 } 079 080 private static ImageIcon getLightIcon(String file, int iconHeight) throws IOException { 081 082 BufferedImage img = ImageIO.read(new File(ROOTPATH + file)).getSubimage(11, 23, 59, 60); 083 BufferedImage copyOfImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 084 085 Graphics g = copyOfImage.createGraphics(); 086 g.drawImage(img, 0, 0, null); 087 088 return new ImageIcon(img.getScaledInstance(iconHeight-3, iconHeight-3, Image.SCALE_DEFAULT)); 089 } 090 091 private void initImages(int iconHeight){ 092 try { 093 tOn = getTurnoutIcon("T-on-s.png",iconHeight); 094 tOff = getTurnoutIcon("T-off-s.png",iconHeight); 095 sOn = getSensorIcon("S-on-s.png",iconHeight); 096 sOff = getSensorIcon("S-off-s.png",iconHeight); 097 lOn = getLightIcon("L-on-s.png",iconHeight); 098 lOff = getLightIcon("L-off-s.png",iconHeight); 099 } catch (IOException ex) { 100 log.error("Error creating Bean Icon Images",ex); 101 } 102 } 103 104 private final static Logger log = LoggerFactory.getLogger(CbusBeanRenderer.class); 105 106}