簡體   English   中英

通過paintComponent和緩沖圖像在JPanel上繪圖的區別

[英]Difference between drawing on JPanel via paintComponent and Buffered Image

我嘗試了兩種不同的方法來繪制相同的形狀,第一個圖像是通過覆蓋 JPanel 的 paintComponent(Graphics g) 方法並使用 g.drawOval(..) 等繪制的,第二個圖像是通過創建緩沖圖像和繪圖來繪制的通過使用緩沖圖像的圖形來處理它。 如何在兩種方法上實現相同的渲染質量? 我嘗試過使用許多不同的渲染提示,但沒有一個提供相同的質量。 我還嘗試使用 Kernel 和過濾進行銳化,但仍然不能。

private void createImage() {
        image = new BufferedImage(IMG_SIZE, IMG_SIZE, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();

        gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        gr.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        //something along the way
        gr.drawOval(.....);
        gr.drawLine(.....);
        gr.drawOval(.....);

        panel.repaint();
        gr.dispose();
}



public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(backgroundColor);
    if (USE_BUFFERED_IMAGE) {
        g.drawImage(image, startX, startY, null);
    } else {
        //something along the way
        g.drawOval(.....);
        g.drawLine(.....);
        g.drawOval(.....);
    }
}

使用 JPanel paintComponent 繪圖

使用緩沖圖像圖形繪制,然后通過 drawimage 在 Jpanel 上繪制

編輯

通過獲取面板圖形的幾乎所有設置並將它們應用於緩沖圖像圖形,我找到了我的解決方案。 不是僅使用相同的渲染提示或“最小可重現示例”方法。 在這里,導入的事情是面板的圖形將所有內容縮放 1.25,然后在面板上顯示之前縮小到原始圖形。

這是一個示例,-這與我的代碼不完全一樣,這只是給您一個想法的示例-

private void createImages(Paint paint, RenderingHints hints,
                         AffineTransform transform, Stroke stroke,
                        Composite composite, GraphicsConfiguration config ){

        image = config.createCompatibleImage(IMG_SIZE, IMG_SIZE, 
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        // same options
        gr.setPaint(paint);
        gr.setRenderingHints(hints);
        gr.setTransform(transform);
        gr.setStroke(stroke);
        gr.setComposite(composite);

        //something along the way
        gr.drawOval(.....);
        gr.drawLine(.....);
        gr.drawOval(.....);

        panel.repaint();
        gr.dispose();
}



public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(backgroundColor);
    if (USE_BUFFERED_IMAGE) {
       
        Graphics2D g2 = (Graphics2D)g;
        createImages(g2.getPaint(), g2.getRenderingHints(),g2.getTransform(),
                     g2.getStroke(),g2.getComposite(), g2.getDeviceConfiguration());
         //scaling down is important because your drawings get scaled to 1.25
         // by panels graphics' transformation
         g.drawImage(image, startX, startY,(int)(IMG_SIZE*0.8),(int)(IMG_SIZE*0.8),  null);
    } else {
        //something along the way
        g.drawOval(.....);
        g.drawLine(.....);
        g.drawOval(.....);
    }
}

通過獲取面板圖形的幾乎所有設置並將它們應用於緩沖圖像圖形,我找到了我的解決方案。 在這里,導入的事情是面板的圖形將所有內容縮放 1.25,然后在面板上顯示之前縮小到原始圖形。

這是一個示例,-這與我的代碼不完全一樣,這只是給您一個想法的示例-

private void createImages(Paint paint, RenderingHints hints,
                         AffineTransform transform, Stroke stroke,
                        Composite composite, GraphicsConfiguration config ){

        image = config.createCompatibleImage(IMG_SIZE, IMG_SIZE, 
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = image.createGraphics();
        // same options
        gr.setPaint(paint);
        gr.setRenderingHints(hints);
        gr.setTransform(transform);
        gr.setStroke(stroke);
        gr.setComposite(composite);

        //something along the way
        gr.drawOval(.....);
        gr.drawLine(.....);
        gr.drawOval(.....);

        panel.repaint();
        gr.dispose();
}



public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(backgroundColor);
    if (USE_BUFFERED_IMAGE) {
       
        Graphics2D g2 = (Graphics2D)g;
        createImages(g2.getPaint(), g2.getRenderingHints(),g2.getTransform(),
                     g2.getStroke(),g2.getComposite(), g2.getDeviceConfiguration());
         //scaling down is important because your drawings get scaled to 1.25
         // by panels graphics' transformation
         g.drawImage(image, startX, startY,(int)(IMG_SIZE*0.8),(int)(IMG_SIZE*0.8),  null);
    } else {
        //something along the way
        g.drawOval(.....);
        g.drawLine(.....);
        g.drawOval(.....);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM