繁体   English   中英

Java Canvas drawImage闪烁

[英]Java Canvas drawImage Flashes

我总是在StackOverflow中寻找问题,但是我似乎没有找到答案。

我正在使用Java创建带有Swing表单的应用程序。 我已将Canvas AWT组件添加到JFrame。

我从数据库中预加载图像,将其绘制到画布中,然后将JFrame设置为可见。

问题是图像将闪烁:显示了几毫秒,然后重新绘制了JFrame。

为什么会这样? 我也尝试覆盖JFrame中的paint()方法,并在每次调用paint绘制图像。 但这不是这种方式。

Button Frame (主框架)-(将final String imgPath替换为您选择的示例图像)

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ButtonFrame extends javax.swing.JFrame {

    public ButtonFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("View Image");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addComponent(jButton1)
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(jButton1)
                .addContainerGap(70, Short.MAX_VALUE))
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        final String imgPath = "/tmp/img.png";

        try
        {
            BufferedImage bi = ImageIO.read(new File(imgPath));

            ImgFrame imgFrame = new ImgFrame();
            imgFrame.setImage(bi);
            imgFrame.setVisible(true);

//            imgFrame.setImageInCanvas(bi);

        } catch (IOException x) {}
    }                                        

    /**
     * @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(ButtonFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ButtonFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ButtonFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ButtonFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}

ImageFrame (显示图像的帧)

import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class ImgFrame extends javax.swing.JFrame {

    BufferedImage bi;

    public ImgFrame() {
        initComponents();
    }

    public void setImage(BufferedImage bi)
    {
        this.bi = bi;
    }

    public void setImageInCanvas(BufferedImage bi)
    {
        canvas1.getGraphics().drawImage(bi, 0, 0, null);
    }

    @Override
    public void paint(Graphics g)
    {
        setImageInCanvas(bi);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        canvas1 = new java.awt.Canvas();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

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


    // Variables declaration - do not modify                     
    private java.awt.Canvas canvas1;
    // End of variables declaration                   
}

影片范例: https//drive.google.com/file/d/0B53gOV_BD8J8Mm10LW5CN2E3M2M/view


问题已解决:

我已经在JFrame中覆盖了paint方法,如下所示:

@Override
public void paint(Graphics g)
{
    g.drawImage(bi, 0, 0, null);
}

其中bi是BufferedImage。

然后,Canvas与Form重叠,因为它已设置为适合JFrame,并且图像未显示。 我已将其删除(AWT画布),并且图像现在显示在背景中。

非常感谢你!

特别感谢Dodd10x和Andrew Thompson。

不要混用Swing和AWT。 要更改的第一件事是,如果要在JFrame中显示,请从Canvas切换到JComponent。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM