首页 > 解决方案 > Java swing/awt 中的像素完美图形

问题描述

有没有办法在 Swing/AWT 组件中实现像素完美的图形?

我正在通过扩展实现按钮的自定义边框AbstractBorder。出于演示目的,厚度始终等于 3,并且每条线都使用不同的颜色绘制。

import javax.swing.*;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.awt.*;

public class ExampleFrame extends JFrame {

    public ExampleFrame() {
        init();
    }

    public static void main(String[] args) throws UnsupportedLookAndFeelException 
    {
        UIManager.setLookAndFeel(new CustomLookAndFeel());

        ExampleFrame frame = new ExampleFrame();
        frame.setVisible(true);
    }

    private void init() {
        setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        getContentPane().add(panel, BorderLayout.NORTH);

        GridBagConstraints constraints = new GridBagConstraints();

        JButton button1 = new JButton("aaa");
        button1.setVerticalTextPosition(JButton.BOTTOM);
        button1.setHorizontalTextPosition(JButton.CENTER);
        button1.setFocusable(false);

        constraints.insets = new Insets(2, 2, 2, 2);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 3;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.LINE_START;
        panel.add(button1, constraints);

        JButton button2 = new JButton("bbb");
        button2.setHorizontalAlignment(JButton.CENTER);
        button2.setFocusable(false);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.LINE_START;
        panel.add(button2, constraints);

        JButton button3 = new JButton("eee");
        button3.setHorizontalAlignment(JButton.CENTER);
        button3.setFocusable(false);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.LINE_START;
        panel.add(button3, constraints);

        JButton button4 = new JButton("ddd");
        button4.setHorizontalAlignment(JButton.CENTER);
        button4.setFocusable(false);

        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.LINE_START;
        panel.add(button4, constraints);

        pack();
    }

    private static final class CustomBorder extends AbstractBorder {

        private final int thickness;

        public CustomBorder() {
            this(1);
        }

        public CustomBorder(int thickness) {
            this.thickness = thickness;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            //            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
            ((Graphics2D) g).setStroke(new BasicStroke(1.0f));

            for (int i = 0; i < thickness; i++) {
                switch (i) {
                    case 0: {
                        g.setColor(Color.RED);
                    }
                    break;
                    case 1: {
                        g.setColor(Color.GREEN);
                    }
                    break;
                    case 2: {
                        g.setColor(Color.BLUE);
                    }
                    break;
                }

                // top-left -> top-right
                g.drawLine(x, y + i, x + width, y + i);
                // top-left > bottom-left
                g.drawLine(x + i, y, x + i, y + height);
            }
        }

        @Override
        public Insets getBorderInsets(Component c, Insets insets) {
            insets.top = thickness;
            insets.bottom = thickness;
            insets.left = thickness;
            insets.right = thickness;
            return insets;
        }
    }

    private static final class CustomLookAndFeel extends BasicLookAndFeel {

        @Override
        protected void initComponentDefaults(UIDefaults table) {
            super.initComponentDefaults(table);

            // button
            Border buttonBorder = new CustomBorder(3);
            table.put("Button.border", buttonBorder);
            table.put("Button.background", new Color(150, 150, 182));
            table.put("Button.foreground", Color.BLACK);
        }

        @Override
        public String getName() {
            return "custom";
        }

        @Override
        public String getID() {
            return "custom";
        }

        @Override
        public String getDescription() {
            return "custom";
        }

        @Override
        public boolean isNativeLookAndFeel() {
            return false;
        }

        @Override
        public boolean isSupportedLookAndFeel() {
            return true;
        }
    }
}

即使所有 3 个按钮都具有相同的边框参数,我也会显示不同的边框宽度和颜色。

VALUE_STROKE_PURE 和 VALUE_STROKE_NORMALIZE 提示给出不同的结果,但它们都不是像素精确的。

VALUE_STROKE_PURE VALUE_STROKE_NORMALIZE

右侧的按钮“bbb”、“eee”和“ddd”具有相同的宽度和高度,但边框的颜色和总宽度仍然不同。此外,正在使用的默认笔画宽度为 1.0f。

我假设会发生这种情况,因为 Java 2D 几何对浮点数进行操作。有没有办法克服这个限制?

我尝试了不同的例程,例如 drawRect 和不同的抗锯齿提示,但没有运气。

标签: javaswingawtgraphics2d

解决方案


推荐阅读