【问题标题】:Coloring an area of BufferedImage为 BufferedImage 的区域着色
【发布时间】:2014-01-07 10:08:38
【问题描述】:

我想为我拥有的 BufferedImage 的一个子区域着色。我目前正在做:

public BufferedImage paintSubImage(BufferedImage img, int x, int y, int w, int h) {
    Graphics g = img.getGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(x,y,w,h);
    return img;
}

但是我不能给它上色。我做错了吗?

【问题讨论】:

  • 我觉得不错。发布您的 SSCCE 来演示问题,以便我们可以看到您在部分绘制后如何使用 BufferedImage 的上下文。
  • @camickr:这个方法会改变我传递的原始 BufferedImage 对象还是返回未着色的 BufferedImage?
  • 会改变原来的BufferedImage。
  • 但是你可能需要disposeGraphics上下文
  • Given the fact that there is little or no context to go on, it's difficult to know exactly what's wrong. - 这就是为什么要求 OP 获得 SSCCE。显然这个问题对 OP 来说并不太重要,因为他有足够的时间来创建和发布 SSCCE。

标签: java awt bufferedimage


【解决方案1】:

鉴于几乎没有或根本没有上下文可以继续下去,很难确切知道哪里出了问题。

一般的经验法则,如果你创建/打开它,你应该处置/关闭它...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ColorMe {

    public static void main(String[] args) {
        new ColorMe();
    }

    public ColorMe() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            try {
                img = ImageIO.read(new File("/path/to/image..."));

                addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        Point p = e.getPoint();
                        Rectangle bounds = getPictureBounds();
                        if (bounds.contains(p)) {

                            int x = p.x - bounds.x;
                            int y = p.y - bounds.y;

                            splat(img, x, y);
                            repaint();

                        }
                    }

                });

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        protected void splat(BufferedImage img, int x, int y) {
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.fillOval(x - 10, y - 10, 20, 20);
            g2d.dispose();
        }

        protected Rectangle getPictureBounds() {

            Rectangle bounds = new Rectangle();

            if (img != null) {

                bounds.x = (getWidth() - img.getWidth()) / 2;
                bounds.y = (getHeight() - img.getHeight()) / 2;
                bounds.width = img.getWidth();
                bounds.height = img.getHeight();

            }

            return bounds;

        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                Rectangle bounds = getPictureBounds();
                g2d.drawImage(img, bounds.x, bounds.y, this);
                g2d.dispose();
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多