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 5 of 5

Thread: Java importing to JFrame get pixel color

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java importing to JFrame get pixel color

    i made this code where you get the pixel color number where ever the mouse is moving the problem is i want it to show up in a jframe and make the JFrame background change color according to the pixel color. For example if the mouse was on white it would print out 255,255,255 and i would like it to show on the Jfream red=255,blue=255,green=255 and then get the background to change white. Could someone help me out and point me to the right direction new to java.

    [code = java]


    <
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.PointerInfo;
    import java.awt.Robot;

    public class PixelColor {
    public static void main(String[] args) throws AWTException {
    PointerInfo pointer;
    pointer = MouseInfo.getPointerInfo();
    Point coord = pointer.getLocation();

    Robot robot = new Robot();
    robot.delay(2000);


    while(true)
    {
    pointer = MouseInfo.getPointerInfo();
    coord = pointer.getLocation();

    System.out.println(" color: " + robot.getPixelColor(coord.x, coord.y).getRed() +
    ", " + robot.getPixelColor(coord.x, coord.y).getGreen() +
    ", " + robot.getPixelColor(coord.x, coord.y).getBlue());
    }










    }
    }
    >
    [/code]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,174 Times in 2,147 Posts

    Default Re: Java importing to JFrame get pixel color

    First thing you will need is a JFrame to display the colors in.
    See the tutorial: http://docs.oracle.com/javase/tutori...ing/index.html
    Post the code and any questions you have about what problems you are having.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java importing to JFrame get pixel color

    thanks and the problem is i know how to create jframe and all
    all i want is to show what my code prints out in the Jframe

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Java importing to JFrame get pixel color

    The frame is displayed and updated using the Swing library of Java. Oracle's Tutorial has a long section on Swing. Google will suggest any number of other tutorials ... of differing quality. Oracle's is well written and tested by much experience, but it is long and aims at being quite comprehensive which is something, as a beginner, you may not really want.

    Don't be afraid to step back from the problem as you have posed it and begin with smaller steps. How to create and display a frame? How to add a component of some sort to the frame? How to change the colour of that component from time to time programatically (ie by an instruction in your code)?

    ---

    One point to notice is that when you use Swing the program logic changes in a fundamental way. You no longer use a while(true) loop in the main() method. Rather the main() method causes the gui to be constructed and shown and that's it. The main() method is, by design, rather small and ends quickly. Moreover a while(true) loop would hog the cpu and that's exactly what you don't want in a responsive gui.

    In addition to frames and components as mentioned above, Swing gives you a couple of ways to avoid the while(true) loop. The first is to detect mouse movements (but only over your frame). Using this approach your application will be told when the mouse moves and you can do your stuff with the robot to determine the pixel colour and update the colour of the component in the frame. This sort of thing is called "event handling" and is fundemental to gui programming. You can probably think of the other events that you can organise to be told about (mouse clicked, key pressed, window minimised, etc...)

    The other way of overcoming the while(true) loop is to use a Timer. Timers cause events to happen, but they aren't event associated with the mouse moving or anything else. They are just events that happen at some periodic rate. So you would use a timer so that your program could be "woken up" and respond 20 times a second, say, and, at that point do your thing with the robot.

    (This second point is something of a digression from where you are now. But I mention it because while(true) loops are a common design flaw when people begin to write Swing programs. And because a brief google reveals that many "intro to Swing" tutorials focus on components and don't necessarily highlight the fact that a timer is available: at some point "Swing Timer" becomes a good thing to google for.)

    ---

    [Edit] Didn't see the previous post. If you are happy with creating a gui, then start there and not the main() you posted originally. Have a look at the link to how to use timers.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java importing to JFrame get pixel color

    thanks a lot i think i got it

Similar Threads

  1. Beginner JAVA help 600x400 pixel three by two primary color square chart.
    By antboy250 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 5th, 2013, 01:20 PM
  2. returning color of pixel in loop
    By Vergil333@gmail.com in forum Loops & Control Statements
    Replies: 2
    Last Post: March 6th, 2012, 08:14 AM
  3. Necessity of importing java.awt.event.*
    By Dracone Cordis in forum Java Theory & Questions
    Replies: 4
    Last Post: January 8th, 2012, 10:50 AM
  4. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  5. Tic Tac Toe Java Application Help w/ Importing Text Field
    By Big Bundy in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 30th, 2011, 02:08 AM

Tags for this Thread