Java自定义组件像素颜色

时间:2016-06-01 11:35:58

标签: java swing colors drawing pixel

我在我的项目中使用的一些自定义组件出现问题。它绘制得很好,但现在我想找到某种颜色的第一个像素的坐标并且有一些麻烦。

这是我的组件代码:

class DrawPad extends JComponent {

    private LinkedList<Line> lines = new LinkedList<>();

    DrawPad() {
        setDoubleBuffered(true);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                lines.add(new Line());
                lines.getLast().add(e.getPoint());
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                lines.getLast().add(e.getPoint());
                repaint();
            }
        });
    }

    void clear() {
        lines.clear();
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        if (!lines.isEmpty()) {
            for (Line line : lines) {
                // TODO
                LinkedList<Point> points = line.getPoints();
                Point previous = points.getFirst(), current = previous;
                for (int i = 1; i < points.size(); i++) {
                    current = points.get(i);
                    g.drawLine(previous.x, previous.y, current.x, current.y);
                    previous = current;
                }
            }
        }
    }
}

我知道它可能会被优化,但现在它并不那么重要。

任何人都可以帮我写一个搜索某种颜色的第一个像素的方法吗?

我最近发现它必须对BufferedImage做一些事情,但不知道如何实现它。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

以下示例将Icon描绘成BufferedImage,并为RED设置find()像素以进行发现。将鼠标悬停在其他像素上以查看基础颜色。

System.out.println(find(Color.RED));
…
private Point find(Color color) {
    for (int r = 0; r < imgH; r++) {
        for (int c = 0; c < imgW; c++) {
            if (img.getRGB(c, r) == color.getRGB()) {
                System.out.println(c + "," + r + ": "
                    + String.format("%08X", img.getRGB(c, r)));
                return new Point(c, r);
            }
        }
    }
    return new Point(-1 , -1);
}

image

控制台:

32,32: FFFF0000
java.awt.Point[x=32,y=32]

代码:

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.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 * @see http://stackoverflow.com/a/37574791/230513
 * @see http://stackoverflow.com/questions/2900801
 */
public class Grid extends JPanel {

    private static final int SCALE = 8;
    private final BufferedImage img;
    private int imgW, imgH, paneW, paneH;

    public Grid(String name) {
        Icon icon = UIManager.getIcon(name);
        imgW = icon.getIconWidth();
        imgH = icon.getIconHeight();
        img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) img.getGraphics();
        icon.paintIcon(null, g2d, 0, 0);
        g2d.dispose();
        img.setRGB(imgW / 2, imgH / 2, Color.RED.getRGB());
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                int x = p.x * imgW / paneW;
                int y = p.y * imgH / paneH;
                int c = img.getRGB(x, y);
                setToolTipText(x + "," + y + ": "
                    + String.format("%08X", c));
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(imgW * SCALE, imgH * SCALE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        paneW = this.getWidth();
        paneH = this.getHeight();
        g.drawImage(img, 0, 0, paneW, paneH, null);
        System.out.println(find(Color.RED));
    }

    private Point find(Color color) {
        for (int r = 0; r < imgH; r++) {
            for (int c = 0; c < imgW; c++) {
                if (img.getRGB(c, r) == color.getRGB()) {
                    System.out.println(r + "," + c + ": "
                        + String.format("%08X", img.getRGB(c, r)));
                    return new Point(c, r);
                }
            }
        }
        return new Point(-1 , -1);
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Grid("OptionPane.informationIcon"));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}