[Java] getRGB from canvas

Hello,

I am using this link
http://www.java2s.com/Code/Java/2D-Graphics-GUI/Catalog2D-Graphics-GUI.htm

but i can't seem to find a way to get canvas rgb (after I've put something in the window) without creating an image.... but if i create an image, all colors go back to RGB(238,238,238);
I do have some regions which are black, green and red after I paint them on the canvas.
is it possible? Could any ching post some examples on this?
  1. class PaintPanel extends JPanel
  2. {
  3.     public PaintPanel()
  4.     {

  5.        count = 0;
  6.        x = new float[100]; // 400
  7.        y = new float[100]; // 400
  8.        srcok = false;
  9.        destok = false;
  10.        olddestx = 0;
  11.        olddesty = 0;
  12.       
  13.     }

  14.     public void paintComponent(Graphics g)
  15.     {
  16.        super.paintComponent(g);
  17.        Graphics2D g2 = (Graphics2D) g;
  18.        g2.setPaint(paint);
  19.      
  20.       
  21.         g2.setColor(Color.black);

  22.         for (int i = 0; i < count; i++) {
  23.            g2.fillRect((int) x[i],(int)y[i],16,16);
  24.         }

  25.         if (srcx != 0 && srcy != 0) {
  26.         invalidate();
  27.         g2.setColor(Color.green);
  28.         g2.fillRect((int)srcx,(int)srcy,16,16);
  29.       
  30.         }

  31.         if (destx != 0 && desty != 0) {
  32.         invalidate();
  33.          g2.setColor(Color.red);
  34.         g2.fillRect((int)destx,(int)desty,16,16);
  35.         }
  36.     }

  37.     public void setXY(float x1, float y1)
  38.     {
  39.        x[count] = x1;
  40.        y[count] = y1;
  41.        count++;
  42.        repaint();
  43.     }

  44.     public void setDest(float x1, float y1)
  45.     {
  46.       destx = x1;
  47.       desty = y1;
  48.       repaint();
  49.     }

  50.     public void setSource(float x1, float y1)
  51.     {
  52.       srcx = x1;
  53.       srcy = y1;
  54.       repaint();
  55.     }

  56.     public int getImageHeight()
  57.     {
  58.        return image.getHeight();
  59.     }


  60.     public int getImageWidth()
  61.     {
  62.        return image.getWidth();
  63.     }

  64.     public int[][] GetCanvasValues()
  65.     {
  66.       if (image == null)
  67.         initImage();  // this sets the canvas to its original color, not intended


  68.   int[][] rgb = new int[image.getWidth()][image.getHeight()];
  69.   for (int i = 0; i < image.getWidth(); i++) {
  70.     for (int j = 0; j < image.getHeight(); j++) {


  71.       
  72.         rgb[i][j] = image.getRGB(i,j);
  73.       int red = ((rgb[i][j] >> 16) & 0xff);
  74.       int green = ((rgb[i][j] >> 8) & 0xff);
  75.       int blue = (rgb[i][j] & 0xff);


  76.   
  77.       System.out.println("RGB: R=" + red + "G=" + green + "B=" + blue);
  78.       }

  79.    }

  80.    return rgb;

  81. }
  82.    public void initImage()
  83.    {
  84.       image = (BufferedImage) createImage(getWidth(), getHeight());
  85.       Graphics2D g2 = image.createGraphics();
  86.       paint(g2);
  87.       g2.dispose();
  88.    }

  89.     boolean srcok, destok;
  90.     int R,G,B,A;
  91.     BufferedImage image;

  92.     private Paint paint;
  93.     private float x[],y[];
  94.     private float destx, desty;
  95.     private float srcx, srcy;
  96.     private int count;
  97.     private int olddesty, olddestx;

  98. }
複製代碼
Thanks

[ 本帖最後由 luckiejacky 於 2009-9-9 16:33 編輯 ]