2015-09-25 65 views
0

目前,這是我的paintComponent的樣子:JPanel的圖片只有的paintComponent循環之後更新結束

@Override 
    public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    System.out.println(choice); 
    if(choice == 1) 
    { 
     g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 

     System.out.println("\nImgWidth: " + img.getWidth() 
       + "\nFrameWidth: " + getWidth() 
       + "\nImgHeight: " + img.getHeight() 
       + "\nFrameHeight: " + getHeight() 
     ); 
    } 
    else if (choice == 2) 
    { 
     scale = 3.0; 
     while(currScale < scale){ 
      currScale += .1; 
      System.out.println(currScale + "-" + scale); 

      Graphics2D g2 = (Graphics2D)g; 
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
          RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
      int w = getWidth(); 
      int h = getHeight(); 
      int imageWidth = img.getWidth(); 
      int imageHeight = img.getHeight(); 
      double x = (w - currScale * imageWidth)/2; 
      double y = (h - currScale * imageHeight)/2; 
      AffineTransform at = AffineTransform.getTranslateInstance(x,y); 
      at.scale(currScale, currScale); 
      g2.drawRenderedImage(img, at); 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException ex) {} 

     } 
    } 

} 

代碼工作時,我忘記了循環和只使用一個固定值的規模,但我想它是動畫而不是立即縮放。我嘗試過使用定時器替代方法,但是沒有任何東西顯示出來,並且在paintComponent被觸發並且選擇是2的時刻圖像就消失了。

使用sleep方法時,縮放的圖像顯示出來,但直到循環之後。在循環之前和期間,它仍然顯示原始的完整圖像。它像循環的整個點運行(動畫)是毫無意義的,因爲變化只發生時纔會發生。

任何幫助表示讚賞。

而且如果你們想知道我是如何搞砸與定時器,這是我的代碼爲它(它去掉了原始圖像,並沒有顯示任何東西,但計時器沒有終止)

else 
    { 
     scale = 3.0; 
      timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 

     if(currScale < scale){ 
      System.out.println(currScale + "-" + scale); 
      currScale += .1; 
      Graphics2D g2 = (Graphics2D)g; 
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
          RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
     int w = getWidth(); 
     int h = getHeight(); 
     int imageWidth = img.getWidth(); 
     int imageHeight = img.getHeight(); 
     double x = (w - currScale * imageWidth)/2; 
     double y = (h - currScale * imageHeight)/2; 
     AffineTransform at = AffineTransform.getTranslateInstance(x,y); 
     at.scale(currScale, currScale); 
     g2.drawRenderedImage(img, at); 
     } 
       } 


      }); 

     if(currScale > scale) 
     { 
      timer.stop(); 
     } 
     else 
     { 
      timer.setRepeats(true); 
      timer.setCoalesce(true); 
      timer.start(); 
     } 
    } 

這是我班的片斷屬性:

class loadImage extends JPanel { 
private int choice; 
double scale; 
double currScale = 1.0; 
BufferedImage img; 
//Timer timer; (if using the timer) 

FIX: 要添加一些背景,我使用Netbean的圖形用戶界面的生成器。每次我需要使用loadImage的方法時,我都會選擇手動強制將面板轉換爲loadImage類。

public void ZoomIn() { 
    double scale = 3.0; 
    double scaleAddOn = 0.1; 
    Timer timer; 
    timer = new Timer(40, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      double currScale; 
      currScale = ((loadImage)imageArea).getCurrScale(); 
      if(currScale < scale) 
      { 
       ((loadImage)imageArea).setCurrScale(currScale + scaleAddOn); 
      } 
     } 
    }); 

    if(((loadImage)imageArea).getCurrScale() > scale) 
    { 
     timer.stop(); 
    } 
    else 
    { 
     timer.setRepeats(true); 
     timer.setCoalesce(true); 
     timer.start(); 
    } 
}  

再次感謝您的幫助。希望這個頁面對其他人也是有用的。

回答

2

當您從JPanel創建繪圖面板時,paintComponent方法將繪製或繪製。句號。 paintComponent方法不會執行其他任何操作。沒有Thread.sleep。沒有計算。畫出來。

既然你剛纔提供了一個paintComponent方法,我只是提供了DrawingPanel類。

package com.ggl.testing; 

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 

import javax.swing.JPanel; 

public class DrawingPanel extends JPanel { 

    private static final long serialVersionUID = 506794466435048661L; 

    private double currScale; 

    private int choice; 

    private BufferedImage img; 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     System.out.println(choice); 
     if (choice == 1) { 
      g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 
      System.out.println("\nImgWidth: " + img.getWidth() 
        + "\nFrameWidth: " + getWidth() + "\nImgHeight: " 
        + img.getHeight() + "\nFrameHeight: " + getHeight()); 
     } else if (choice == 2) { 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
        RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
      int w = getWidth(); 
      int h = getHeight(); 
      int imageWidth = img.getWidth(); 
      int imageHeight = img.getHeight(); 
      double x = (w - currScale * imageWidth)/2; 
      double y = (h - currScale * imageHeight)/2; 
      AffineTransform at = AffineTransform.getTranslateInstance(x, y); 
      System.out.println("Pass"); 
      at.scale(currScale, currScale); 
      System.out.println("Pass"); 
      g2.drawRenderedImage(img, at); 
      System.out.println("Pass"); 
     } 
    } 

    public double getCurrScale() { 
     return currScale; 
    } 

    public void setCurrScale(double currScale) { 
     this.currScale = currScale; 
     repaint(); 
    } 

    public int getChoice() { 
     return choice; 
    } 

    public void setChoice(int choice) { 
     this.choice = choice; 
    } 

    public BufferedImage getImg() { 
     return img; 
    } 

    public void setImg(BufferedImage img) { 
     this.img = img; 
    } 

} 

您可以使用Runnable類或Swing Timer來更新currScale值。

+0

因此,避免添加除paintComponent之外的任何東西嗎?在閱讀setter方法後,我想我現在有了這個想法。我想知道計時器如何調用繪畫。你也做了一個二傳和二傳的選擇,這是我沒有做的,所以這是你答案的另一個補充。 **執行後編輯:**神聖煙。我能夠做到。非常感謝你的幫助。非常感激。 – RandomInquirer