Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Get Pixel

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get Pixel

    Hello

    I'm trying to write a minor "snake" clone; but I have some problems with the code

    I want to retrieve a pixel at (x,y) and check its color; and according to my googling I should use g.getRGB(x,y), but I can't get it to work.


    import java.applet.Applet;
    import java.awt.*;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.image.BufferedImage;
     
    import javax.swing.JFrame;
     
    public class SnakeTest2 extends Applet {
      int gap = 3;
     
      int mx, my;
     
      Image buffer = null;
     
      int w, h;
     
      public static void main(String[] a) {
        JFrame f = new JFrame();
        f.setSize(258, 277);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new SnakeTest2());
        f.setVisible(true);
      }
     
      public SnakeTest2() {
        setSize(258, 277);
        Dimension d = getSize();
        w = d.width;
        h = d.height;
        buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      }
     
      public void paint(Graphics g) {
    	  Graphics screengc = null;
     
        screengc = g;
     
     
        g = buffer.getGraphics();
     
     
     
      	g.setColor(Color.BLUE);
    	g.fillRect(0,0,250,250);
    	g.setColor(Color.BLACK);
    	g.fillRect(5,5,240,240);
     
     
      // I want to check if the color of pixel (x,y) = RED, how do I do it?
     
     
     
        screengc.drawImage(buffer, 0, 0, null);
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Get Pixel

    Where were you told that? Graphics doesn't have a getRGB() method, so that doesn't make much sense.

    You might be looking for the getPixelColor() in the Robot class. The API is your friend.

    But I'd be willing to bet there's an easier/better way to do this. For example, why are you testing the color of a pixel? Game logic should not be in your painting code (painting logic is fine). Are you trying to do collision detection?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get Pixel

    The reason I'm testing is for collisiontesting.

    I'm not quite sure of how to approach the problem in any different way.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Get Pixel

    Well if you're trying to do collision testing, you almost definitely don't want to be testing for collisions in your paint method.

    But if you know where the objects are to be painted, then you should be able to figure out where they are for testing collisions. This isn't exactly trivial, but it's been done countless times before. Google is your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get Pixel

    I was thinking that the performance of fetching data from one pixel and react on the result would be much better than to store an entire area in an array and try to keep track of which pixels are colored that way for example. If thinking of a snake clone and the snake being of length 150 pixels, that would mean at least 150 iterations to just make sure you haven't run into the snake each time you are to update one pixel, as compared to just checking one single pixel to see if it's black or not. And it also gets around the problem of trying to figure out what pixels a circel of size x would color.

    But still, I would be happy to hear any good alternative solutions to this problem!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Get Pixel

    If what you're testing for collisions is indeed only one pixel, then I suppose that's fine. But for most games, you're going to be checking the collision of quite a few pixels- or of paths, or walls, etc.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get Pixel

    I tried to use the robot class; and it worked to some extent, the problem is that the robotclass only works if it's on top of the screen. I want to read the pixel of the active window. And yes; I can fetch the proper location, but if another window is on top of the app window, the robotclass doesn't do it.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Get Pixel

    Quote Originally Posted by Ophe View Post
    I tried to use the robot class; and it worked to some extent, the problem is that the robotclass only works if it's on top of the screen. I want to read the pixel of the active window. And yes; I can fetch the proper location, but if another window is on top of the app window, the robotclass doesn't do it.
    Yep, that's another reason not to use the pixel value.

    You could try painting the game window to an image first, then using the image to get the pixel value, but I would predict that simply doing more standard collision checking would be simpler. Up to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM