title: How to get pixel color from Graphics g
body: <p>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.</p>
  
  <pre><code>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 &lt; 0) || (y &lt; 0) || (x &gt;= 600) || (y &gt;= 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 &lt; 0) {
                  d += 2 * x + 1;
              } else {
                  d += 2 * (x - y) + 1;
                  y--;
              }
              x++;
          } while (x &lt;= 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&lt;0) dx1 = -1 ; else if (w&gt;0) dx1 = 1 ;
      if (h&lt;0) dy1 = -1 ; else if (h&gt;0) dy1 = 1 ;
      if (w&lt;0) dx2 = -1 ; else if (w&gt;0) dx2 = 1 ;
      int longest = Math.abs(w) ;
      int shortest = Math.abs(h) ;
      if (!(longest&gt;shortest)) {
          longest = Math.abs(h) ;
          shortest = Math.abs(w) ;
          if (h&lt;0) dy2 = -1 ; else if (h&gt;0) dy2 = 1 ;
          dx2 = 0 ;            
      }
      int numerator = longest &gt;&gt; 1 ;
      for (int i=0;i&lt;=longest;i++) {
          g2d.drawRect(x, y, 1, 1);
          numerator += shortest ;
          if (!(numerator&lt;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);
      } 
  
  
      }
  </code></pre>
  
  <p>How to get pixel color from x and y ?</p>