2012-11-13 89 views
1

我試圖讓這兩段代碼一起工作,但我遇到了問題。代碼的第一部分構建了由我的編輯器(Netbeans GUI Builder for Java)生成的表單,但不確定這是否是造成問題的原因。JPanel Image獲取多次運行

發生了什麼事情是當我運行項目時,我的代碼(第二代碼塊)運行並反轉圖像的顏色(正是它應該做的),然後它恢復到原始圖像(非反轉),不應該發生,圖像應該在面板中顯示爲非反轉圖像。然後當我點擊框架邊框來重新調整面板圖像的閃爍大小時,反轉,然後反轉。再次單擊它,它會執行相同的操作,但會停止在非倒轉狀態。什麼導致這個奇怪的問題?

package weblaftest; 

/** 
* 
* @author Ryan 
*/ 
public class Main extends javax.swing.JFrame{ 
    /** 
    * Creates new form Main 
    */ 
    public Main(){ 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jInternalFrame1 = new javax.swing.JInternalFrame(); 
     test1 = new weblaftest.test(); 

     jInternalFrame1.setVisible(true); 

     javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane()); 
     jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout); 
     jInternalFrame1Layout.setHorizontalGroup(
      jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 0, Short.MAX_VALUE) 
     ); 
     jInternalFrame1Layout.setVerticalGroup(
      jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 0, Short.MAX_VALUE) 
     ); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout test1Layout = new javax.swing.GroupLayout(test1); 
     test1.setLayout(test1Layout); 
     test1Layout.setHorizontalGroup(
      test1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 409, Short.MAX_VALUE) 
     ); 
     test1Layout.setVerticalGroup(
      test1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 362, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(test1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(262, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(83, Short.MAX_VALUE) 
       .addComponent(test1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(74, 74, 74)) 
     ); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 

    public static void main(String args[]){ 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try{ 
      for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()){ 
       if("Nimbus".equals(info.getName())){ 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     }catch(ClassNotFoundException ex){ 
      java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     }catch(InstantiationException ex){ 
      java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     }catch(IllegalAccessException ex){ 
      java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     }catch(javax.swing.UnsupportedLookAndFeelException ex){ 
      java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable(){ 
      @Override 
      public void run(){ 
       new Main().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JInternalFrame jInternalFrame1; 
    private weblaftest.test test1; 
    // End of variables declaration 
} 

這部分是我寫的,它是爲顯示圖像,用於測試目的面板中的代碼,它是所有反轉是顯示在面板上的圖像色彩。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package weblaftest; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import weblaftest.grapics.Colors; 

/** 
* 
* @author Ryan 
*/ 
public class test extends javax.swing.JPanel{ 
private BufferedImage image; 
    /** 
    * Creates new form test 
    */ 
    public test(){ 
     try{ 
      image = ImageIO.read(new File("/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg")); 
     }catch(IOException e){ 
     } 
    } 

    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     int width = image.getWidth(); 
     int height = image.getHeight(); 
     for(int x = 0; x < width; x++){ 
      for(int y = 0; y < height; y++){ 
       int color = image.getRGB(x, y); 
       int red = Colors.red(color); 
       int green = Colors.green(color); 
       int blue = Colors.blue(color); 

       int rgb = Colors.rgba(255 - red, 255 - green, 255 - blue); 
       image.setRGB(x, y, rgb); 
      } 
     } 
     g.drawImage(image, 0, 0, width, height, Color.black, null); 
    } 


    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 
    }// </editor-fold> 
    // Variables declaration - do not modify 
    // End of variables declaration 
} 

這裏是請求的顏色類:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package weblaftest.grapics; 

/** 
* 
* @author Ryan 
*/ 
public class Colors{ 

    public static int rgba(int red, int green, int blue, Integer alpha){ 
     int rgba = alpha; 
     rgba = (rgba << 8) + red; 
     rgba = (rgba << 8) + green; 
     rgba = (rgba << 8) + blue; 
     return rgba; 
    } 
    public static int rgba(int red, int green, int blue){ 
     int rgba = 255; 
     rgba = (rgba << 8) + red; 
     rgba = (rgba << 8) + green; 
     rgba = (rgba << 8) + blue; 
     return rgba; 
    } 

    public static int alpha(int color){ 
     return color >> 24 & 0x0FF; 
    } 

    public static int red(int color){ 
     return color >> 16 & 0x0FF; 
    } 

    public static int green(int color){ 
     return color >> 8 & 0x0FF; 
    } 

    public static int blue(int color){ 
     return color & 0x0FF; 
    } 
} 
+0

請向我們提供'Colors'類 –

回答

1
  • 保持paintComponent()只繪製沒有長時間運行的處理應在paintComponent(..)
  • 類名來完成應即Test大寫字母開頭

這是問題:

@Override 
public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    for(int x = 0; x < width; x++){ 
     for(int y = 0; y < height; y++){ 
      int color = image.getRGB(x, y); 
      int red = Colors.red(color); 
      int green = Colors.green(color); 
      int blue = Colors.blue(color); 

      int rgb = Colors.rgba(255 - red, 255 - green, 255 - blue); 
      image.setRGB(x, y, rgb); 
     } 
    } 
    g.drawImage(image, 0, 0, width, height, Color.black, null); 
} 

您在第一次通話畫出倒BufferedImagepaintComponent()在連續通話將重新VERT每一次再次反轉。

而是反轉在構造函數中BufferedImage和油漆只在paintComponent(..)BufferedImage不做在paintComponent(..)反轉,因此爲JFrame倒和原始圖像之間的切換的尺寸重新調整:

/** 
* 
* @author Ryan 
*/ 
public class Test extends javax.swing.JPanel { 

    private BufferedImage image; 
    int width; 
    int height; 

    /** 
    * Creates new form test 
    */ 
    public Test() { 
     try { 
      image = ImageIO.read(new URL("http://imgs.mi9.com/uploads/flower/4533/tulip-flowers_400x300_80023.jpg")); 
      invertImage(); 
     } catch (Exception e) { 
     } 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(image, 0, 0, width, height, Color.black, null); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGap(0, 400, Short.MAX_VALUE)); 
     layout.setVerticalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGap(0, 300, Short.MAX_VALUE)); 
    }// </editor-fold> 
    // Variables declaration - do not modify 
    // End of variables declaration 

    private void invertImage() { 
     width = image.getWidth(); 
     height = image.getHeight(); 
     for (int x = 0; x < width; x++) { 
      for (int y = 0; y < height; y++) { 
       int color = image.getRGB(x, y); 
       int red = Colors.red(color); 
       int green = Colors.green(color); 
       int blue = Colors.blue(color); 

       int rgb = Colors.rgba(255 - red, 255 - green, 255 - blue); 
       image.setRGB(x, y, rgb); 
      } 
     } 
    } 
} 
+0

謝謝!那正是錯誤!油漆運行到底是什麼時候?每一幀? –

+1

不是每一幀,但paintComponent被定期調用,並且每調用一次setRGB都會反轉圖像。當然,您可以更快速地反轉圖像/ g。 –

+0

+1給JoopEggen。 @RyanNaddy它現在被調用,除非另一個組件調用組件的繪畫鏈。在這種情況下,當我們重新調整'JFrame'的大小時,'JPanel'被重新調整大小,因此調用'paintComponent(...)' –