2016-06-01 71 views
0

我在使用我的項目中的一些自定義組件時遇到問題。它畫的很好,但現在我想找到某個顏色的第一個像素的座標,並有一些麻煩。Java自定義組件像素顏色

這裏是我的組件代碼:

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

下面的示例將Icon繪製爲BufferedImage,併爲find()設置了RED像素以供發現。將鼠標懸停在其他像素上以查看底層顏色。

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(); 
      } 
     }); 
    } 
}