1

これら 2 つのコード セクションを連携させようとしていますが、問題が発生しています。このコードの最初のセクションは、私のエディター (Netbeans GUI Builder for Java) によって生成されたフォームを作成しますが、これが問題の原因であるかどうかはわかりません。

プロジェクトを実行すると、コード (コードの 2 番目のブロック) が実行され、画像の色が反転し (正確に何をすべきか)、元の画像に戻ります (非反転)。画像は非反転画像としてパネルに表示されるはずです。次に、フレームの境界線をクリックしてサイズを変更すると、パネルの画像が点滅して反転し、次に非反転になり、次に反転します。もう一度クリックすると同じことが行われますが、非反転で停止します。この奇妙な問題の原因は何ですか?

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
}

要求された Colors クラスは次のとおりです。

/*
 * 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;
    }
}
4

1 に答える 1

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);
}

BufferedImage最初の呼び出しで反転を描画し、paintComponent()後続の呼び出しで毎回反転して再び反転します。

むしろBufferedImage、コンストラクターでを反転し、反転したものだけをペイントします。反転BufferedImagepaintComponent(..)ないでください。したがって、サイズ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);
            }
        }
    }
}
于 2012-11-13T16:34:51.020 に答える