Advertisement
Guest User

wrapper example

a guest
Mar 14th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.12 KB | None | 0 0
  1. package hauptprogramm;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics2D;
  8. import java.awt.GraphicsConfiguration;
  9. import java.awt.GraphicsDevice;
  10. import java.awt.GraphicsEnvironment;
  11. import java.awt.Image;
  12. import java.awt.image.BufferedImage;
  13.  
  14. import javax.swing.AbstractButton;
  15. import javax.swing.Icon;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JLabel;
  18. import javax.swing.JMenuBar;
  19. import javax.swing.JToolBar;
  20.  
  21. import <removed>.CMainClass;
  22. import <removed>.CUiObjectButton;
  23. import <removed>.CUiObjectFftLocal;
  24. import <removed>.CUiObjectLabel;
  25. import <removed>.CUiObjectPanel;
  26. import <removed>.CUiObjectSlider;
  27. import <removed>.CUiObjectSwitchImage;
  28. import <removed>.CUiObjectTsView;
  29.  
  30. public class start {
  31.     /**
  32.      *If TRUE the scaling factor is fixed to "2" (for test and debug purposes)
  33.      */
  34.     private static boolean fixedScale2x = false;
  35.    
  36.     /**
  37.      * If TRUE there will be no scaling no matter of the screen Size / DPI-Value
  38.      */
  39.     private static boolean noScale = false;
  40.     /**
  41.      * Stores the scaling Factor
  42.      * DefaultValue = 2 (scales double the size - will be ignored id noScale is TRUE)
  43.      */
  44.     private static float scaleFactor = 2.0f;
  45.    
  46.     /**
  47.      * This value is the base value used for calculating the scale factor.
  48.      * The factor is calculated by how much bigger the current screen resolution is, compared to this value.
  49.      */
  50.     private static int baseXResolution = 1920;
  51.  
  52.    
  53.    
  54.     public static void main(String[] args) {
  55.          args = new String[6];
  56.          args[0] = "<path to the tool that is to be fixed>.jar";
  57.          args[1] = "-c";                    //Some parameters neccessary
  58.          args[2] = "config_53";                 //Some parameters neccessary
  59.          args[3] = "-u";                    //Some parameters neccessary
  60.          args[4] = "-n";                    //Some parameters neccessary
  61.          args[5] = "";                      //Some parameters neccessary
  62.        
  63.         for(int i=0; i< args.length; i++) {
  64.             if (args[i].trim().equalsIgnoreCase("-fixedscale")) {
  65.                 fixedScale2x=true;
  66.                 args[i] = "";   //Remove the additional parameter for not confusing the original software
  67.             }
  68.             if (args[i].trim().equalsIgnoreCase("-noscale")   ) {
  69.                 noScale=true;
  70.                 args[i] = "";   //Remove the additional parameter for not confusing the original software
  71.             }
  72.         }
  73.        
  74.        
  75.         /*
  76.          * If not disabled by parameter, call mathod to calculate the scaling factor.
  77.          */
  78.         if(!noScale && !fixedScale2x) {
  79.             findScaleFactor();
  80.         }
  81.  
  82.        
  83.         CMainClass.main(args);          //Call main() mathod and start original programm
  84.         if(!noScale) {
  85.             CMainClass.frame.setSize(new Dimension((int)(CMainClass.frame.getWidth()*scaleFactor), (int)(CMainClass.frame.getHeight()*scaleFactor)));
  86.             getAllComponents(CMainClass.frame);         //Iterates all child-components and scales them
  87.         }
  88.     }
  89.    
  90.    
  91. /**
  92.  * Calculates the scaling factor by calculating how much bigger the current screen resolution (x-direction) is
  93.  * in comparision to the value defined as baseXResolution.
  94.  */
  95.     private static void findScaleFactor() {
  96.         GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  97.         int currentXResolution = gd.getDisplayMode().getWidth();
  98.         if(baseXResolution > currentXResolution) return;                        //if current resolution is less then we do not need to calculate anything
  99.         scaleFactor = currentXResolution / baseXResolution;                     //calculate factor
  100.         scaleFactor = (float) (Math.round(100.0 * scaleFactor) / 100.0);        //round to 2 tailing digits
  101.         System.out.println("Scalefactor:"+scaleFactor);
  102.     }
  103.  
  104.  
  105.  
  106.     /**
  107.      * Iterates all child components of the Container in c
  108.      * and changes sizes, font-sizes, margins, backgrounds, and so on.
  109.      * Because a lot of components are to be handled in a different way, there are a lot of if-cases
  110.      * @param c
  111.      */
  112.     public static void getAllComponents(final Container c) {
  113.    
  114.         Component[] comps = c.getComponents();
  115.         for (Component comp : comps) {
  116.            
  117.             if(!(comp instanceof CUiObjectSlider) && !(comp instanceof CUiObjectFftLocal) && !(comp instanceof CUiObjectTsView)) {
  118.                 comp.setFont(new Font(  comp.getFont().getFontName(),
  119.                                         comp.getFont().getStyle(),
  120.                                         (int)(comp.getFont().getSize()*scaleFactor)
  121.                                  )
  122.                         );
  123.             };
  124.            
  125.             if((comp instanceof CUiObjectFftLocal) || (comp instanceof CUiObjectTsView)) {
  126.                 comp.setFont(new Font(  comp.getFont().getFontName(),
  127.                         Font.BOLD,
  128.                         14/*comp.getFont().getSize()*/
  129.                     ));
  130.                
  131.             };
  132.            
  133.            
  134.             comp.setBounds(
  135.                         (int)(comp.getBounds().x*scaleFactor),
  136.                         (int)(comp.getBounds().y*scaleFactor),
  137.                         (int)(comp.getBounds().width*scaleFactor),
  138.                         (int)(comp.getBounds().height*scaleFactor)
  139.                         );
  140.  
  141.            
  142.             if(comp instanceof CUiObjectButton) {
  143.                 Icon tIcon;
  144.                
  145.                 try { tIcon = ((CUiObjectButton) comp).getIcon();
  146.                 ((CUiObjectButton) comp).setIcon(iconToScaledImageIcon(tIcon)); }
  147.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  148.                
  149.                 try { tIcon = ((CUiObjectButton) comp).getDisabledIcon();
  150.                 ((CUiObjectButton) comp).setDisabledIcon(iconToScaledImageIcon(tIcon)); }
  151.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  152.                
  153.                 try { tIcon = ((CUiObjectButton) comp).getPressedIcon();
  154.                 ((CUiObjectButton) comp).setPressedIcon(iconToScaledImageIcon(tIcon)); }
  155.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  156.                
  157.                 try { tIcon = ((CUiObjectButton) comp).getRolloverIcon();
  158.                 ((CUiObjectButton) comp).setRolloverIcon(iconToScaledImageIcon(tIcon)); }
  159.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  160.                                
  161.                 try { tIcon = ((CUiObjectButton) comp).getDisabledSelectedIcon();
  162.                 ((CUiObjectButton) comp).setDisabledSelectedIcon(iconToScaledImageIcon(tIcon)); }
  163.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }      
  164.  
  165.                 try { tIcon = ((CUiObjectButton) comp).getRolloverSelectedIcon();
  166.                 ((CUiObjectButton) comp).setRolloverSelectedIcon(iconToScaledImageIcon(tIcon)); }
  167.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  168.             }
  169.  
  170.             else
  171.             if(comp instanceof CUiObjectPanel) {
  172.                 String iconPath = ((CUiObjectPanel)comp).getUiImage();
  173.                 ((CUiObjectPanel)comp).setUiImage("");
  174.                 if(iconPath.length()>0){
  175.                   ImageIcon i = new ImageIcon(iconPath);
  176.                   /* Atention: double code - can be made better */
  177.                   Image scaledImage = i.getImage().getScaledInstance(
  178.                           (int)(i.getIconWidth()*scaleFactor),  
  179.                           (int)(i.getIconHeight()*scaleFactor),
  180.                           java.awt.Image.SCALE_SMOOTH);
  181.                   ImageIcon imageIcon = new ImageIcon(scaledImage);
  182.                   JLabel t = new JLabel();
  183.                   t.setIcon(imageIcon);
  184.                   t.setLocation(0,0);
  185.                   t.setSize(comp.getSize());
  186.                   t.setVerticalAlignment(JLabel.TOP);
  187.                   t.setHorizontalTextPosition(JLabel.LEFT);
  188.                   ((CUiObjectPanel)comp).add(t);                 
  189.                 }
  190.             }
  191.             else   
  192.             if(comp instanceof CUiObjectSlider) {
  193.                 Icon tIcon;
  194.                 try { tIcon = ((AbstractButton) comp).getIcon();
  195.                 ((AbstractButton) comp).setIcon(iconToScaledImageIcon(tIcon)); }
  196.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  197.  
  198.                 try { tIcon = ((AbstractButton) comp).getDisabledIcon();
  199.                 ((AbstractButton) comp).setDisabledIcon(iconToScaledImageIcon(tIcon)); }
  200.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  201.  
  202.                 try { tIcon = ((AbstractButton) comp).getPressedIcon();
  203.                 ((AbstractButton) comp).setPressedIcon(iconToScaledImageIcon(tIcon)); }
  204.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  205.  
  206.                 try { tIcon = ((AbstractButton) comp).getRolloverIcon();
  207.                 ((AbstractButton) comp).setRolloverIcon(iconToScaledImageIcon(tIcon)); }
  208.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  209.  
  210.                 try { tIcon = ((AbstractButton) comp).getDisabledSelectedIcon();
  211.                 ((AbstractButton) comp).setDisabledSelectedIcon(iconToScaledImageIcon(tIcon)); }
  212.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }      
  213.  
  214.                 try { tIcon = ((AbstractButton) comp).getRolloverSelectedIcon();
  215.                 ((AbstractButton) comp).setRolloverSelectedIcon(iconToScaledImageIcon(tIcon)); }
  216.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  217.             }
  218.  
  219.             else
  220.             if(comp instanceof CUiObjectLabel) {
  221.                 Icon tIcon;
  222.  
  223.                 try { tIcon = ((CUiObjectLabel) comp).getIcon();
  224.                 ((CUiObjectLabel) comp).setIcon(iconToScaledImageIcon(tIcon)); }
  225.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  226.  
  227.                 try { tIcon = ((CUiObjectLabel) comp).getDisabledIcon();
  228.                 ((CUiObjectLabel) comp).setDisabledIcon(iconToScaledImageIcon(tIcon)); }
  229.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  230.  
  231.                 try { tIcon = ((CUiObjectLabel) comp).getPressedIcon();
  232.                 ((CUiObjectLabel) comp).setPressedIcon(iconToScaledImageIcon(tIcon)); }
  233.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  234.  
  235.                 try { tIcon = ((CUiObjectLabel) comp).getRolloverIcon();
  236.                 ((CUiObjectLabel) comp).setRolloverIcon(iconToScaledImageIcon(tIcon)); }
  237.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  238.        
  239.                 try { tIcon = ((CUiObjectLabel) comp).getDisabledSelectedIcon();
  240.                 ((CUiObjectLabel) comp).setDisabledSelectedIcon(iconToScaledImageIcon(tIcon)); }
  241.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }      
  242.  
  243.                 try { tIcon = ((CUiObjectLabel) comp).getRolloverSelectedIcon();
  244.                 ((CUiObjectLabel) comp).setRolloverSelectedIcon(iconToScaledImageIcon(tIcon)); }
  245.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }
  246.             }
  247.  
  248.             else
  249.             if(comp instanceof CUiObjectSwitchImage) {
  250.                 Icon tIcon;
  251.                 try { tIcon = ((CUiObjectSwitchImage) comp).getIcon();
  252.                 ((CUiObjectSwitchImage) comp).setIcon(iconToScaledImageIcon(tIcon));
  253.             }
  254.                 catch (Exception e){ /*Gibt eigentlich nichts zu tun... oder?*/ }              
  255.             }          
  256.  
  257.             if (comp instanceof Container) {
  258.                  if(!(comp instanceof JMenuBar) && !(comp instanceof JToolBar)) {
  259.                      getAllComponents((Container) comp);
  260.                  }
  261.             }
  262.         }
  263.     }
  264.    
  265.    
  266.     static ImageIcon iconToScaledImageIcon(Icon icon) {
  267.             int w = icon.getIconWidth();
  268.             int h = icon.getIconHeight();
  269.             GraphicsEnvironment ge =
  270.             GraphicsEnvironment.getLocalGraphicsEnvironment();
  271.             GraphicsDevice gd = ge.getDefaultScreenDevice();
  272.             GraphicsConfiguration gc = gd.getDefaultConfiguration();
  273.             BufferedImage image = gc.createCompatibleImage(w, h, java.awt.image.BufferedImage.TRANSLUCENT);
  274.             Graphics2D g = image.createGraphics();
  275.             icon.paintIcon(null, g, 0, 0);
  276.             g.dispose();
  277.  
  278.             /* Achtung, doppelter code ... kann verbessert werden */
  279.             Image scaledImage = image.getScaledInstance(
  280.                     (int)(w*scaleFactor),
  281.                     (int)(h*scaleFactor),
  282.                     java.awt.Image.SCALE_SMOOTH);
  283.             ImageIcon imageIcon = new ImageIcon(scaledImage);
  284.             return imageIcon;
  285.  
  286.          }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement