简体   繁体   English

java缩放图像绘制问题

[英]java scaled image drawing problems

I searched a bit and also asked more experienced friends but could not find anything. 我搜索了一下,还问了更多有经验的朋友,但找不到任何东西。 I'm trying to make a recording software with java (eg like fraps,bandicam). 我正在尝试用java(例如fraps,bandicam)制作录音软件。 Basically it works without problems and even pretty good what I noticed on mistake because I'm making an image every 2 milliseconds and it works good=) the only problem I had was that the image was too big for a little jframe to print on it (of course 1080p screen doesn´t fit on to like 600p frame :/ ) so I tried to scale it with Image.getScaledInstance() but when I print the scaled image it starts to flicker so you see the image for a short period of time (like a half second) and then it's gone. 基本上它没有问题,甚至可以很好地工作,因为我每隔2毫秒就制作一次图像,而且效果很好=)我唯一的问题是图像太大,无法在其中打印jframe (当然1080p屏幕无法适应600p帧:/),因此我尝试使用Image.getScaledInstance()对其进行缩放,但是当我打印缩放后的图像时,它开始闪烁,因此您会在短时间内看到图像时间(大约半秒钟)然后消失了。 (NOTE: when I use the debugger mode it works pretty much perfect, also I'm aware that the framework in the code isn´t good and im not saving anything yet. I am using the Netbeans IDE). (注意:当我使用调试器模式时,它的工作原理非常完美,我也意识到代码中的框架不是很好,并且还没有保存任何内容。我正在使用Netbeans IDE。)

Code: 码:

package Main;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Lucas
 */
public class MainClass {

    private DisplayFrame DF;
    private boolean isRecOn;
    private int counter = 0;
    private BufferedImage[] images;
    public void MainClass(){
        DF = new DisplayFrame(this);
        DF.setVisible(true); 
    }

    /**
     *
     */
    public void on_offRec(boolean on_off){

        this.isRecOn = on_off;
        if(on_off == true){
            try {
                this.doRecording();
            } catch (AWTException ex) {
                Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    private void doRecording() throws AWTException{

        Robot robo = new Robot();


        BufferedImage bi = this.giveAFrame(robo);  
        boolean changeDisplay = DF.changeDisplay(bi);
        if(changeDisplay == true){
            Timer timy = new Timer();
            TimerTask tt = new TimerTask() {
                @Override
                public void run() {
                    try {
                        doRecording();
                    } catch (AWTException ex) {
                        Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            };
            timy.schedule(tt, 2);
        }
        this.counter = this.counter +1;

    }

    private BufferedImage giveAFrame(Robot robo){
        Dimension rectt = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle rect = new Rectangle(rectt.width,rectt.height);
        BufferedImage createScreenCapture = robo.createScreenCapture(rect);
        return createScreenCapture;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MainClass mc = new MainClass();
        mc.MainClass();
    }

}



package Main;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;

/**
 *
 * @author Lucas
 */
public class DisplayFrame extends javax.swing.JFrame {

    private boolean isRecOn = false;
    private Object recall;
    /**
     * Creates new form DisplayFrame
     */
    public DisplayFrame(Object returner) {
        initComponents();

        recall = returner;



        this.getRootPane().addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                // This is only called when the user releases the mouse button.
                IGotResized(e);
            }
        });
     }


    private void IGotResized(ComponentEvent e){
        System.out.println("Testification rezised msg");
    }
    public boolean changeDisplay(BufferedImage bi){
        Graphics g = this.getGraphics();
        Image i = bi.getScaledInstance(this.getWidth() - 20, this.getHeight() - jButton1.getX() - 10, Image.SCALE_FAST);
        g.drawImage(i,0 + 20 , 0+20, this);  
        return isRecOn;
    }
    /**
     * 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() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Start/Stop");
        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()
                .addContainerGap()
                .addComponent(jButton1)
                .addContainerGap(434, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(353, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (isRecOn == true){
            isRecOn = false;
        }else{
            isRecOn = true;
            ((MainClass)(recall)).on_offRec(isRecOn); 
        }
    }                                        

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

        /* Create and display the form */

    }

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

the problem is, that you draw the image in your custom function. 问题是,您在自定义函数中绘制了图像。 In swing every component has it's own paint(...) function, that will be called by the framework if the component needs to be redrawn: 在摇摆中,每个组件都有其自己的paint(...)函数,如果需要重绘该组件,则框架将调用该函数:

So you need to override the paint(...) function in your DisplayFrame class. 因此,您需要在DisplayFrame类中重写paint(...)函数。 Save your current Image as class parameter and use it in the overridden paint(...) function. 将当前图像保存为类参数,并在覆盖的paint(...)函数中使用它。

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

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