2012-10-13 90 views

回答

2

一種粗暴的方式..

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.draw(image,0,0,getWidth(),getHeight(),this); 
    // .. 
5

這是一個開放式的問題。

您想縮放以填充或縮放以適應該區域,或者您不關心長寬比?

規模以填充和尺度之間的差異,以適應

enter image description here enter image description here

這個例子將到變化作出反應的幀的大小,並實時重新縮放圖像。

public class TestScaling { 

    public static void main(String[] args) { 
     new TestScaling(); 
    } 

    public TestScaling() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new ScalingPane()); 
       frame.setSize(200, 200); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class ScalingPane extends javax.swing.JPanel { 

     private BufferedImage image; 

     public ScalingPane() { 
      try { 
       image = ImageIO.read(getClass().getResource("/AtDesk.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      }  
      setBackground(Color.red); 
     } 

     public double getScaleFactor(int iMasterSize, int iTargetSize) {  
      double dScale = 1; 
      dScale = (double) iTargetSize/(double) iMasterSize; 

      return dScale;  
     } 

     public double getScaleFactorToFit(Dimension original, Dimension toFit) {  
      double dScale = 1d; 

      if (original != null && toFit != null) {  
       double dScaleWidth = getScaleFactor(original.width, toFit.width); 
       double dScaleHeight = getScaleFactor(original.height, toFit.height); 

       dScale = Math.min(dScaleHeight, dScaleWidth); 
      }  
      return dScale; 
     } 

     public double getScaleFactorToFill(Dimension masterSize, Dimension targetSize) { 
      double dScaleWidth = getScaleFactor(masterSize.width, targetSize.width); 
      double dScaleHeight = getScaleFactor(masterSize.height, targetSize.height); 

      double dScale = Math.max(dScaleHeight, dScaleWidth); 

      return dScale; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      double scaleFactor = Math.min(1d, getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), getSize())); 
//   double scaleFactor = Math.min(1d, getScaleFactorToFill(new Dimension(image.getWidth(), image.getHeight()), getSize())); 

      int scaleWidth = (int) Math.round(image.getWidth() * scaleFactor); 
      int scaleHeight = (int) Math.round(image.getHeight() * scaleFactor); 

      Image scaled = image.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH); 

      int width = getWidth() - 1; 
      int height = getHeight() - 1; 

      int x = (width - scaled.getWidth(this))/2; 
      int y = (height - scaled.getHeight(this))/2; 

      g.drawImage(scaled, x, y, this); 
     } 
    } 
} 

一個更好的解決辦法是有某種可能的變化作出反應的部件的尺寸,並在後臺重新縮放的原始圖像的後臺線程,提供低質量和高質量的規模。

還應該注意的是,Image.getScaledInstance既不是最快的或最高質量的縮放算法。請參閱The Perils of Image.getScaledInstance瞭解更多信息。

您也可能會發現

Java: maintaining aspect ratio of JPanel background image

+0

我想要擴展以填充,並感謝您的答案! –

相關問題