2012-10-13 3 views
2

내 응용 프로그램을 최대화하면 JPanel 내부의 이미지 크기가 조정되지 않습니다. 창이 최대화되었을 때 JPanel과 내용을 조정하려면 어떻게해야합니까?응용 프로그램 윈도우의 크기를 조정할 때 응용 프로그램 내에서 이미지의 크기를 조정하려면 어떻게해야합니까?

편집 : 나는 오픈했다 질문 BufferedImage의

+0

당신이 확대 하시겠습니까 관심의 다음을 찾을 수 있을까요? –

+1

이미지의 크기를 조정하는 방법이나 이미지의 크기를 조정하는 방법을 알고 싶습니까? – MadProgrammer

답변

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

필자는 채우기 위해 축척을 시도하고 있으며 답변에 감사드립니다! –

관련 문제