java - 如何从 Graphics g 获取像素颜色

标签 java swing graphics2d

在我的学习中,我有图形类(class)。我们有 Bresenham 用于画线和画圆。下一课我将学习洪水填充。对于洪水填充,我需要获取像素颜色来检查是否需要填充。 这是我现在所有类(class)中的代码。

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获取像素颜色?

最佳答案

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

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

关于java - 如何从 Graphics g 获取像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22839618/

相关文章:

java - 在 GUI 页面之间切换的可取设计模式是什么?

c++ - 初学者 openGL 项目渲染三角形的问题

Java在选定的位置将一个字符串分割成多个

java - 使用 arrayList 作为 Graphics2d 中的 y 坐标

java - Swing Java 中的 Jcalendar

java - 图像未显示在 JPanel 上?

java - 返回字符解析KML文件

java - 在linux中连接POP3S服务器

java - 如何将 JLabel 变成按钮?

java - 尝试使用 tomcat 对 solr 实例执行完全导入