如何从Graphics g获取像素颜色

时间:2014-04-03 13:42:00

标签: java swing graphics2d

在我的学习上,我有图形课程。我们有Bresenham用于画线和圆画。在下一课我会学习洪水填充。对于洪水填充,我需要获得像素颜色以检查是否需要填充或不填充。 这是我所有课程的代码。

package lab1;
import javax.swing.*;  
import java.awt.*; 
import java.util.Random;

public class Lab1 extends JPanel{

    private Random random = new Random();
    private boolean isRed;
    private String s = "";
    private int Fill(int x,int y,Graphics g)
    {
        if ((x < 0) || (y < 0) || (x >= 600) || (y >= 600)) return 0;
        return 0;
    }
    private void drawCircle(int centerX,int centerY,int radius, Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
        int d = (5 - radius * 4)/4;
        int x = 0;
        int y = radius;
        Color circleColor = Color.white;

        do {
            g2d.drawRect(centerX + x, centerY + y, 1,1);
            g2d.drawRect(centerX + x, centerY - y, 1,1);
            g2d.drawRect(centerX - x, centerY + y, 1,1);
            g2d.drawRect(centerX - x, centerY - y, 1,1);
            g2d.drawRect(centerX + y, centerY + x, 1,1);
            g2d.drawRect(centerX + y, centerY - x, 1,1);
            g2d.drawRect(centerX - y, centerY + x,1,1);
            g2d.drawRect(centerX - y, centerY - x, 1,1);
            if (d < 0) {
                d += 2 * x + 1;
            } else {
                d += 2 * (x - y) + 1;
                y--;
            }
            x++;
        } while (x <= y);

    }
    public void line(int x,int y,int x2, int y2, Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        //super.paintComponent(g);
    int w = x2 - x ;
    int h = y2 - y ;
    int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
    if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
    if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
    if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
    int longest = Math.abs(w) ;
    int shortest = Math.abs(h) ;
    if (!(longest>shortest)) {
        longest = Math.abs(h) ;
        shortest = Math.abs(w) ;
        if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
        dx2 = 0 ;            
    }
    int numerator = longest >> 1 ;
    for (int i=0;i<=longest;i++) {
        g2d.drawRect(x, y, 1, 1);
        numerator += shortest ;
        if (!(numerator<longest)) {
            numerator -= longest ;
            x += dx1 ;
            y += dy1 ;
        } else {
            x += dx2 ;
            y += dy2 ;
        }
    }
}
    public Lab1() {

    }
  public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        drawCircle(100,500,50,g);
        drawCircle(500,500,50,g);
        g.setColor(Color.red);
        line(50,450,550,450,g);
        line(50,450,50,350,g);
        line(50,350,150,350,g);
        line(150,350,325,175,g);
        line(325,175,400,175,g);
        line(400,175,400,325,g);
        line(400,325,550,325,g);
        line(550,325,550,450,g);


    }

    //main method: create an instance of TestPanel and output it on a JFrame
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(600, 600);
        f.setTitle("Sometimes Red, Sometimes Blue");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new Lab1());
        f.setVisible(true);
    } 


    }

如何从x和y获得像素颜色?

3 个答案:

答案 0 :(得分:8)

您无法从Graphics对象读取像素。图形是绘图的抽象,可能没有像素(想想打印,绘图等)。

你可以创建一个java.awt.BufferedImage并使用它的createGraphics()方法来获取要渲染的Graphics,同时能够使用BufferedImage的getRGB()方法读取像素来访问像素。

答案 1 :(得分:0)

而不是我重写已经写过的东西,看看这个链接 How to color a pixel?

答案 2 :(得分:0)

我建议填写BufferedImage。在那里,您可以使用BufferedImage.getRGB

获取每个像素的RGB值