Java Reflection - Getting name of color without comparision

In this tutorial I am describing how get value of field/ property defined in class dynamically using Java Reflection.
And i am using it to get name of color (java.awt.Color) using Reflection.

Instead of doing lengthy comparison (shown below), we can do this easily by using java reflection:
   public static String getNameReflection(Color colorParam) {
        try {
            //first read all fields in array
            Field[] field = Class.forName("java.awt.Color").getDeclaredFields();
            for (Field f : field) {
                String colorName = f.getName();
                Class<?> t = f.getType();
                // System.out.println(f.getType());
                // check only for constants - "public static final Color"
                if (t == java.awt.Color.class) {
                    Color defined = (Color) f.get(null);
                    if (defined.equals(colorParam)) {
                        System.out.println(colorName);
                        return colorName.toUpperCase();
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Error... " + e.toString());
        }
        return "NO_MATCH";
    }

Getting name of color by Comparision
One solution to get name of color may be by comparison like this :
    public static String getNameByComparision(Color color) {
        if (color.equals(Color.RED)) {
            return "RED";
        }
        if (color.equals(Color.BLACK)) {
            return "BLACK";
        }
        // ..
        return "NOT_DEFINED";
    }

cygwin: access windows disk, directories and files

Cygwin is a Open-source Linux-like environment for Windows. In this tutorial, I am going to show how we can access the windows directories and files using cygwin.

Java: Screen Capture using Robot and save

Robot : java.awt.Robot  is used to take control of the mouse and keyboard. It is used for test automation, self-running demos and screen capture at various state of execution of the program.
Java Code for detecting screen size and capturing whole screen using Robot and saving :

Java Collision Detection and bounce - Complete example source code download

Complete example of simple collision detection and bounce between circle-circle and ball-rectangular wall.

java collision detection 
Download Full source code : - Can be used in building simple games

Similar posts with little explanations :

Java Collision Detection and bounce- two circles collision and response

Collision detection between two circles and their response - Java source code - working

    public static void intersect(Ball a, Ball b) {
        //ref http://gamedev.stackexchange.com/questions/20516/ball-collisions-sticking-together
        double xDist, yDist;
        xDist = a.x - b.x;
        yDist = a.y - b.y;
        double distSquared = xDist * xDist + yDist * yDist;
        // Check the squared distances instead of the the distances, same
        // result, but avoids a square root.
        if (distSquared <= (a.radius + b.radius) * (a.radius + b.radius)) {
            double speedXocity = b.speedX - a.speedX;
            double speedYocity = b.speedY - a.speedY;
            double dotProduct = xDist * speedXocity + yDist * speedYocity;
            // Neat vector maths, used for checking if the objects moves towards
            // one another.
            if (dotProduct > 0) {
                double collisionScale = dotProduct / distSquared;
                double xCollision = xDist * collisionScale;
                double yCollision = yDist * collisionScale;
                // The Collision vector is the speed difference projected on the
                // Dist vector,
                // thus it is the component of the speed difference needed for
                // the collision.
                double combinedMass = a.getMass() + b.getMass();
                double collisionWeightA = 2 * b.getMass() / combinedMass;
                double collisionWeightB = 2 * a.getMass() / combinedMass;
                a.speedX += (collisionWeightA * xCollision);
                a.speedY += (collisionWeightA * yCollision);
                b.speedX -= (collisionWeightB * xCollision);
                b.speedY -= (collisionWeightB * yCollision);
            }
        }
    }

Java Collision Detection and bounce - Circle and rectangle

Collision detection between circle(any object) and rectangular wall is simple.
For collision detection we simply compare the distances. And if collision between ball and wall is detected, we change the directions of their speeds for bouncing the ball.

Here is the code:

Collision Detection of two circles - Simple Java Code

Java : Drawing of two circles, mouse motion event handler on one circle, and detect collision with other.

Determining whether or not two circles intersect or overlap or collide is done by comparing the distance between the two circles to the sum of radius of the two circles.


Full working code for collision detection only download here: 


Steps
1)Find the distance between the centers of the two circles(a and b) using the distance formula
 float dxSq = (a.x - b.x) * (a.x - b.x);
 float dySq = (a.y - b.y) * (a.y - b.y);
 int d = (int) Math.sqrt(dxSq + dySq);

2)Then the distance is compared with the radii .
    int r1Pr2 = (int) (a.radius + b.radius);
    if (d < r1Pr2) {
        System.out.println("Collided");
    } else if (d == r1Pr2) {
        System.out.println("Just touching");
    } 



Collision detection and response - bounce examples

JavaCV - Color based thresholding in image using OpenCV

JavaCV - Red color based thresholding (RGB-A space) in image using OpenCV : Full working java source code 


Note that the order of colors is BGR-A not RGB-A. 
Read it more from http://stackoverflow.com/questions/367449/bgr-color-space
//static imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
//non-static imports
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

JavaCV capture-save-flip-show live camera

NOTE: Updated code with configuration and example is available here:


--

JavaCV:  Capture/save/flip image and show live image on CanvasFrame from camera

JAVA CODE:
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage; import com.googlecode.javacv.CanvasFrame; import com.googlecode.javacv.FrameGrabber; import com.googlecode.javacv.VideoInputFrameGrabber; import com.googlecode.javacv.cpp.opencv_core.IplImage; public class GrabberShow implements Runnable { //final int INTERVAL=1000;///you may use interval IplImage image; CanvasFrame canvas = new CanvasFrame("Web Cam"); public GrabberShow() { canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); } @Override public void run() { FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next camera int i=0; try { grabber.start(); IplImage img; while (true) { img = grabber.grab(); if (img != null) { cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise cvSaveImage((i++)+"-aa.jpg", img); // show image on window canvas.showImage(img); } //Thread.sleep(INTERVAL); } } catch (Exception e) { } }

public static void main(String[] args) { GrabberShow gs = new GrabberShow(); Thread th = new Thread(gs); th.start(); } }