Full Code :
Java Collision Detection and bounce
Colored object tracking in java- javacv code
You need to integrate the ideas from above links.
The full (integrated code will be uploaded shortly)
Red spot in image - position to be detected later |
static Dimension getCoordinates(IplImage thresholdImage) {
int posX = 0;
int posY = 0;
//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.*;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class ColorDetect {
private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
// total samples = (duration in second) * (samples per second)
byte[] sin = new byte[seconds * sampleRate];
double samplingInterval = (double) (sampleRate / frequencyOfSignal);
System.out.println("Sampling Frequency : "+sampleRate);
System.out.println("Frequency of Signal : "+frequencyOfSignal);
System.out.println("Sampling Interval : "+samplingInterval);
for (int i = 0; i < sin.length; i++) {
double angle = (2.0 * Math.PI * i) / samplingInterval;
sin[i] = (byte) (Math.sin(angle) * 127);
System.out.println("" + sin[i]);
}
//initialize source data line - for playback
SourceDataLine line = AudioSystem.getSourceDataLine(audioFormat);
line.open(audioFormat);
line.start();
//play the byteArray
line.write(byteArray, 0, byteArray .length);//(byte[] b, int off, int len)
line.drain();
line.close();
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
/**
* Reads data from the input channel and writes to the output stream
*/
public class MicrophoneRecorder implements Runnable {
// record microphone && generate stream/byte array
private AudioInputStream audioInputStream;
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";
}
public static String getNameByComparision(Color color) {
if (color.equals(Color.RED)) {
return "RED";
}
if (color.equals(Color.BLACK)) {
return "BLACK";
}
// ..
return "NOT_DEFINED";
}
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 collision detection |
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);
}
}
}
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);
int r1Pr2 = (int) (a.radius + b.radius);
if (d < r1Pr2) {
System.out.println("Collided");
} else if (d == r1Pr2) {
System.out.println("Just touching");
}
//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;
<pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace;
color: #000000; background-color: #eee;
font-size: 12px; border: 1px dashed #999999;
line-height: 14px; padding: 5px;
overflow: auto; width: 100%">
<code style="color:#000000;word-wrap:normal;">
<<<<YOUR CODE HERE>>>>
</code>
</pre>
import java.io.*;
import java.util.zip.*;
public class UnzipTest {
public static void unzipFile(File f) throws ZipException, IOException {
ZipInputStream zin = new ZipInputStream(new FileInputStream(f));
System.out.println(f.getAbsoluteFile());
String workingDir = f.getPath() + File.separator + "unziped";
byte buffer[] = new byte[4096];
int bytesRead;
public class TreeTest {
public static void main(String[] args) {
showDir(1, new File("D:\\test"));
}
static void showDir(int indent, File file) {
for (int i = 0; i < indent; i++)
System.out.print(' ');
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
showDir(indent + 4, files[i]);
}
}
}
String SPACE =" ";
String [] words = input.split(SPACE);
Map<String,Integer> frequency = new HashMap<String,Integer>();
for (String word:words){
Integer f = frequency.get(word);
frequency.put(word,f+1);
}
frequency.get(word);
The code to ping a URL from java application is as follows :
With response time:
public static boolean isThisApplicationRunning(final Context context, final String appPackage) {
if (appPackage == null) {
return false;
}
final ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningAppProcessInfo> runningAppProcesses = manager.getRunningAppProcesses();
for (final RunningAppProcessInfo app : runningAppProcesses) {
if (appPackage.equals(app.processName)) {
return true;
}
}
return false;
}
public static boolean isThisServiceRunning( final Context context, final String servicePackageName) {
final ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (final RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (servicePackageName.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
WebView
class allows you to display web pages as a part of your activity layout. WebView becomes helpful when your application frequently displays content from online resources.It simplifies task of performing a network request, parsing the data and rendering it in other Android layout. We can directly make HTTP request to an URL and load the returned HTML into WebView.