【发布时间】:2016-03-27 15:05:43
【问题描述】:
我想获取 BufferedImage 上像素的颜色。我将 BufferedImage 的背景设置为白色,然后在 BufferedImage 上画一条从 (100, 100) 到 (100, 200) 的线。然后,我将 BufferedImage 绘制到 JPanel 上。有线条但背景不是白色。为什么?
此外,对于 R、G 和 B,getRGB 方法返回 0,即使它不是 getRGB(100, 100)。怎么了?
代码:
public class PixelColour extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = bi.createGraphics();
gbi.setColor(Color.black);
gbi.setBackground(Color.white);
gbi.drawLine(100, 100, 100, 200);
g2.drawImage(bi, null, 0, 0);
int rgb = bi.getRGB(100, 100);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);
System.out.println(red + " " + green + " " + blue);
}
public static void main(String[] args) throws IOException{
PixelColour pc = new PixelColour();
JFrame frame = new JFrame("Pixel colour");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(pc);
frame.setSize(500,500);
frame.setVisible(true);
}
}
【问题讨论】:
-
这个答案有什么问题?? stackoverflow.com/questions/36246988/…
-
@gpasch 我正在尝试使用 BufferedImage 而不是 Robot。
标签: java swing colors pixel bufferedimage