如何从图形g中获取像素颜色

时间:2023-02-09 16:50:56

On my study I have graphics lessons. We have Bresenham for line drawing and Circle drawing. On next lesson I will be learning flood fill. For flood fill I need to get pixel color to check whether I need to fill or not to fill. This is my code now from all lessons.

在我的学习中,我上了图形课。我们有布里森纳姆画线和画圆。下节课我将学习洪水填充。对于洪泛填充,我需要获取像素颜色来检查是否需要填充。这是我所有课程的代码。

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


    }

How to get pixel color from x and y ?

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

3 个解决方案

#1


0  

Instead of me rewriting what has already been writtin take a look at this link How to color a pixel?

我不是重写已经写过的内容而是看看这个链接如何给像素上色?

#2


7  

You can not read pixels from a Graphics object. Graphics is an abstraction for drawing, there might not be pixels (think of printing, plotting etc.).

不能从图形对象中读取像素。图形是绘图的抽象,可能没有像素(例如打印、绘图等)。

You can create a java.awt.BufferedImage and use its createGraphics() method to get a Graphics to render into, while being able to read pixels using BufferedImage's getRGB() method to access pixels.

您可以创建一个java.awt。BufferedImage和使用它的createGraphics()方法获取要呈现的图形,同时可以使用BufferedImage的getRGB()方法读取像素来访问像素。

#3


0  

I would suggest to fill into a BufferedImage. There you can get the RGB value of each pixel with BufferedImage.getRGB

我建议填写BufferedImage。在这里,您可以使用BufferedImage.getRGB获得每个像素的RGB值

#1


0  

Instead of me rewriting what has already been writtin take a look at this link How to color a pixel?

我不是重写已经写过的内容而是看看这个链接如何给像素上色?

#2


7  

You can not read pixels from a Graphics object. Graphics is an abstraction for drawing, there might not be pixels (think of printing, plotting etc.).

不能从图形对象中读取像素。图形是绘图的抽象,可能没有像素(例如打印、绘图等)。

You can create a java.awt.BufferedImage and use its createGraphics() method to get a Graphics to render into, while being able to read pixels using BufferedImage's getRGB() method to access pixels.

您可以创建一个java.awt。BufferedImage和使用它的createGraphics()方法获取要呈现的图形,同时可以使用BufferedImage的getRGB()方法读取像素来访问像素。

#3


0  

I would suggest to fill into a BufferedImage. There you can get the RGB value of each pixel with BufferedImage.getRGB

我建议填写BufferedImage。在这里,您可以使用BufferedImage.getRGB获得每个像素的RGB值