gpt4 book ai didi

Java Swing BufferedImage 质量差

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:05 24 4
gpt4 key购买 nike

我正在尝试在 BufferedImage 上创建绘图,然后复制到 JPanel 上。当我直接在 JPanel 上绘制时,图片质量非常好,但是当使用中间 BufferedImage 时,质量/分辨率明显降低。我已经使用 OSX 的辅助功能面板中的缩放选项进行了检查。我正在 MacBook Pro Retina 上开发。

是否发生了某种自动缩放?我对 BufferedImage 做错了什么?

这是演示问题的代码

package com.sample.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class QualityProblem {

private static final double DOT_SIZE = 4;

public static void main(String[] args) {
JFrame frame = new JFrame("ChartPanel demo");
frame.setBackground(Color.WHITE);

// JPanel draw = new DrawingOK();
JPanel draw = new DrawingUgly();
draw.setBackground(Color.BLACK);

frame.getContentPane().add(draw, BorderLayout.CENTER);
frame.setSize(new Dimension(1200, 900));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
}

private static class DrawingOK extends JPanel {

private static final long serialVersionUID = 1L;

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2draw = (Graphics2D) g.create();
try {
g2draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2draw.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2draw.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

Ellipse2D.Double e = new Ellipse2D.Double(50, 50, DOT_SIZE, DOT_SIZE);
g2draw.setColor(Color.YELLOW);
g2draw.fill(e);
} finally {
g2draw.dispose();
}
}
}

private static class DrawingUgly extends JPanel {

private static final long serialVersionUID = 1L;

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Dimension size = getParent().getSize();
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);

Graphics2D ig = image.createGraphics();
ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ig.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
ig.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

Graphics2D g2draw = (Graphics2D) g.create();
try {
Ellipse2D.Double e = new Ellipse2D.Double(50, 50, DOT_SIZE, DOT_SIZE);
ig.setColor(Color.YELLOW);
ig.fill(e);
g2draw.drawImage(image, 0, 0, null);
} finally {
ig.dispose();
g2draw.dispose();
}
}
}
}

编辑:添加了 4 像素点和 50D 放大的图像。丑陋的一个来自 BufferedImage 复制到屏幕的 Graphics Small 4pixel dot Big balls

最佳答案

我修正了你的绘图代码。

这是丑陋的 GUI。

ChartPanel demo

  1. 我将面板的大小移到了面板构造函数中。设置框架大小包括边框。设置面板大小可为您提供所需的绘图区域。

  2. 我将黑色背景绘制移到了 paintComponent 方法中。你还不如在一个地方完成所有的绘画。

  3. 我清理了您的绘图代码。您无需复制 paintComponent 图形实例即可获取 Graphics2D。

  4. 我把圆放大了,所以你可以看到清晰度。我将原点移到了圆心,并将 DOT_SIZE 变成了半径。

这是代码。

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class QualityProblem implements Runnable {

private static final double DOT_SIZE = 50D;

public static void main(String[] args) {
SwingUtilities.invokeLater(new QualityProblem());
}

@Override
public void run() {
JFrame frame = new JFrame("ChartPanel demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.WHITE);

// JPanel draw = new DrawingOK();
JPanel draw = new DrawingUgly();

frame.getContentPane().add(draw, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}

private class DrawingOK extends JPanel {

private static final long serialVersionUID = 1L;

public DrawingOK() {
this.setPreferredSize(new Dimension(600, 400));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2draw = (Graphics2D) g;

g2draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2draw.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2draw.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);

g2draw.setColor(Color.BLACK);
g2draw.fillRect(0, 0, getWidth(), getHeight());

Ellipse2D.Double e = new Ellipse2D.Double(300D - DOT_SIZE,
200D - DOT_SIZE, DOT_SIZE + DOT_SIZE, DOT_SIZE + DOT_SIZE);
g2draw.setColor(Color.YELLOW);
g2draw.fill(e);
}
}

private class DrawingUgly extends JPanel {

private static final long serialVersionUID = 1L;

public DrawingUgly() {
this.setPreferredSize(new Dimension(600, 400));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

BufferedImage image = new BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_RGB);

Graphics2D ig = image.createGraphics();
ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ig.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
ig.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);

ig.setColor(Color.BLACK);
ig.fillRect(0, 0, getWidth(), getHeight());

Ellipse2D.Double e = new Ellipse2D.Double(300D - DOT_SIZE,
200D - DOT_SIZE, DOT_SIZE + DOT_SIZE, DOT_SIZE + DOT_SIZE);
ig.setColor(Color.YELLOW);
ig.fill(e);
ig.dispose();

g.drawImage(image, 0, 0, this);
}
}

}

关于Java Swing BufferedImage 质量差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com