如何从Graphics g获取像素颜色 [英] How to get pixel color from Graphics g

查看:123
本文介绍了如何从Graphics g获取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


    }

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

How to get pixel color from x and y ?

推荐答案

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

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.).

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

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.

这篇关于如何从Graphics g获取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆