public void applyForce() { // force is proportional to the diff between restLen and current idst // a vector from A --> B Vector diff = new Vector(endA.pos, endB.pos); // compute the current distance float dist = diff.getMagnitude(); // compute dx, which is what the force depends on float dx = Math.abs(dist - restLen); // a vector containing just the direction component of A --> B Vector dir = diff.copy().normalize(); Vector force = dir.copy().scale(-K * dx, -K * dx); if (restLen < getDistance()) { // forces go INWARDS endB.addForce(force); endA.addForce(force.reverse()); } else { // forces go OUTWARDS endA.addForce(force); endB.addForce(force.reverse()); } }
private void drawShooter() { stroke(255); double x2 = width / 2 - 75 * (Math.cos(radians((float) angledegrees))); // calculates point to draw line from double y2 = 75 * (Math.sin( radians( (float) angledegrees))); // calculates y co-ord to draw line from using trig // fucntions line( width / 2, height - (15 * height / 480), (float) x2, height - (15 * height / 480) - (float) y2); // draws line if (angledegrees == 170 || angledegrees == 10) { // stop ball being shot horizontally. HAHA WE DIDN'T FORGET!!! increase = increase * -1; // sets shooter to rotate in opposite direction } if (canfire) { // checks if can fire angledegrees += increase; // increases only when true. This stops the animation whil ball is moving } }
/** * @param colors * @param step * @param wrap * @return */ private int colorsBetween(final int[] colors, final float step, boolean wrap) { int length = colors.length - 1; if (wrap) { length = colors.length; } if (step <= 0) { return colors[0]; } if (step >= 1) { return colors[colors.length - 1]; } int a = (int) Math.floor(length * step); float f = 1.0f / length; float newStep = (step - (a * f)) / f; int nextA = Math.min(a + 1, length); if (wrap) { if (nextA >= colors.length) { nextA = 0; } } return colorBetween(colors[a], colors[nextA], newStep); }
public void run() { while (running) { if (!paused) { if (mode == 0) { for (double i = 0; i < 3.14f; i = i + 0.1f) { for (int j = 0; j < 12; j++) { double osc = Math.sin(i) * 127; int value = (int) osc; MIDILight(j, value); } delay(30); } for (double i = 3.14f; i >= 0; i = i - 0.1f) { for (int j = 0; j < 12; j++) { double osc = Math.sin(i) * 127; int value = (int) osc; MIDILight(j, value); } delay(30); } } else if (mode == 1) { for (int j = 0; j < 12; j++) { MIDILight(j, 127); } mode = 1000; } else if (mode == 2) { for (int j = 0; j < 12; j++) { MIDILight(j, 0); } mode = 1000; } else if (mode == 3) { int controller = (int) random(12); int randomValue = (int) random(127); MIDILight(controller, randomValue); delay(30); } else if (mode == 4) { for (int i = 0; i < 64; i++) { for (int j = 0; j < 12; j++) { MIDILight(j, i * 2); } delay(20); } for (int i = 63; i >= 0; i--) { for (int j = 0; j < 12; j++) { MIDILight(j, i * 2); } delay(20); } } else { delay(10); // FIX THIS. WITHOUT THIS THE CPU USE GOES THROUGH THE ROOF. NEED A WAY // TO SLEEP THIS THREAD WHEN IT'S NOT IN USE. } } } System.out.println(id + " thread is done!"); }
/** * Returns a random unit Quaternion. * * <p>You can create a randomly directed unit vector using: * * <p>{@code PVector randomDir = new PVector(1.0f, 0.0f, 0.0f);} <br> * {@code randomDir = Quaternion.multiply(Quaternion.randomQuaternion(), randomDir);} */ public static final Quaternion randomQuaternion() { float seed = (float) Math.random(); float r1 = PApplet.sqrt(1.0f - seed); float r2 = PApplet.sqrt(seed); float t1 = 2.0f * PI * (float) Math.random(); float t2 = 2.0f * PI * (float) Math.random(); return new Quaternion( PApplet.sin(t1) * r1, PApplet.cos(t1) * r1, PApplet.sin(t2) * r2, PApplet.cos(t2) * r2); }
public void draw() { fill(0); noStroke(); rect(0, höjd, bredd, höjd + 21); stroke(220); fill(230); mouse = toComplex(mouseX, mouseY); text(" X: " + Math.round(mouse.getRe() * 1000000) / 1000000.0, 5, höjd + 16); text("Y: " + Math.round(mouse.getIm() * 1000000) / 1000000.0 + "i", bredd / 2, höjd + 16); }
public boolean CheckIntersection(PVector point) { float dist = (float) Math.sqrt( Math.pow((double) (point.x - origin.x), 2.0) + Math.pow((double) (point.y - origin.y), 2.0)); if (dist < radiusX) { return true; } return false; }
/* Perform Simulation */ public void simulate(float dt) { // Accelerate angular speed float requiredSpeed = targetAngularSpeed - angularSpeed; float angularAccel = requiredSpeed / dt; angularAccel = PApplet.constrain(angularAccel, -maxAngularAccel, maxAngularAccel); // Limit Angular speed angularSpeed += angularAccel * dt; angularSpeed = PApplet.constrain(angularSpeed, -maxAngularSpeed, maxAngularSpeed); // Orientation Simulation orientation += angularSpeed * dt; // Position simulation PVector worldRequiredSpeed = targetSpeed.get(); worldRequiredSpeed.rotate(orientation); worldRequiredSpeed.sub(speed); // PVector worldRequiredSpeed = worldTargetSpeed.get(); float dSpeed = worldRequiredSpeed.mag(); float dAcell = dSpeed / dt; float dForce = Math.min(dAcell * getMass(), motorForce); worldRequiredSpeed.normalize(); worldRequiredSpeed.mult(dForce); force.add(worldRequiredSpeed); super.simulate(dt); }
// Set the target angular speed public void setRotation(float speed) { if (speed == Float.NaN) { System.out.println("Excep: setRotation"); return; } targetAngularSpeed = (float) Math.toRadians(speed); }
public void setup() { int window_height = 220; size((int) (window_height * 5.12f), 220); canvas = createGraphics((int) (window_height * 5.12f) / 2, 220, JAVA2D); textMode(SCREEN); textFont(createFont("SanSerif", 12)); minim = new Minim(this); in = minim.getLineIn(Minim.MONO, buffer_size, sample_rate); // create an FFT object that has a time-domain buffer // the same size as line-in's sample buffer fft = new FFT(in.bufferSize(), in.sampleRate()); // Tapered window important for log-domain display fft.window(FFT.HAMMING); // initialize peak-hold structures peaksize = 1 + Math.round(fft.specSize() / binsperband); peaks = new float[peaksize]; peak_age = new int[peaksize]; particles = new Particle[fft.specSize()]; for (int i = 0; i < fft.specSize(); i++) particles[i] = new Particle(i); }
public boolean isPrime(double dNum) { for (int i = 2; i <= Math.sqrt(dNum); i++) { if (dNum % i == 0) { return false; } } return true; }
private void drawSavedValues() { background(0); // now draw current spectrum for (int i = 0; i < saved_freqs.length; i++) { int[] c = calculated_color(i); stroke(c[0], c[1], c[2]); noFill(); int x1 = (int) map( legend_width + i, legend_width, legend_width + spectrum_width, legend_width, width); line(x1, spectrum_height, x1, saved_freqs[i]); int x2 = x1 + 1; line(x2, spectrum_height, x2, saved_freqs[i]); } int[] color_waveform = calculated_color(loudest_freq); stroke(color_waveform[0], color_waveform[1], color_waveform[2]); for (int i = 0; i < saved_buffer.length - 1; i++) { line(i + 50, height / 2 + saved_buffer[i] * 50, i + 51, height / 2 + saved_buffer[i] * 50); } drawParticles(); if (drawScale) { // add legend // frequency axis fill(255); stroke(255); int y = spectrum_height; line(legend_width, y, legend_width + spectrum_width, y); // horizontal line // x,y address of text is immediately to the left of the middle of the letters textAlign(CENTER, TOP); for (float freq = 0.0f; freq < in.sampleRate() / 2; freq += 2000.0) { int x = legend_width + (fft.freqToIndex(freq) * 2); // which bin holds this frequency line(x, y, x, y + 4); // tick mark text(Math.round(freq / 1000) + "kHz", x, y + 5); // add text label } // level axis int x = legend_width; line(x, 0, x, spectrum_height); // vertictal line textAlign(RIGHT, CENTER); for (float level = -100.0f; level < 100.0; level += 20.0) { y = spectrum_height - (int) (dB_scale * (level + gain)); line(x, y, x - 3, y); text((int) level + " dB", x - 5, y); } } canvas.endDraw(); image(canvas, 0, 0, width, height); // draw canvas streched to sketch dimensions }
public void keyPressed() { if (key == ENTER) { background(0); minX = -2.0; maxX = 1.0; minY = -1.2; maxY = 1.2; // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } else if (key == 's') { saveFrame("mandelbrot-####.jpg"); } else if (key == 'q') { c = new Complex(-0.123, 0.745); ritaUt(); } else if (key == 'w') { c = new Complex(-0.391, 0.587); ritaUt(); } else if (key == 'e') { c = new Complex(0, 1); ritaUt(); } else if (key == 'r') { c = new Complex(-0.75, 0); ritaUt(); } else if (key == '5') { c = new Complex(-0.75, 0); } else if (key == '6') { c = new Complex(-0.75, 0); } else if (key == '7') { c = new Complex(-0.75, 0); } else if (key == '8') { c = new Complex(-0.75, 0); } else if (key == '9') { c = new Complex(-0.75, 0); } }
public void drawGrid() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (gridA[i][j].getValue() == 0) { fill(255); rect(j * 105, i * 105, 100, 100); } else { fill( (float) (256 - (256 / 11) * (Math.log(gridA[i][j].getValue())) / (Math.log(2))), (float) ((256 / 11) * (Math.log(gridA[i][j].getValue()) / Math.log(2))), (float) 0.0); rect(j * 105, i * 105, 100, 100); fill(255); text(toString(gridA[i][j].getValue()), j * 105 + 50, i * 105 + 50); } } } }
public int[] sunColorSec(float seconds) { int[] rgb = new int[3]; float diffFrom30 = Math.abs(30 - seconds); float ratio = diffFrom30 / 30; rgb[0] = (int) (255 * ratio); rgb[1] = (int) (255 * ratio); rgb[2] = 0; return rgb; }
public void mouseReleased() { mouse = toComplex(mouseX, mouseY); if (mouseButton == LEFT) { background(0); minX = (mouse.getRe() - 0.25 * breddZ); maxX = (mouse.getRe() + 0.25 * breddZ); minY = (mouse.getIm() - 0.25 * höjdZ); maxY = (mouse.getIm() + 0.25 * höjdZ); // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } else if (mouseButton == RIGHT) { background(0); minX = (mouse.getRe() - 1.5 * breddZ); maxX = (mouse.getRe() + 1.5 * breddZ); minY = (mouse.getIm() - 1.5 * höjdZ); maxY = (mouse.getIm() + 1.5 * höjdZ); // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } }
private void drawParticles() { pushStyle(); colorMode(RGB, 255); // background(0); popStyle(); for (int i = 0; i < fft.specSize() - 1; i++) { float val = dB_scale * (20 * ((float) Math.log10(fft.getBand(i)))); if (fft.getBand(i) == 0) { val = -200; } // avoid log(0) particles[i].update(val); particles[i].render(); } }
/** Displays this marker's name in a box. */ public void draw(PGraphics pg, float x, float y) { pg.pushStyle(); pg.pushMatrix(); if (selected) { pg.translate(0, 0, 1); } pg.strokeWeight(strokeWeight); if (selected) { pg.fill(highlightColor); pg.stroke(highlightStrokeColor); } else { pg.fill(color); pg.stroke(strokeColor); } pg.ellipse(x, y, size, size); // TODO use radius in km and convert to px // label if (selected && name != null) { if (font != null) { pg.textFont(font); } pg.fill(highlightColor); pg.stroke(highlightStrokeColor); pg.rect( x + strokeWeight / 2, y - fontSize + strokeWeight / 2 - space, pg.textWidth(name) + space * 1.5f, fontSize + space); pg.fill(255, 255, 255); pg.text( name, Math.round(x + space * 0.75f + strokeWeight / 2), Math.round(y + strokeWeight / 2 - space * 0.75f)); } pg.popMatrix(); pg.popStyle(); }
private void calcCam(boolean is3D) { if (rotVector.mag() != 0 || panVector.mag() != 0 || camDist != prevCamDist) { prevCamDist = camDist; camRot += rotVector.x; if ((camPitch + rotVector.y) < 0.99 && (camPitch + rotVector.y) > 0.01) camPitch += rotVector.y; if (is3D) { PVector camPan = new PVector( (panVector.x * (float) Math.sin(Math.PI * 0.5 + (Math.PI * camRot))) + (panVector.y * (float) Math.sin(Math.PI * 1.0 + (Math.PI * camRot))), (panVector.y * (float) Math.cos(Math.PI * 1.0 + (Math.PI * camRot))) + (panVector.x * (float) Math.cos(Math.PI * 0.5 + (Math.PI * camRot)))); camPan.mult(0.05f * PVector.dist(camPos, camCenter)); camCenter.x += camPan.x; camCenter.y += camPan.y; camPan.x = 0; camPan.y = 0; } else { camCenter.x -= 0.05f * (camPos.z - camCenter.z) * panVector.x; camCenter.y += 0.05f * (camPos.z - camCenter.z) * panVector.y; } panVector.x = 0; panVector.y = 0; rotVector.x = 0; rotVector.y = 0; camPos.x = camCenter.x - camDist * (float) Math.sin(Math.PI * camRot) * (float) Math.sin(Math.PI * camPitch); camPos.y = camCenter.y - camDist * (float) Math.cos(Math.PI * camRot) * (float) Math.sin(Math.PI * camPitch); camPos.z = camCenter.z - camDist * (float) Math.cos(Math.PI * camPitch); } }
public void draw() { canvas.beginDraw(); if (paused) { drawSavedValues(); return; } // clear window // background(0); fill(0, fill_amount); // semi-transparent white rect(0, 0, width, height); fill(random(255)); // perform a forward FFT on the samples in input buffer fft.forward(in.mix); loudest_freq = 0; loudest_db = -1000; // now draw current spectrum for (int i = 0; i < spectrum_width; i++) { // draw the line for frequency band i using dB scale float val = dB_scale * (20 * ((float) Math.log10(fft.getBand(i))) + gain); if (fft.getBand(i) == 0) { val = -200; } // avoid log(0) int y = spectrum_height - Math.round(val); y = y > spectrum_height ? spectrum_height : y; if (i > 20 && val > loudest_db) { loudest_db = (int) val; loudest_freq = i; } int[] c = calculated_color(i); stroke(c[0], c[1], c[2]); noFill(); int x1 = (int) map( legend_width + i, legend_width, legend_width + spectrum_width, legend_width, width); // int x1 = legend_width + i; int x2 = x1 + 1; if (i < 520) { line(x1, spectrum_height, x1, y); line(x2, spectrum_height, x2, y); } saved_freqs[i] = y; } int[] color_waveform = calculated_color(loudest_freq); println( "Peak: " + loudest_freq + " dB: " + loudest_db + " Color " + color_waveform[0] + " " + color_waveform[1] + " " + color_waveform[2]); stroke(color_waveform[0], color_waveform[1], color_waveform[2]); drawWaveForm(); drawParticles(); // level axis if (drawScale) { // add legend // frequency axis fill(255); stroke(255); int y = spectrum_height; // line(legend_width,y,legend_width+spectrum_width,y); // horizontal line // x,y address of text is immediately to the left of the middle of the letters textAlign(CENTER, TOP); for (float freq = 0.0f; freq < in.sampleRate() / 2; freq += 2000.0) { int x = legend_width + (fft.freqToIndex(freq) * 2); // which bin holds this frequency line(x, y, x, y + 4); // tick mark text(Math.round(freq / 1000) + "kHz", x, y + 5); // add text label } int x = legend_width; line(x, 0, x, spectrum_height); // vertictal line textAlign(RIGHT, CENTER); for (float level = -100.0f; level < 100.0; level += 20.0) { y = spectrum_height - (int) (dB_scale * (level + gain)); line(x, y, x - 3, y); text((int) level + " dB", x - 5, y); } } drawArcs(); canvas.endDraw(); image(canvas, 0, 0, width, height); // draw canvas streched to sketch dimensions }
public class julia1 extends PApplet { public final int bredd = 960; public final int höjd = 720; double minX = -2.0; double maxX = 2.0; double minY = -1.5; double maxY = 1.5; public int maxIterations = 100; Complex c = new Complex(-0.123, 0.745); ////////////////////////////////////////////////////////////////////////////////////////// double breddZ = (Math.abs(maxX - minX)); double höjdZ = (Math.abs(maxY - minY)); public double pixelPerRe = (double) (bredd / breddZ); public double pixelPerIm = (double) (höjd / höjdZ); Complex mouse; PFont font; ////////////////////////////////////////////////////////////////////////////////////////// public void setup() { size(bredd, höjd + 21 /*, P2D*/); background(0); stroke(255); fill(255); font = createFont("Courier New", 16, true); textFont(font); ritaUt(); } //////////////////////////////////////////////////////////////////////////////////// public void draw() { fill(0); noStroke(); rect(0, höjd, bredd, höjd + 21); stroke(220); fill(230); mouse = toComplex(mouseX, mouseY); text(" X: " + Math.round(mouse.getRe() * 1000000) / 1000000.0, 5, höjd + 16); text("Y: " + Math.round(mouse.getIm() * 1000000) / 1000000.0 + "i", bredd / 2, höjd + 16); } /////////////////////////////////////////////////////////////////////////// public void ritaUt() { Complex z, hej; boolean isInside; int antalN = 0; background(0); for (int i = 0; i < width * height; i++) { isInside = true; z = toComplex(indexToPixel(i)[0], indexToPixel(i)[1]); for (int a = 0; a <= maxIterations; a++) { if ((z.getRe() * z.getRe() + z.getIm() * z.getIm()) > 4.0) { isInside = false; antalN = a; break; } hej = z.mul(z); z = hej.add(c); } if (isInside) { stroke(0); point(indexToPixel(i)[0], indexToPixel(i)[1]); } else { // the smmoth fade from black to red to yellow to white to yellow to black. if (antalN <= 12) { stroke(antalN * 21, 0, 0); } else if (antalN <= 24) { stroke(252, (antalN - 12) * 21, 0); } else if (antalN >= 76) { stroke(255 - (antalN - 76) * 7, 140 - (antalN - 76) * 8, 0); } else if (antalN >= 40) { stroke(252, 252 - (antalN - 40) * 4, 140 - (antalN - 40) * 9); } else { stroke(255 /*-((antalN-24)/6)*/, 252, (antalN - 24) * 9); } point(indexToPixel(i)[0], indexToPixel(i)[1]); } } } /////////////////////////////////////////////////////////////////////////// public void mouseReleased() { mouse = toComplex(mouseX, mouseY); if (mouseButton == LEFT) { background(0); minX = (mouse.getRe() - 0.25 * breddZ); maxX = (mouse.getRe() + 0.25 * breddZ); minY = (mouse.getIm() - 0.25 * höjdZ); maxY = (mouse.getIm() + 0.25 * höjdZ); // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } else if (mouseButton == RIGHT) { background(0); minX = (mouse.getRe() - 1.5 * breddZ); maxX = (mouse.getRe() + 1.5 * breddZ); minY = (mouse.getIm() - 1.5 * höjdZ); maxY = (mouse.getIm() + 1.5 * höjdZ); // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } } public void keyPressed() { if (key == ENTER) { background(0); minX = -2.0; maxX = 1.0; minY = -1.2; maxY = 1.2; // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } else if (key == 's') { saveFrame("mandelbrot-####.jpg"); } else if (key == 'q') { c = new Complex(-0.123, 0.745); ritaUt(); } else if (key == 'w') { c = new Complex(-0.391, 0.587); ritaUt(); } else if (key == 'e') { c = new Complex(0, 1); ritaUt(); } else if (key == 'r') { c = new Complex(-0.75, 0); ritaUt(); } else if (key == '5') { c = new Complex(-0.75, 0); } else if (key == '6') { c = new Complex(-0.75, 0); } else if (key == '7') { c = new Complex(-0.75, 0); } else if (key == '8') { c = new Complex(-0.75, 0); } else if (key == '9') { c = new Complex(-0.75, 0); } } /////////////////////////////////////////////////////////////////////////// public int[] indexToPixel(int i) { int[] pixel = {i % bredd, (i / bredd)}; return pixel; } public Complex toComplex(int x, int y) { return new Complex(minX + x * (maxX - minX) / (bredd), maxY - y * (maxY - minY) / (höjd)); } public int[] toCoordinate(Complex punkt) { int[] koordinater = { (int) (punkt.getRe() * pixelPerRe + (bredd / 2)), (int) ((höjd / 2) + (-1 * punkt.getIm() * pixelPerIm)) }; return koordinater; } ////////////////////////////////////////////////////////////////////////////// // Krävs för att använda processings lib. public static void main(String args[]) { PApplet.main(new String[] {"--present", "julia1"}); } }
public boolean launchVirtualMachine(boolean presenting) { String[] vmParams = getMachineParams(); String[] sketchParams = getSketchParams(presenting); // PApplet.printArray(sketchParams); int port = 8000 + (int) (Math.random() * 1000); String portStr = String.valueOf(port); // Older (Java 1.5 and earlier) version, go figure // String jdwpArg = "-Xrunjdwp:transport=dt_socket,address=" + portStr + // ",server=y,suspend=y"; // String debugArg = "-Xdebug"; // Newer (Java 1.5+) version that uses JVMTI String jdwpArg = "-agentlib:jdwp=transport=dt_socket,address=" + portStr + ",server=y,suspend=y"; // Everyone works the same under Java 7 (also on OS X) String[] commandArgs = new String[] {Base.getJavaPath(), jdwpArg}; commandArgs = PApplet.concat(commandArgs, vmParams); commandArgs = PApplet.concat(commandArgs, sketchParams); // PApplet.println(commandArgs); // commandArg.setValue(commandArgs); launchJava(commandArgs); AttachingConnector connector = (AttachingConnector) findConnector("com.sun.jdi.SocketAttach"); // PApplet.println(connector); // gets the defaults Map<String, Argument> arguments = connector.defaultArguments(); // Connector.Argument addressArg = // (Connector.Argument)arguments.get("address"); // addressArg.setValue(addr); Connector.Argument portArg = arguments.get("port"); portArg.setValue(portStr); // Connector.Argument timeoutArg = // (Connector.Argument)arguments.get("timeout"); // timeoutArg.setValue("10000"); // PApplet.println(connector); // prints the current // com.sun.tools.jdi.AbstractLauncher al; // com.sun.tools.jdi.RawCommandLineLauncher rcll; // System.out.println(PApplet.javaVersion); // http://java.sun.com/j2se/1.5.0/docs/guide/jpda/conninv.html#sunlaunch try { // boolean available = false; // while (!available) { while (true) { try { vm = connector.attach(arguments); // vm = connector.attach(arguments); if (vm != null) { // generateTrace(); // available = true; return true; } } catch (IOException e) { // System.out.println("waiting"); // e.printStackTrace(); try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(sketchErr); } } } // } catch (IOException exc) { // throw new Error("Unable to launch target VM: " + exc); } catch (IllegalConnectorArgumentsException exc) { throw new Error("Internal error: " + exc); } }
public boolean launchVirtualMachine(boolean presenting) { String[] vmParams = getMachineParams(); String[] sketchParams = getSketchParams(presenting); int port = 8000 + (int) (Math.random() * 1000); String portStr = String.valueOf(port); // Older (Java 1.5 and earlier) version, go figure // String jdwpArg = "-Xrunjdwp:transport=dt_socket,address=" + portStr + // ",server=y,suspend=y"; // String debugArg = "-Xdebug"; // Newer (Java 1.5+) version that uses JVMTI String jdwpArg = "-agentlib:jdwp=transport=dt_socket,address=" + portStr + ",server=y,suspend=y"; // Everyone works the same under Java 7 (also on OS X) String[] commandArgs = new String[] {Base.getJavaPath(), jdwpArg}; /* String[] commandArgs = null; if (!Base.isMacOS()) { commandArgs = new String[] { Base.getJavaPath(), jdwpArg }; } else { // Decided to just set this to 1.6 only, because otherwise it's gonna // be a shitshow if folks are getting Apple's 1.6 with 32-bit and // Oracle's 1.7 when run in 64-bit mode. ("Why does my sketch suck in // 64-bit? Why is retina broken?) // The --request flag will prompt to install Apple's 1.6 JVM if none is // available. We're specifying 1.6 so that we can get support for both // 32- and 64-bit, because Oracle won't be releasing Java 1.7 in 32-bit. // Helpfully, the --request flag is not present on Mac OS X 10.6 // (luckily it is also not needed, because 1.6 is installed by default) // but it requires an additional workaround to not use that flag, // otherwise will see an error about an unsupported option. The flag is // available with 10.7 and 10.8, the only other supported versions of // OS X at this point, because we require 10.6.8 and higher. That also // means we don't need to check for any other OS versions, the user is // a douchebag and modifies Info.plist to get around the restriction. if (false) { if (System.getProperty("os.version").startsWith("10.6")) { commandArgs = new String[] { "/usr/libexec/java_home", "--version", "1.6", "--exec", "java", "-d" + Base.getNativeBits(), jdwpArg }; } else { // for 10.7, 10.8, etc commandArgs = new String[] { "/usr/libexec/java_home", "--request", // install on-demand "--version", "1.6", "--exec", "java", "-d" + Base.getNativeBits(), // debugArg, jdwpArg }; } } else { // testing jdk-7u40 commandArgs = new String[] { //"/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/java", Base.getJavaPath(), jdwpArg }; } } */ commandArgs = PApplet.concat(commandArgs, vmParams); commandArgs = PApplet.concat(commandArgs, sketchParams); // PApplet.println(commandArgs); // commandArg.setValue(commandArgs); launchJava(commandArgs); AttachingConnector connector = (AttachingConnector) findConnector("com.sun.jdi.SocketAttach"); // PApplet.println(connector); // gets the defaults Map arguments = connector.defaultArguments(); // Connector.Argument addressArg = // (Connector.Argument)arguments.get("address"); // addressArg.setValue(addr); Connector.Argument portArg = (Connector.Argument) arguments.get("port"); portArg.setValue(portStr); // Connector.Argument timeoutArg = // (Connector.Argument)arguments.get("timeout"); // timeoutArg.setValue("10000"); // PApplet.println(connector); // prints the current // com.sun.tools.jdi.AbstractLauncher al; // com.sun.tools.jdi.RawCommandLineLauncher rcll; // System.out.println(PApplet.javaVersion); // http://java.sun.com/j2se/1.5.0/docs/guide/jpda/conninv.html#sunlaunch try { // boolean available = false; // while (!available) { while (true) { try { vm = connector.attach(arguments); // vm = connector.attach(arguments); if (vm != null) { // generateTrace(); // available = true; return true; } } catch (IOException e) { // System.out.println("waiting"); // e.printStackTrace(); try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } } } // } catch (IOException exc) { // throw new Error("Unable to launch target VM: " + exc); } catch (IllegalConnectorArgumentsException exc) { throw new Error("Internal error: " + exc); } }
// Return location at coordinates x, y on the screen public Location locationAt(float mousex, float mousey) { Location l = new Location((int) Math.floor((mousex - x) / dx), (int) Math.floor((mousey - y) / dy)); return l; }